haya-select 1.0.14 → 1.0.16

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.14",
3
+ "version": "1.0.16",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -6,12 +6,15 @@ import debounce from "debounce"
6
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
+ import Option from "./option"
10
+ import OptionGroup from "./option-group"
9
11
  import PropTypes from "prop-types"
12
+ import React from "react"
10
13
 
11
14
  const nameForComponentWithMultiple = (component) => {
12
15
  let name = nameForComponent(component)
13
16
 
14
- if (component.props.multiple) name += "[]"
17
+ if (component.props.multiple && name) name += "[]"
15
18
 
16
19
  return name
17
20
  }
@@ -85,9 +88,9 @@ export default class HayaSelect extends React.PureComponent {
85
88
  }
86
89
 
87
90
  componentDidMount() {
88
- const {attribute, defaultValue, defaultValuesFromOptions, model, options} = this.props
91
+ const {attribute, defaultValue, defaultValues, defaultValuesFromOptions, model, options} = this.props
89
92
 
90
- if (((defaultValue || defaultValuesFromOptions) || (attribute && model)) && typeof options == "function") {
93
+ if (((defaultValue || defaultValues || defaultValuesFromOptions) || (attribute && model)) && typeof options == "function") {
91
94
  this.loadDefaultValuesFromOptionsCallback()
92
95
  }
93
96
  }
@@ -117,6 +120,7 @@ export default class HayaSelect extends React.PureComponent {
117
120
  <div
118
121
  className={classNames("haya-select", className)}
119
122
  data-id={idForComponent(this)}
123
+ data-toggles={Boolean(toggleOptions)}
120
124
  {...restProps}
121
125
  >
122
126
  {opened &&
@@ -145,16 +149,25 @@ export default class HayaSelect extends React.PureComponent {
145
149
  </div>
146
150
  }
147
151
  {currentOptions.map((currentOption) =>
148
- <div className="current-option" key={`current-value-${digg(currentOption, "value")}`}>
149
- {nameForComponentWithMultiple(this) &&
150
- <input
151
- id={idForComponent(this)}
152
- name={nameForComponentWithMultiple(this)}
153
- type="hidden"
154
- value={digg(currentOption, "value")}
155
- />
152
+ <div className="current-option" key={currentOption.key || `current-value-${currentOption.value}`}>
153
+ {currentOption.type == "group" &&
154
+ <div style={{fontWeight: "bold"}}>
155
+ {currentOption.text}
156
+ </div>
157
+ }
158
+ {currentOption.type != "group" &&
159
+ <>
160
+ {nameForComponentWithMultiple(this) &&
161
+ <input
162
+ id={idForComponent(this)}
163
+ name={nameForComponentWithMultiple(this)}
164
+ type="hidden"
165
+ value={digg(currentOption, "value")}
166
+ />
167
+ }
168
+ {this.presentOption(currentOption)}
169
+ </>
156
170
  }
157
- {this.presentOption(currentOption)}
158
171
  </div>
159
172
  )}
160
173
  </>
@@ -166,10 +179,11 @@ export default class HayaSelect extends React.PureComponent {
166
179
  }
167
180
 
168
181
  defaultValues () {
169
- const {attribute, defaultValue, defaultValuesFromOptions, model} = this.props
182
+ const {attribute, defaultValue, defaultValues, defaultValuesFromOptions, model} = this.props
170
183
 
171
184
  if (defaultValuesFromOptions) return defaultValuesFromOptions
172
185
  if (defaultValue) return defaultValue
186
+ if (defaultValues) return defaultValues
173
187
 
174
188
  if (attribute && model) {
175
189
  if (!(attribute in model)) throw new Error(`No such attribute on ${model.modelClassData().name}: ${attribute}`)
@@ -207,21 +221,11 @@ export default class HayaSelect extends React.PureComponent {
207
221
  }
208
222
 
209
223
  hayaSelectOption({key, loadedOption}) {
210
- const {currentOptions} = digs(this.state, "currentOptions")
211
- const selected = Boolean(currentOptions.find((currentOption) => currentOption.value == loadedOption.value))
224
+ if (loadedOption.type == "group") {
225
+ return <OptionGroup key={key} option={loadedOption} />
226
+ }
212
227
 
213
- return (
214
- <div
215
- className="haya-select-option"
216
- data-selected={selected}
217
- data-value={digg(loadedOption, "value")}
218
- key={key}
219
- onClick={(e) => this.onOptionClicked(e, loadedOption)}
220
- style={{cursor: "pointer"}}
221
- >
222
- {this.presentOption(loadedOption)}
223
- </div>
224
- )
228
+ return <Option currentOptions={this.state.currentOptions} key={key} option={loadedOption} onOptionClicked={this.onOptionClicked} presentOption={this.presentOption} />
225
229
  }
226
230
 
227
231
  loadOptionsFromArray(options, searchValue) {
@@ -269,9 +273,7 @@ export default class HayaSelect extends React.PureComponent {
269
273
  scrollLeft: document.documentElement.scrollLeft,
270
274
  scrollTop: document.documentElement.scrollTop
271
275
  },
272
- () => {
273
- digg(this, "searchTextInputRef", "current").focus()
274
- }
276
+ () => digg(this, "searchTextInputRef", "current").focus()
275
277
  )
276
278
  }
277
279
 
@@ -320,7 +322,7 @@ export default class HayaSelect extends React.PureComponent {
320
322
  <EventListener event="click" onCalled={this.onWindowClicked} target={window} />
321
323
  {loadedOptions?.map((loadedOption) =>
322
324
  this.hayaSelectOption({
323
- key: `loaded-option-${digg(loadedOption.value)}`,
325
+ key: loadedOption.key || `loaded-option-${loadedOption.value}`,
324
326
  loadedOption
325
327
  })
326
328
  )}
@@ -0,0 +1,16 @@
1
+ import PropTypes from "prop-types"
2
+ import React from "react"
3
+
4
+ export default class OptionGroup extends React.PureComponent {
5
+ static propTypes = {
6
+ option: PropTypes.object.isRequired
7
+ }
8
+
9
+ render() {
10
+ return (
11
+ <div className="haya-select--option-group">
12
+ {this.props.option.text}
13
+ </div>
14
+ )
15
+ }
16
+ }
@@ -0,0 +1,31 @@
1
+ import {digs} from "diggerize"
2
+ import PropTypes from "prop-types"
3
+ import React from "react"
4
+
5
+ export default class Option extends React.PureComponent {
6
+ static propTypes = {
7
+ currentOptions: PropTypes.array.isRequired,
8
+ onOptionClicked: PropTypes.func.isRequired,
9
+ option: PropTypes.object.isRequired,
10
+ presentOption: PropTypes.func.isRequired
11
+ }
12
+
13
+ render() {
14
+ const {currentOptions, option} = digs(this.props, "currentOptions", "option")
15
+ const selected = Boolean(currentOptions.find((currentOption) => currentOption.value == option.value))
16
+
17
+ return (
18
+ <div
19
+ className="haya-select-option"
20
+ data-selected={selected}
21
+ data-value={option.value}
22
+ onClick={this.onClick}
23
+ style={{cursor: "pointer"}}
24
+ >
25
+ {this.props.presentOption(option)}
26
+ </div>
27
+ )
28
+ }
29
+
30
+ onClick = (e) => this.props.onOptionClicked(e, this.props.option)
31
+ }
@@ -1,15 +1,22 @@
1
- .haya-select-current-selected {
2
- background-color: #fff;
3
- border: 1px solid #999;
4
- cursor: pointer;
5
- padding: 5px;
1
+ .haya-select {
2
+ .haya-select-current-selected {
3
+ display: flex;
4
+ background-color: #fff;
5
+ border: 1px solid #999;
6
+ cursor: pointer;
7
+ padding: 5px;
6
8
 
7
- &[data-opened="false"] {
8
- border-radius: 4px;
9
- }
9
+ &[data-opened="false"] {
10
+ border-radius: 4px;
11
+ }
12
+
13
+ &[data-opened="true"] {
14
+ border-radius: 4px 4px 0 0;
15
+ }
10
16
 
11
- &[data-opened="true"] {
12
- border-radius: 4px 4px 0 0;
17
+ .current-option + .current-option {
18
+ margin-left: 6px;
19
+ }
13
20
  }
14
21
  }
15
22
 
@@ -39,4 +46,10 @@
39
46
  background-color: steelblue;
40
47
  }
41
48
  }
49
+
50
+ .haya-select--option-group {
51
+ padding: 4px 8px;
52
+ color: #000;
53
+ font-weight: bold;
54
+ }
42
55
  }