haya-select 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/select/index.jsx +36 -8
package/package.json
CHANGED
package/src/select/index.jsx
CHANGED
|
@@ -23,7 +23,7 @@ const presentOption = (currentValue) => {
|
|
|
23
23
|
)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
throw new Error(
|
|
26
|
+
throw new Error(`Couldn't figure out how to present option from the given keys: ${Object.keys(currentValue, ", ")}`)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
const LoadedOption = class LoadedOption extends BaseComponent {
|
|
@@ -56,6 +56,7 @@ export default class CustomSelect extends React.PureComponent {
|
|
|
56
56
|
static propTypes = {
|
|
57
57
|
attribute: PropTypes.string,
|
|
58
58
|
className: PropTypes.string,
|
|
59
|
+
defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
59
60
|
defaultValues: PropTypes.array,
|
|
60
61
|
defaultValuesFromOptions: PropTypes.array,
|
|
61
62
|
model: PropTypes.object,
|
|
@@ -70,7 +71,7 @@ export default class CustomSelect extends React.PureComponent {
|
|
|
70
71
|
searchTextInputRef = React.createRef()
|
|
71
72
|
|
|
72
73
|
state = {
|
|
73
|
-
currentOptions:
|
|
74
|
+
currentOptions: this.defaultCurrentOptions(),
|
|
74
75
|
loadedOptions: this.defaultLoadedOptions(),
|
|
75
76
|
opened: false,
|
|
76
77
|
optionsLeft: undefined,
|
|
@@ -80,12 +81,28 @@ export default class CustomSelect extends React.PureComponent {
|
|
|
80
81
|
scrollTop: undefined
|
|
81
82
|
}
|
|
82
83
|
|
|
84
|
+
defaultCurrentOptions() {
|
|
85
|
+
const {defaultValue, defaultValues} = this.props
|
|
86
|
+
const {options} = digs(this.props, "options")
|
|
87
|
+
|
|
88
|
+
if (!Array.isArray(options)) return []
|
|
89
|
+
|
|
90
|
+
return options.filter(({value}) =>
|
|
91
|
+
(defaultValue && value == defaultValue) ||
|
|
92
|
+
(defaultValues && defaultValues.includes(value))
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
83
96
|
defaultLoadedOptions() {
|
|
84
97
|
const {options} = digs(this.props, "options")
|
|
85
98
|
|
|
86
|
-
if (typeof options == "function")
|
|
99
|
+
if (typeof options == "function") {
|
|
100
|
+
return undefined
|
|
101
|
+
} else if (Array.isArray(options)) {
|
|
102
|
+
return options
|
|
103
|
+
}
|
|
87
104
|
|
|
88
|
-
|
|
105
|
+
throw new Error(`Unknown type of options: ${typeof options}`)
|
|
89
106
|
}
|
|
90
107
|
|
|
91
108
|
componentDidMount() {
|
|
@@ -98,7 +115,7 @@ export default class CustomSelect extends React.PureComponent {
|
|
|
98
115
|
|
|
99
116
|
render() {
|
|
100
117
|
const {endOfSelectRef} = digs(this, "endOfSelectRef")
|
|
101
|
-
const {attribute, className, defaultValues, model, multiple, onChange, options, search, ...restProps} = this.props
|
|
118
|
+
const {attribute, className, defaultValue, defaultValues, model, multiple, onChange, options, search, ...restProps} = this.props
|
|
102
119
|
const {currentOptions, opened} = digs(this.state, "currentOptions", "opened")
|
|
103
120
|
const BodyPortal = config.getBodyPortal()
|
|
104
121
|
|
|
@@ -181,9 +198,20 @@ export default class CustomSelect extends React.PureComponent {
|
|
|
181
198
|
|
|
182
199
|
loadOptions = async () => {
|
|
183
200
|
const {options} = digs(this.props, "options")
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
201
|
+
const searchValue = digg(this, "searchTextInputRef", "current")?.value
|
|
202
|
+
|
|
203
|
+
if (Array.isArray(options)) {
|
|
204
|
+
return this.loadOptionsFromArray(options, searchValue)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const loadedOptions = await options({searchValue})
|
|
208
|
+
|
|
209
|
+
this.setState({loadedOptions})
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
loadOptionsFromArray(options, searchValue) {
|
|
213
|
+
const lowerSearchValue = searchValue?.toLowerCase()
|
|
214
|
+
const loadedOptions = options.filter(({text}) => !lowerSearchValue || text.toLowerCase().includes(lowerSearchValue))
|
|
187
215
|
|
|
188
216
|
this.setState({loadedOptions})
|
|
189
217
|
}
|