haya-select 1.0.17 → 1.0.19
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 -2
- package/src/select/index.jsx +43 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "haya-select",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"classnames": "^2.3.1",
|
|
13
13
|
"debounce": "^1.2.1",
|
|
14
14
|
"diggerize": "^1.0.4",
|
|
15
|
-
"fetching-object": "^1.0.1"
|
|
15
|
+
"fetching-object": "^1.0.1",
|
|
16
|
+
"set-state-compare": "^1.0.19"
|
|
16
17
|
},
|
|
17
18
|
"peerDependencies": {
|
|
18
19
|
"prop-types": ">= 15.7.2",
|
package/src/select/index.jsx
CHANGED
|
@@ -10,6 +10,7 @@ import Option from "./option"
|
|
|
10
10
|
import OptionGroup from "./option-group"
|
|
11
11
|
import PropTypes from "prop-types"
|
|
12
12
|
import React from "react"
|
|
13
|
+
import {anythingDifferent} from "set-state-compare"
|
|
13
14
|
|
|
14
15
|
const nameForComponentWithMultiple = (component) => {
|
|
15
16
|
let name = nameForComponent(component)
|
|
@@ -21,7 +22,6 @@ const nameForComponentWithMultiple = (component) => {
|
|
|
21
22
|
|
|
22
23
|
export default class HayaSelect extends React.PureComponent {
|
|
23
24
|
static defaultProps = {
|
|
24
|
-
defaultToggled: {},
|
|
25
25
|
multiple: false,
|
|
26
26
|
search: false
|
|
27
27
|
}
|
|
@@ -37,14 +37,17 @@ 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,
|
|
44
|
+
toggled: PropTypes.object,
|
|
43
45
|
toggleOptions: PropTypes.arrayOf(PropTypes.shape({
|
|
44
46
|
icon: PropTypes.string.isRequired,
|
|
45
47
|
label: PropTypes.string.isRequired,
|
|
46
48
|
value: PropTypes.string.isRequired
|
|
47
|
-
}))
|
|
49
|
+
})),
|
|
50
|
+
values: PropTypes.array
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
p = fetchingObject(() => this.props)
|
|
@@ -64,18 +67,19 @@ export default class HayaSelect extends React.PureComponent {
|
|
|
64
67
|
optionsWidth: undefined,
|
|
65
68
|
scrollLeft: undefined,
|
|
66
69
|
scrollTop: undefined,
|
|
67
|
-
toggled:
|
|
70
|
+
toggled: this.defaultToggled()
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
defaultCurrentOptions() {
|
|
71
|
-
const {defaultValue, defaultValues} = this.props
|
|
74
|
+
const {defaultValue, defaultValues, values} = this.props
|
|
72
75
|
const {options} = this.p
|
|
73
76
|
|
|
74
77
|
if (!Array.isArray(options)) return []
|
|
75
78
|
|
|
76
79
|
return options.filter(({value}) =>
|
|
77
80
|
(defaultValue && value == defaultValue) ||
|
|
78
|
-
(defaultValues && defaultValues.includes(value))
|
|
81
|
+
(defaultValues && defaultValues.includes(value)) ||
|
|
82
|
+
(values && values.includes(value))
|
|
79
83
|
)
|
|
80
84
|
}
|
|
81
85
|
|
|
@@ -91,6 +95,10 @@ export default class HayaSelect extends React.PureComponent {
|
|
|
91
95
|
throw new Error(`Unknown type of options: ${typeof options}`)
|
|
92
96
|
}
|
|
93
97
|
|
|
98
|
+
defaultToggled() {
|
|
99
|
+
return this.props.defaultToggled || this.props.toggled || {}
|
|
100
|
+
}
|
|
101
|
+
|
|
94
102
|
componentDidMount() {
|
|
95
103
|
const {attribute, defaultValue, defaultValues, defaultValuesFromOptions, model, options} = this.props
|
|
96
104
|
|
|
@@ -99,6 +107,30 @@ export default class HayaSelect extends React.PureComponent {
|
|
|
99
107
|
}
|
|
100
108
|
}
|
|
101
109
|
|
|
110
|
+
componentDidUpdate() {
|
|
111
|
+
this.setState((prevState) => {
|
|
112
|
+
const stateUpdate = {}
|
|
113
|
+
|
|
114
|
+
if ("values" in this.props) {
|
|
115
|
+
const newCurrentOptions = this.defaultCurrentOptions()
|
|
116
|
+
|
|
117
|
+
if (anythingDifferent(prevState.currentOptions, newCurrentOptions)) {
|
|
118
|
+
stateUpdate.currentOptions = newCurrentOptions
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if ("toggled" in this.props) {
|
|
123
|
+
const newToggled = digg(this, "props", "toggled")
|
|
124
|
+
|
|
125
|
+
if (anythingDifferent(prevState.toggled, newToggled)) {
|
|
126
|
+
stateUpdate.toggled = newToggled
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return stateUpdate
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
102
134
|
render() {
|
|
103
135
|
const {endOfSelectRef} = this.t
|
|
104
136
|
const {
|
|
@@ -111,6 +143,7 @@ export default class HayaSelect extends React.PureComponent {
|
|
|
111
143
|
model,
|
|
112
144
|
multiple,
|
|
113
145
|
onChange,
|
|
146
|
+
onOptionsClosed,
|
|
114
147
|
options,
|
|
115
148
|
placeholder,
|
|
116
149
|
search,
|
|
@@ -266,6 +299,10 @@ export default class HayaSelect extends React.PureComponent {
|
|
|
266
299
|
loadedOptions: undefined,
|
|
267
300
|
opened: false
|
|
268
301
|
})
|
|
302
|
+
|
|
303
|
+
if (this.props.onOptionsClosed) {
|
|
304
|
+
this.props.onOptionsClosed({options: this.state.currentOptions})
|
|
305
|
+
}
|
|
269
306
|
}
|
|
270
307
|
|
|
271
308
|
openOptions() {
|
|
@@ -294,7 +331,7 @@ export default class HayaSelect extends React.PureComponent {
|
|
|
294
331
|
|
|
295
332
|
// If options are open and a click is made outside of the options container
|
|
296
333
|
if (opened && optionsContainerRef.current && !optionsContainerRef.current?.contains(e.target)) {
|
|
297
|
-
this.
|
|
334
|
+
this.closeOptions()
|
|
298
335
|
}
|
|
299
336
|
}
|
|
300
337
|
|