haya-select 1.0.31 → 1.0.33
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 +3 -3
- package/src/select/index.jsx +48 -42
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "haya-select",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.33",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
"license": "ISC",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"classnames": "^2.3.1",
|
|
13
|
-
"debounce": "^
|
|
13
|
+
"debounce": "^2.1.0",
|
|
14
14
|
"diggerize": "^1.0.4",
|
|
15
15
|
"fetching-object": "^1.0.1",
|
|
16
|
-
"set-state-compare": "^1.0.
|
|
16
|
+
"set-state-compare": "^1.0.45"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
19
|
"prop-types": ">= 15.7.2",
|
package/src/select/index.jsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import "./style"
|
|
2
|
+
import {anythingDifferent} from "set-state-compare/src/diff-utils"
|
|
2
3
|
import classNames from "classnames"
|
|
3
4
|
import config from "../config.js"
|
|
4
5
|
import {digg, digs} from "diggerize"
|
|
5
6
|
import debounce from "debounce"
|
|
6
|
-
import EventListener from "@kaspernj/api-maker/src/event-listener"
|
|
7
7
|
import idForComponent from "@kaspernj/api-maker/src/inputs/id-for-component.mjs"
|
|
8
8
|
import nameForComponent from "@kaspernj/api-maker/src/inputs/name-for-component.mjs"
|
|
9
9
|
import Option from "./option"
|
|
@@ -11,6 +11,7 @@ import OptionGroup from "./option-group"
|
|
|
11
11
|
import PropTypes from "prop-types"
|
|
12
12
|
import {memo, useRef} from "react"
|
|
13
13
|
import {shapeComponent, ShapeComponent} from "set-state-compare/src/shape-component.js"
|
|
14
|
+
import useEventListener from "@kaspernj/api-maker/src/use-event-listener"
|
|
14
15
|
|
|
15
16
|
const nameForComponentWithMultiple = (component) => {
|
|
16
17
|
let name = nameForComponent(component)
|
|
@@ -77,6 +78,9 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
77
78
|
scrollTop: undefined,
|
|
78
79
|
toggled: () => this.defaultToggled()
|
|
79
80
|
})
|
|
81
|
+
useEventListener(window, "click", this.onWindowClicked)
|
|
82
|
+
useEventListener(window, "resize", this.tt.onAnythingResizedDebounced)
|
|
83
|
+
useEventListener(window, "scroll", this.tt.onAnythingScrolledDebounced)
|
|
80
84
|
}
|
|
81
85
|
|
|
82
86
|
defaultCurrentOptions() {
|
|
@@ -104,8 +108,21 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
104
108
|
throw new Error(`Unknown type of options: ${typeof options}`)
|
|
105
109
|
}
|
|
106
110
|
|
|
107
|
-
defaultToggled() {
|
|
108
|
-
|
|
111
|
+
defaultToggled = () => ("toggled" in this.props) ? this.p.toggled : this.props.defaultToggled || {}
|
|
112
|
+
getToggled = () => ("toggled" in this.props) ? this.p.toggled : this.s.toggled
|
|
113
|
+
getValues = () => ("values" in this.props) ? this.p.values : this.s.currentOptions.map((currentOption) => currentOption.value)
|
|
114
|
+
getCurrentOptions = () => {
|
|
115
|
+
if ("values" in this.props) {
|
|
116
|
+
if (this.s.loadedOptions) {
|
|
117
|
+
return this.p.values.map((value) => this.s.loadedOptions.find((option) => option.value == value))
|
|
118
|
+
} else if (this.props.options) {
|
|
119
|
+
return this.p.values.map((value) => this.p.options.find((option) => option.value == value))
|
|
120
|
+
} else {
|
|
121
|
+
return this.p.values.map((value) => ({value}))
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return this.s.currentOptions
|
|
109
126
|
}
|
|
110
127
|
|
|
111
128
|
componentDidMount() {
|
|
@@ -114,9 +131,6 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
114
131
|
if (((defaultValue || defaultValues || defaultValuesFromOptions) || (attribute && model)) && typeof options == "function") {
|
|
115
132
|
this.loadDefaultValuesFromOptionsCallback()
|
|
116
133
|
}
|
|
117
|
-
|
|
118
|
-
window.addEventListener("resize", this.tt.onAnythingResizedDebounced, true)
|
|
119
|
-
window.addEventListener("scroll", this.tt.onAnythingScrolledDebounced, true)
|
|
120
134
|
}
|
|
121
135
|
|
|
122
136
|
componentDidUpdate() {
|
|
@@ -138,12 +152,9 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
138
152
|
}
|
|
139
153
|
}
|
|
140
154
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
componentWillUnmount() {
|
|
145
|
-
window.removeEventListener("resize", this.tt.onAnythingResizedDebounced)
|
|
146
|
-
window.removeEventListener("scroll", this.tt.onAnythingScrolledDebounced)
|
|
155
|
+
if (Object.keys(newState).length > 0) {
|
|
156
|
+
this.setState(newState)
|
|
157
|
+
}
|
|
147
158
|
}
|
|
148
159
|
|
|
149
160
|
render() {
|
|
@@ -166,7 +177,8 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
166
177
|
toggleOptions,
|
|
167
178
|
...restProps
|
|
168
179
|
} = this.props
|
|
169
|
-
const {
|
|
180
|
+
const {opened, optionsPlacement} = this.s
|
|
181
|
+
const currentOptions = this.getCurrentOptions()
|
|
170
182
|
const BodyPortal = config.getBodyPortal()
|
|
171
183
|
|
|
172
184
|
return (
|
|
@@ -184,10 +196,7 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
184
196
|
</BodyPortal>
|
|
185
197
|
}
|
|
186
198
|
<div className="haya-select-container" onClick={onSelectClicked}>
|
|
187
|
-
<div
|
|
188
|
-
className="haya-select-current-selected"
|
|
189
|
-
ref={currentSelectedRef}
|
|
190
|
-
>
|
|
199
|
+
<div className="haya-select-current-selected" ref={currentSelectedRef}>
|
|
191
200
|
{opened &&
|
|
192
201
|
<input
|
|
193
202
|
className="haya-select-search-text-input"
|
|
@@ -300,7 +309,7 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
300
309
|
}
|
|
301
310
|
|
|
302
311
|
return <Option
|
|
303
|
-
currentOptions={this.
|
|
312
|
+
currentOptions={this.getCurrentOptions()}
|
|
304
313
|
icon={this.iconForOption(loadedOption)}
|
|
305
314
|
key={key}
|
|
306
315
|
option={loadedOption}
|
|
@@ -311,7 +320,7 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
311
320
|
|
|
312
321
|
loadOptionsFromArray(options, searchValue) {
|
|
313
322
|
const lowerSearchValue = searchValue?.toLowerCase()
|
|
314
|
-
const loadedOptions = options.filter(({text}) => !lowerSearchValue || text
|
|
323
|
+
const loadedOptions = options.filter(({text}) => !lowerSearchValue || text?.toLowerCase()?.includes(lowerSearchValue))
|
|
315
324
|
|
|
316
325
|
this.setState({loadedOptions})
|
|
317
326
|
}
|
|
@@ -338,7 +347,7 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
338
347
|
})
|
|
339
348
|
|
|
340
349
|
if (this.props.onOptionsClosed) {
|
|
341
|
-
this.props.onOptionsClosed({options: this.
|
|
350
|
+
this.props.onOptionsClosed({options: this.getCurrentOptions()})
|
|
342
351
|
}
|
|
343
352
|
}
|
|
344
353
|
|
|
@@ -463,7 +472,6 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
463
472
|
width: optionsWidth,
|
|
464
473
|
}}
|
|
465
474
|
>
|
|
466
|
-
<EventListener event="click" onCalled={this.onWindowClicked} target={window} />
|
|
467
475
|
{loadedOptions?.map((loadedOption) =>
|
|
468
476
|
this.hayaSelectOption({
|
|
469
477
|
key: loadedOption.key || `loaded-option-${loadedOption.value}`,
|
|
@@ -485,25 +493,30 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
485
493
|
|
|
486
494
|
const {onChange, toggleOptions} = this.props
|
|
487
495
|
const {multiple} = this.p
|
|
488
|
-
const
|
|
496
|
+
const currentOptions = this.getCurrentOptions()
|
|
497
|
+
const toggled = this.getToggled()
|
|
489
498
|
const newState = {}
|
|
490
499
|
const existingOption = currentOptions.find((currentOption) => currentOption.value == loadedOption.value)
|
|
491
500
|
const newToggled = {...toggled}
|
|
492
501
|
|
|
493
502
|
if (existingOption) {
|
|
494
503
|
if (toggleOptions) {
|
|
495
|
-
const
|
|
504
|
+
const currentToggle = toggled[loadedOption.value]
|
|
505
|
+
const currentIndex = toggleOptions.findIndex((element) => element.value == currentToggle)
|
|
496
506
|
|
|
497
507
|
if (currentIndex >= (toggleOptions.length - 1)) {
|
|
508
|
+
// No next toggled - remove toggled and option
|
|
498
509
|
delete newToggled[loadedOption.value]
|
|
499
510
|
|
|
500
511
|
newState.currentOptions = currentOptions.filter((currentOption) => currentOption.value != loadedOption.value)
|
|
501
512
|
} else {
|
|
502
|
-
|
|
513
|
+
// Already toggled - set to next toggle
|
|
514
|
+
newToggled[loadedOption.value] = digg(toggleOptions, currentIndex + 1, "value")
|
|
503
515
|
}
|
|
504
516
|
|
|
505
517
|
newState.toggled = newToggled
|
|
506
518
|
} else {
|
|
519
|
+
// Remove from current options
|
|
507
520
|
newState.currentOptions = currentOptions.filter((currentOption) => currentOption.value != loadedOption.value)
|
|
508
521
|
}
|
|
509
522
|
} else {
|
|
@@ -511,7 +524,9 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
511
524
|
if (loadedOption.disabled) return
|
|
512
525
|
|
|
513
526
|
if (toggleOptions) {
|
|
514
|
-
|
|
527
|
+
// Set fresh toggle
|
|
528
|
+
|
|
529
|
+
newToggled[loadedOption.value] = toggleOptions[0].value
|
|
515
530
|
newState.toggled = newToggled
|
|
516
531
|
}
|
|
517
532
|
|
|
@@ -522,42 +537,33 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
|
|
|
522
537
|
}
|
|
523
538
|
}
|
|
524
539
|
|
|
525
|
-
this.setState(newState)
|
|
526
|
-
|
|
527
540
|
if (onChange) {
|
|
528
|
-
const toggles = {}
|
|
529
|
-
|
|
530
|
-
for(const toggleKey in this.state.toggled) {
|
|
531
|
-
const toggleValue = digg(this.s.toggled, toggleKey)
|
|
532
|
-
const toggleOption = digg(this.p.toggleOptions, toggleValue)
|
|
533
|
-
|
|
534
|
-
toggles[toggleKey] = toggleOption
|
|
535
|
-
}
|
|
536
|
-
|
|
537
541
|
onChange({
|
|
538
542
|
event,
|
|
539
|
-
options:
|
|
540
|
-
toggles
|
|
543
|
+
options: newState.currentOptions || currentOptions,
|
|
544
|
+
toggles: newToggled
|
|
541
545
|
})
|
|
542
546
|
}
|
|
543
547
|
|
|
544
548
|
if (!multiple) this.closeOptions()
|
|
549
|
+
this.setState(newState)
|
|
545
550
|
}
|
|
546
551
|
|
|
547
552
|
iconForOption(option) {
|
|
548
553
|
const {toggleOptions} = this.props || {}
|
|
549
|
-
const
|
|
554
|
+
const toggled = this.getToggled()
|
|
550
555
|
|
|
551
556
|
if (toggleOptions && (option.value in toggled)) {
|
|
552
|
-
const
|
|
557
|
+
const toggledValue = toggled[option.value]
|
|
558
|
+
const toggledOption = toggleOptions.find((element) => element.value == toggledValue)
|
|
553
559
|
|
|
554
|
-
return icon
|
|
560
|
+
return toggledOption.icon
|
|
555
561
|
}
|
|
556
562
|
}
|
|
557
563
|
|
|
558
564
|
presentOption = (currentValue) => {
|
|
559
565
|
const {toggleOptions} = this.props || {}
|
|
560
|
-
const
|
|
566
|
+
const toggled = this.getToggled()
|
|
561
567
|
const icon = this.iconForOption(currentValue)
|
|
562
568
|
|
|
563
569
|
return (
|