haya-select 1.0.1 → 1.0.4

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.1",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -33,7 +33,7 @@ const LoadedOption = class LoadedOption extends BaseComponent {
33
33
 
34
34
  return (
35
35
  <div
36
- className="custom-select-option"
36
+ className="haya-select-option"
37
37
  data-selected={selected}
38
38
  data-value={digg(loadedOption, "value")}
39
39
  onClick={this.onOptionClicked}
@@ -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,13 +115,13 @@ 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
 
105
122
  return (
106
123
  <div
107
- className={classNames("components-custom-select", className)}
124
+ className={classNames("haya-select", className)}
108
125
  data-id={idForComponent(this)}
109
126
  {...restProps}
110
127
  >
@@ -114,13 +131,13 @@ export default class CustomSelect extends React.PureComponent {
114
131
  </BodyPortal>
115
132
  }
116
133
  <div
117
- className="custom-select-current-selected"
134
+ className="haya-select-current-selected"
118
135
  data-opened={opened}
119
136
  onClick={this.onSelectClicked}
120
137
  >
121
138
  {opened &&
122
139
  <input
123
- className="custom-select-search-text-input"
140
+ className="haya-select-search-text-input"
124
141
  onChange={digg(this, "onSearchTextInputChangedDebounced")}
125
142
  placeholder={I18n.t("haya_select.search_dot_dot_dot")} type="text"
126
143
  ref={digg(this, "searchTextInputRef")}
@@ -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 loadedOptions = await options({
185
- searchValue: digg(this, "searchTextInputRef", "current")?.value
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
  }
@@ -267,7 +295,7 @@ export default class CustomSelect extends React.PureComponent {
267
295
 
268
296
  return (
269
297
  <div
270
- className="custom-select-options-container"
298
+ className="haya-select-options-container"
271
299
  data-id={idForComponent(this)}
272
300
  ref={optionsContainerRef}
273
301
  style={{
@@ -286,7 +314,7 @@ export default class CustomSelect extends React.PureComponent {
286
314
  />
287
315
  )}
288
316
  {loadedOptions?.length === 0 &&
289
- <div className="custom-select-no-options-container">
317
+ <div className="haya-select-no-options-container">
290
318
  {I18n.t("haya_select.no_options_found")}
291
319
  </div>
292
320
  }
@@ -1,4 +1,4 @@
1
- .custom-select-current-selected {
1
+ .haya-select-current-selected {
2
2
  background-color: #fff;
3
3
  border: 1px solid #999;
4
4
  cursor: pointer;
@@ -13,13 +13,13 @@
13
13
  }
14
14
  }
15
15
 
16
- .custom-select-search-text-input {
16
+ .haya-select-search-text-input {
17
17
  border: 0;
18
18
  outline: none;
19
19
  padding: 0;
20
20
  }
21
21
 
22
- .custom-select-options-container {
22
+ .haya-select-options-container {
23
23
  background-color: #fff;
24
24
  border: 1px solid #999;
25
25
  max-height: 300px;
@@ -27,7 +27,7 @@
27
27
  position: absolute;
28
28
  z-index: 99999;
29
29
 
30
- .custom-select-option {
30
+ .haya-select-option {
31
31
  padding: 4px 8px;
32
32
  color: #000;
33
33