haya-select 1.0.2 → 1.0.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "haya-select",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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") return undefined
99
+ if (typeof options == "function") {
100
+ return undefined
101
+ } else if (Array.isArray(options)) {
102
+ return options
103
+ }
87
104
 
88
- return options
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
 
@@ -155,11 +172,15 @@ export default class CustomSelect extends React.PureComponent {
155
172
  defaultValues () {
156
173
  const {attribute, defaultValuesFromOptions, model} = this.props
157
174
 
175
+ console.log("defaultValues")
176
+
158
177
  if (defaultValuesFromOptions) return defaultValuesFromOptions
159
178
 
160
179
  if (attribute && model) {
161
180
  if (!(attribute in model)) throw new Error(`No such attribute on ${model.modelClassData().name}: ${attribute}`)
162
181
 
182
+ console.log(`Default value from ${attribute}: ${model[attribute]()}`)
183
+
163
184
  return model[attribute]()
164
185
  }
165
186
  }
@@ -181,9 +202,20 @@ export default class CustomSelect extends React.PureComponent {
181
202
 
182
203
  loadOptions = async () => {
183
204
  const {options} = digs(this.props, "options")
184
- const loadedOptions = await options({
185
- searchValue: digg(this, "searchTextInputRef", "current")?.value
186
- })
205
+ const searchValue = digg(this, "searchTextInputRef", "current")?.value
206
+
207
+ if (Array.isArray(options)) {
208
+ return this.loadOptionsFromArray(options, searchValue)
209
+ }
210
+
211
+ const loadedOptions = await options({searchValue})
212
+
213
+ this.setState({loadedOptions})
214
+ }
215
+
216
+ loadOptionsFromArray(options, searchValue) {
217
+ const lowerSearchValue = searchValue?.toLowerCase()
218
+ const loadedOptions = options.filter(({text}) => !lowerSearchValue || text.toLowerCase().includes(lowerSearchValue))
187
219
 
188
220
  this.setState({loadedOptions})
189
221
  }