haya-select 1.0.8 → 1.0.11

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.8",
3
+ "version": "1.0.11",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -3,6 +3,7 @@ import classNames from "classnames"
3
3
  import config from "../config.js"
4
4
  import {digg, digs} from "diggerize"
5
5
  import debounce from "debounce"
6
+ import EventListener from "@kaspernj/api-maker/src/event-listener"
6
7
  import {idForComponent, nameForComponent} from "@kaspernj/api-maker-inputs"
7
8
  import PropTypes from "prop-types"
8
9
 
@@ -14,8 +15,9 @@ const nameForComponentWithMultiple = (component) => {
14
15
  return name
15
16
  }
16
17
 
17
- export default class CustomSelect extends React.PureComponent {
18
+ export default class HayaSelect extends React.PureComponent {
18
19
  static defaultProps = {
20
+ defaultToggled: {},
19
21
  multiple: false,
20
22
  search: false
21
23
  }
@@ -23,13 +25,16 @@ export default class CustomSelect extends React.PureComponent {
23
25
  static propTypes = {
24
26
  attribute: PropTypes.string,
25
27
  className: PropTypes.string,
28
+ defaultToggled: PropTypes.object,
26
29
  defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
27
30
  defaultValues: PropTypes.array,
28
31
  defaultValuesFromOptions: PropTypes.array,
32
+ id: PropTypes.node,
29
33
  model: PropTypes.object,
30
34
  multiple: PropTypes.bool.isRequired,
31
35
  onChange: PropTypes.func,
32
36
  options: PropTypes.oneOfType([PropTypes.array, PropTypes.func]).isRequired,
37
+ placeholder: PropTypes.node,
33
38
  search: PropTypes.bool.isRequired,
34
39
  toggleOptions: PropTypes.arrayOf(PropTypes.shape({
35
40
  icon: PropTypes.string.isRequired,
@@ -51,7 +56,7 @@ export default class CustomSelect extends React.PureComponent {
51
56
  optionsWidth: undefined,
52
57
  scrollLeft: undefined,
53
58
  scrollTop: undefined,
54
- toggled: {}
59
+ toggled: digg(this, "props", "defaultToggled")
55
60
  }
56
61
 
57
62
  defaultCurrentOptions() {
@@ -88,7 +93,22 @@ export default class CustomSelect extends React.PureComponent {
88
93
 
89
94
  render() {
90
95
  const {endOfSelectRef} = digs(this, "endOfSelectRef")
91
- const {attribute, className, defaultValue, defaultValues, model, multiple, onChange, options, search, toggleOptions, ...restProps} = this.props
96
+ const {
97
+ attribute,
98
+ className,
99
+ defaultToggled,
100
+ defaultValue,
101
+ defaultValues,
102
+ id,
103
+ model,
104
+ multiple,
105
+ onChange,
106
+ options,
107
+ placeholder,
108
+ search,
109
+ toggleOptions,
110
+ ...restProps
111
+ } = this.props
92
112
  const {currentOptions, opened} = digs(this.state, "currentOptions", "opened")
93
113
  const BodyPortal = config.getBodyPortal()
94
114
 
@@ -120,17 +140,19 @@ export default class CustomSelect extends React.PureComponent {
120
140
  <>
121
141
  {currentOptions.length == 0 &&
122
142
  <div style={{color: "grey"}}>
123
- {I18n.t("haya_select.nothing_selected")}
143
+ {placeholder || I18n.t("haya_select.nothing_selected")}
124
144
  </div>
125
145
  }
126
146
  {currentOptions.map((currentOption) =>
127
147
  <div className="current-option" key={`current-value-${digg(currentOption, "value")}`}>
128
- <input
129
- id={idForComponent(this)}
130
- name={nameForComponentWithMultiple(this)}
131
- type="hidden"
132
- value={digg(currentOption, "value")}
133
- />
148
+ {nameForComponentWithMultiple(this) &&
149
+ <input
150
+ id={idForComponent(this)}
151
+ name={nameForComponentWithMultiple(this)}
152
+ type="hidden"
153
+ value={digg(currentOption, "value")}
154
+ />
155
+ }
134
156
  {this.presentOption(currentOption)}
135
157
  </div>
136
158
  )}
@@ -375,15 +397,19 @@ export default class CustomSelect extends React.PureComponent {
375
397
  )
376
398
 
377
399
  if (!multiple) this.closeOptions()
378
-
379
400
  }
380
401
 
381
402
  presentOption = (currentValue) => {
382
- const {toggleOptions} = this.props
403
+ const {toggleOptions} = this.props || {}
383
404
  const {toggled} = digs(this.state, "toggled")
384
405
 
385
406
  return (
386
- <div>
407
+ <div
408
+ className="haya-select-option-presentation"
409
+ data-toggle-icon={toggleOptions && toggled && toggleOptions[toggled[currentValue.value]]?.icon}
410
+ data-toggle-value={toggleOptions && toggled && toggleOptions[toggled[currentValue.value]]?.value}
411
+ data-value={currentValue.value}
412
+ >
387
413
  {toggleOptions && !(currentValue.value in toggled) &&
388
414
  <i className="fa fa-fw" />
389
415
  }