haya-select 1.0.16 → 1.0.18

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.16",
3
+ "version": "1.0.18",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,7 +11,8 @@
11
11
  "dependencies": {
12
12
  "classnames": "^2.3.1",
13
13
  "debounce": "^1.2.1",
14
- "diggerize": "^1.0.4"
14
+ "diggerize": "^1.0.4",
15
+ "fetching-object": "^1.0.1"
15
16
  },
16
17
  "peerDependencies": {
17
18
  "prop-types": ">= 15.7.2",
@@ -37,6 +37,7 @@ export default class HayaSelect extends React.PureComponent {
37
37
  model: PropTypes.object,
38
38
  multiple: PropTypes.bool.isRequired,
39
39
  onChange: PropTypes.func,
40
+ onOptionsClosed: PropTypes.func,
40
41
  options: PropTypes.oneOfType([PropTypes.array, PropTypes.func]).isRequired,
41
42
  placeholder: PropTypes.node,
42
43
  search: PropTypes.bool.isRequired,
@@ -47,6 +48,10 @@ export default class HayaSelect extends React.PureComponent {
47
48
  }))
48
49
  }
49
50
 
51
+ p = fetchingObject(() => this.props)
52
+ s = fetchingObject(() => this.state)
53
+ t = fetchingObject(this)
54
+
50
55
  endOfSelectRef = React.createRef()
51
56
  optionsContainerRef = React.createRef()
52
57
  searchTextInputRef = React.createRef()
@@ -65,7 +70,7 @@ export default class HayaSelect extends React.PureComponent {
65
70
 
66
71
  defaultCurrentOptions() {
67
72
  const {defaultValue, defaultValues} = this.props
68
- const {options} = digs(this.props, "options")
73
+ const {options} = this.p
69
74
 
70
75
  if (!Array.isArray(options)) return []
71
76
 
@@ -76,7 +81,7 @@ export default class HayaSelect extends React.PureComponent {
76
81
  }
77
82
 
78
83
  defaultLoadedOptions() {
79
- const {options} = digs(this.props, "options")
84
+ const {options} = this.p
80
85
 
81
86
  if (typeof options == "function") {
82
87
  return undefined
@@ -96,7 +101,7 @@ export default class HayaSelect extends React.PureComponent {
96
101
  }
97
102
 
98
103
  render() {
99
- const {endOfSelectRef} = digs(this, "endOfSelectRef")
104
+ const {endOfSelectRef} = this.t
100
105
  const {
101
106
  attribute,
102
107
  className,
@@ -107,13 +112,14 @@ export default class HayaSelect extends React.PureComponent {
107
112
  model,
108
113
  multiple,
109
114
  onChange,
115
+ onOptionsClosed,
110
116
  options,
111
117
  placeholder,
112
118
  search,
113
119
  toggleOptions,
114
120
  ...restProps
115
121
  } = this.props
116
- const {currentOptions, opened} = digs(this.state, "currentOptions", "opened")
122
+ const {currentOptions, opened} = this.s
117
123
  const BodyPortal = config.getBodyPortal()
118
124
 
119
125
  return (
@@ -208,7 +214,7 @@ export default class HayaSelect extends React.PureComponent {
208
214
  }
209
215
 
210
216
  loadOptions = async () => {
211
- const {options} = digs(this.props, "options")
217
+ const {options} = this.p
212
218
  const searchValue = digg(this, "searchTextInputRef", "current")?.value
213
219
 
214
220
  if (Array.isArray(options)) {
@@ -225,7 +231,14 @@ export default class HayaSelect extends React.PureComponent {
225
231
  return <OptionGroup key={key} option={loadedOption} />
226
232
  }
227
233
 
228
- return <Option currentOptions={this.state.currentOptions} key={key} option={loadedOption} onOptionClicked={this.onOptionClicked} presentOption={this.presentOption} />
234
+ return <Option
235
+ currentOptions={this.state.currentOptions}
236
+ icon={this.iconForOption(loadedOption)}
237
+ key={key}
238
+ option={loadedOption}
239
+ onOptionClicked={this.onOptionClicked}
240
+ presentOption={this.presentOption}
241
+ />
229
242
  }
230
243
 
231
244
  loadOptionsFromArray(options, searchValue) {
@@ -239,7 +252,7 @@ export default class HayaSelect extends React.PureComponent {
239
252
  e.preventDefault()
240
253
  e.stopPropagation()
241
254
 
242
- const {opened} = digs(this.state, "opened")
255
+ const {opened} = this.s
243
256
 
244
257
  if (opened) {
245
258
  this.closeOptions()
@@ -255,10 +268,12 @@ export default class HayaSelect extends React.PureComponent {
255
268
  loadedOptions: undefined,
256
269
  opened: false
257
270
  })
271
+
272
+ if (this.props.onOptionsClosed) this.props.onOptionsClosed()
258
273
  }
259
274
 
260
275
  openOptions() {
261
- const {endOfSelectRef} = digs(this, "endOfSelectRef")
276
+ const {endOfSelectRef} = this.t
262
277
  const position = endOfSelectRef.current.getBoundingClientRect()
263
278
  const {left, top, width} = digs(position, "left", "top", "width")
264
279
 
@@ -278,35 +293,18 @@ export default class HayaSelect extends React.PureComponent {
278
293
  }
279
294
 
280
295
  onWindowClicked = (e) => {
281
- const {optionsContainerRef} = digs(this, "optionsContainerRef")
282
- const {opened} = digs(this.state, "opened")
296
+ const {optionsContainerRef} = this.t
297
+ const {opened} = this.s
283
298
 
284
299
  // If options are open and a click is made outside of the options container
285
300
  if (opened && optionsContainerRef.current && !optionsContainerRef.current?.contains(e.target)) {
286
- this.setState({opened: false})
301
+ this.closeOptions()
287
302
  }
288
303
  }
289
304
 
290
305
  optionsContainer() {
291
- const {optionsContainerRef} = digs(this, "endOfSelectRef", "optionsContainerRef")
292
- const {
293
- loadedOptions,
294
- optionsLeft,
295
- optionsTop,
296
- optionsWidth,
297
- scrollLeft,
298
- scrollTop
299
- } = digs(
300
- this.state,
301
- "loadedOptions",
302
- "currentOptions",
303
- "opened",
304
- "optionsLeft",
305
- "optionsTop",
306
- "optionsWidth",
307
- "scrollLeft",
308
- "scrollTop"
309
- )
306
+ const {optionsContainerRef} = this.t
307
+ const {loadedOptions, optionsLeft, optionsTop, optionsWidth, scrollLeft, scrollTop} = this.s
310
308
 
311
309
  return (
312
310
  <div
@@ -340,7 +338,7 @@ export default class HayaSelect extends React.PureComponent {
340
338
  e.stopPropagation()
341
339
 
342
340
  const {onChange, toggleOptions} = this.props
343
- const {multiple} = digs(this.props, "multiple")
341
+ const {multiple} = this.p
344
342
 
345
343
  this.setState(
346
344
  (prevState) => {
@@ -402,9 +400,21 @@ export default class HayaSelect extends React.PureComponent {
402
400
  if (!multiple) this.closeOptions()
403
401
  }
404
402
 
403
+ iconForOption(option) {
404
+ const {toggleOptions} = this.props || {}
405
+ const {toggled} = this.s
406
+
407
+ if (toggleOptions && (option.value in toggled)) {
408
+ const icon = toggleOptions[toggled[option.value]].icon
409
+
410
+ return icon
411
+ }
412
+ }
413
+
405
414
  presentOption = (currentValue) => {
406
415
  const {toggleOptions} = this.props || {}
407
- const {toggled} = digs(this.state, "toggled")
416
+ const {toggled} = this.s
417
+ const icon = this.iconForOption(currentValue)
408
418
 
409
419
  return (
410
420
  <div
@@ -417,7 +427,7 @@ export default class HayaSelect extends React.PureComponent {
417
427
  <i className="fa fa-fw" />
418
428
  }
419
429
  {toggleOptions && (currentValue.value in toggled) &&
420
- <i className={`fa fa-fw fa-${toggleOptions[toggled[currentValue.value]].icon}`} />
430
+ <i className={`fa fa-fw fa-${icon}`} />
421
431
  }
422
432
  {currentValue.text}
423
433
  {("html" in currentValue) &&
@@ -5,11 +5,14 @@ import React from "react"
5
5
  export default class Option extends React.PureComponent {
6
6
  static propTypes = {
7
7
  currentOptions: PropTypes.array.isRequired,
8
+ icon: PropTypes.string,
8
9
  onOptionClicked: PropTypes.func.isRequired,
9
10
  option: PropTypes.object.isRequired,
10
11
  presentOption: PropTypes.func.isRequired
11
12
  }
12
13
 
14
+ style = {cursor: "pointer"}
15
+
13
16
  render() {
14
17
  const {currentOptions, option} = digs(this.props, "currentOptions", "option")
15
18
  const selected = Boolean(currentOptions.find((currentOption) => currentOption.value == option.value))
@@ -20,7 +23,7 @@ export default class Option extends React.PureComponent {
20
23
  data-selected={selected}
21
24
  data-value={option.value}
22
25
  onClick={this.onClick}
23
- style={{cursor: "pointer"}}
26
+ style={this.style}
24
27
  >
25
28
  {this.props.presentOption(option)}
26
29
  </div>
@@ -1,10 +1,13 @@
1
1
  .haya-select {
2
2
  .haya-select-current-selected {
3
3
  display: flex;
4
+ flex-wrap: wrap;
4
5
  background-color: #fff;
5
6
  border: 1px solid #999;
6
7
  cursor: pointer;
7
- padding: 5px;
8
+ padding-top: 5px;
9
+ padding-bottom: 5px;
10
+ padding-left: 5px;
8
11
 
9
12
  &[data-opened="false"] {
10
13
  border-radius: 4px;
@@ -14,8 +17,8 @@
14
17
  border-radius: 4px 4px 0 0;
15
18
  }
16
19
 
17
- .current-option + .current-option {
18
- margin-left: 6px;
20
+ .current-option {
21
+ margin-right: 6px;
19
22
  }
20
23
  }
21
24
  }