haya-select 1.0.41 → 1.0.43

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.41",
3
+ "version": "1.0.43",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  import {anythingDifferent} from "set-state-compare/src/diff-utils"
2
2
  import config from "../config.js"
3
- import {digg, digs} from "diggerize"
3
+ import {dig, digg, digs} from "diggerize"
4
4
  import debounce from "debounce"
5
5
  import idForComponent from "@kaspernj/api-maker/src/inputs/id-for-component.mjs"
6
6
  import nameForComponent from "@kaspernj/api-maker/src/inputs/name-for-component.mjs"
@@ -44,6 +44,7 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
44
44
  placeholder: PropTypes.node,
45
45
  search: PropTypes.bool.isRequired,
46
46
  selectContainerStyle: PropTypes.object,
47
+ styles: PropTypes.object,
47
48
  toggled: PropTypes.object,
48
49
  toggleOptions: PropTypes.arrayOf(PropTypes.shape({
49
50
  icon: PropTypes.string.isRequired,
@@ -126,7 +127,21 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
126
127
  if ("values" in this.props && typeof this.props.values != "undefined") {
127
128
  if (this.s.loadedOptions || typeof this.props.options == "function" && this.props.values) {
128
129
  if (this.s.loadedOptions) {
129
- return this.p.values.map((value) => this.s.loadedOptions.find((option) => option.value == value))
130
+ const result = this.p.values.map((value) => {
131
+ let foundOption = this.s.loadedOptions.find((option) => option.value == value)
132
+
133
+ if (!foundOption) {
134
+ foundOption = this.s.currentOptions.find((option) => option.value == value)
135
+
136
+ if (!foundOption) {
137
+ throw new Error(`Couldn't find option: ${value} in loadedOptions or state currentOptions`)
138
+ }
139
+ }
140
+
141
+ return foundOption
142
+ })
143
+
144
+ return result
130
145
  } else {
131
146
  this.setCurrentFromGivenValues()
132
147
  }
@@ -140,6 +155,14 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
140
155
  return this.s.currentOptions
141
156
  }
142
157
 
158
+ getCurrentOptionValues() {
159
+ if ("values" in this.props) {
160
+ return this.p.values
161
+ }
162
+
163
+ return this.getCurrentOptions().map((option) => option.value)
164
+ }
165
+
143
166
  componentDidMount() {
144
167
  const {attribute, defaultValue, defaultValues, defaultValuesFromOptions, model, options} = this.props
145
168
 
@@ -166,62 +189,44 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
166
189
 
167
190
  render() {
168
191
  const {currentSelectedRef, endOfSelectRef} = this.tt
169
- const {
170
- attribute,
171
- className,
172
- defaultToggled,
173
- defaultValue,
174
- defaultValues,
175
- id,
176
- model,
177
- multiple,
178
- name,
179
- onChange,
180
- onOptionsClosed,
181
- options,
182
- placeholder,
183
- search,
184
- selectContainerStyle,
185
- toggleOptions,
186
- ...restProps
187
- } = this.props
192
+ const {className, placeholder, toggleOptions} = this.props
188
193
  const {opened, optionsPlacement} = this.s
189
194
  const currentOptions = this.getCurrentOptions()
190
195
  const BodyPortal = config.getBodyPortal()
191
196
 
192
- const selectContainerStyleActual = Object.assign(
193
- {
194
- display: "flex",
195
- flexDirection: "row",
196
- alignItems: "center",
197
- backgroundColor: "#fff",
198
- border: "1px solid #999",
199
- color: "#000",
200
- cursor: "pointer",
201
- paddingTop: 5,
202
- paddingBottom: 5,
203
- paddingLeft: 5
204
- },
205
- selectContainerStyle
206
- )
197
+ const selectContainerStyleActual = this.stylingFor("selectContainer", {
198
+ display: "flex",
199
+ flexDirection: "row",
200
+ alignItems: "center",
201
+ backgroundColor: "#fff",
202
+ border: "1px solid #999",
203
+ borderRadius: 4,
204
+ color: "#000",
205
+ cursor: "pointer",
206
+ paddingTop: 5,
207
+ paddingBottom: 5,
208
+ paddingLeft: 5
209
+ })
207
210
 
208
211
  if (opened) {
209
212
  if (optionsPlacement == "above") {
210
213
  selectContainerStyleActual.borderTopLeftRadius = 0
211
214
  selectContainerStyleActual.borderTopRightRadius = 0
212
- selectContainerStyleActual.borderBottomRightRadius = 4
213
- selectContainerStyleActual.borderBottomLeftRadius = 4
215
+ selectContainerStyleActual.borderBottomRightRadius = selectContainerStyleActual.borderRadius
216
+ selectContainerStyleActual.borderBottomLeftRadius = selectContainerStyleActual.borderRadius
217
+
218
+ delete selectContainerStyleActual.borderRadius
214
219
  } else if (optionsPlacement == "below") {
215
- selectContainerStyleActual.borderTopLeftRadius = 4
216
- selectContainerStyleActual.borderTopRightRadius = 4
220
+ selectContainerStyleActual.borderTopLeftRadius = selectContainerStyleActual.borderRadius
221
+ selectContainerStyleActual.borderTopRightRadius = selectContainerStyleActual.borderRadius
217
222
  selectContainerStyleActual.borderBottomRightRadius = 0
218
223
  selectContainerStyleActual.borderBottomLeftRadius = 0
224
+
225
+ delete selectContainerStyleActual.borderRadius
219
226
  }
220
- } else {
221
- selectContainerStyleActual.borderRadius = 4
222
227
  }
223
228
 
224
- const chevronStyle = {fontSize: 24}
229
+ const chevronStyle = this.stylingFor("chevron", {fontSize: 24})
225
230
 
226
231
  if (opened) {
227
232
  chevronStyle.marginBottom = -9
@@ -231,8 +236,15 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
231
236
 
232
237
  return (
233
238
  <View
234
- dataSet={{class: className, component: "haya-select", id: idForComponent(this), opened, optionsPlacement, toggles: Boolean(toggleOptions)}}
235
- {...restProps}
239
+ dataSet={{
240
+ class: className,
241
+ component: "haya-select",
242
+ id: idForComponent(this),
243
+ opened,
244
+ optionsPlacement,
245
+ toggles: Boolean(toggleOptions)
246
+ }}
247
+ style={this.stylingFor("main")}
236
248
  >
237
249
  {opened &&
238
250
  <BodyPortal>
@@ -247,7 +259,7 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
247
259
  <View
248
260
  dataSet={{class: "current-selected"}}
249
261
  ref={currentSelectedRef}
250
- style={{flexWrap: "wrap"}}
262
+ style={this.stylingFor("currentSelected", {width: "calc(100% - 27px)", flexWrap: "wrap"})}
251
263
  >
252
264
  {opened &&
253
265
  <TextInput
@@ -256,19 +268,19 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
256
268
  onChangeText={this.tt.onChangeSearchText}
257
269
  placeholder={this.t(".search_dot_dot_dot")}
258
270
  ref={this.tt.searchTextInputRef}
259
- style={{
271
+ style={this.stylingFor("searchTextInput", {
260
272
  width: "100%",
261
273
  border: 0,
262
274
  outline: "none",
263
275
  padding: 0
264
- }}
276
+ })}
265
277
  value={this.state.searchText}
266
278
  />
267
279
  }
268
280
  {!opened &&
269
281
  <>
270
282
  {currentOptions.length == 0 &&
271
- <Text style={{color: "grey"}}>
283
+ <Text style={this.stylingFor("nothingSelected", {color: "grey"})}>
272
284
  {placeholder || this.t(".nothing_selected")}
273
285
  </Text>
274
286
  }
@@ -281,10 +293,14 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
281
293
  />
282
294
  }
283
295
  {currentOptions.map((currentOption) =>
284
- <View dataSet={{class: "current-option"}} key={currentOption.key || `current-value-${currentOption.value}`} style={{marginRight: 6}}>
296
+ <View
297
+ dataSet={{class: "current-option"}}
298
+ key={currentOption.key || `current-value-${currentOption.value}`}
299
+ style={this.stylingFor("currentOption", {marginRight: 6})}
300
+ >
285
301
  {currentOption.type == "group" &&
286
- <View style={{fontWeight: "bold"}}>
287
- <Text>
302
+ <View style={this.stylingFor("currentOptionGroup", {fontWeight: "bold"})}>
303
+ <Text style={this.stylingFor("currentOptionGroupText")}>
288
304
  {currentOption.text}
289
305
  </Text>
290
306
  </View>
@@ -309,14 +325,14 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
309
325
  </View>
310
326
  <View
311
327
  className="chevron-container"
312
- style={{
328
+ style={this.stylingFor("chevronContainer", {
313
329
  display: "flex",
314
330
  alignItems: "center",
315
331
  justifyContent: "center",
316
332
  height: "100%",
317
333
  marginRight: 8,
318
334
  marginLeft: "auto"
319
- }}
335
+ })}
320
336
  >
321
337
  <Text style={chevronStyle}>
322
338
  {opened &&
@@ -388,7 +404,7 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
388
404
 
389
405
  return (
390
406
  <Option
391
- currentOptions={this.getCurrentOptions()}
407
+ currentOptionValues={this.getCurrentOptionValues()}
392
408
  icon={this.iconForOption(loadedOption)}
393
409
  key={key}
394
410
  option={loadedOption}
@@ -548,18 +564,20 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
548
564
  <View
549
565
  dataSet={{class: "options-container", id: idForComponent(this)}}
550
566
  ref={optionsContainerRef}
551
- style={{
552
- position: "absolute",
553
- left: optionsLeft + scrollLeft,
554
- top: optionsTop + scrollTop - 1,
555
- zIndex: 99999,
556
- visibility: optionsVisibility,
557
- width: optionsWidth,
558
- backgroundColor: "#fff",
559
- border: "1px solid #999",
560
- maxHeight: 300,
561
- overflowY: "auto"
562
- }}
567
+ style={
568
+ this.stylingFor("optionsContainer", {
569
+ position: "absolute",
570
+ left: optionsLeft + scrollLeft,
571
+ top: optionsTop + scrollTop - 1,
572
+ zIndex: 99999,
573
+ visibility: optionsVisibility,
574
+ width: optionsWidth,
575
+ backgroundColor: "#fff",
576
+ border: "1px solid #999",
577
+ maxHeight: 300,
578
+ overflowY: "auto"
579
+ })
580
+ }
563
581
  >
564
582
  {loadedOptions?.map((loadedOption) =>
565
583
  this.hayaSelectOption({
@@ -640,6 +658,18 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
640
658
  this.setState(newState)
641
659
  }
642
660
 
661
+ stylingFor(stylingName, style = {}) {
662
+ let customStyling = dig(this, "props", "styles", stylingName)
663
+
664
+ if (typeof customStyling == "function") customStyling = customStyling({state: this.state, style})
665
+
666
+ if (customStyling) {
667
+ return Object.assign(style, customStyling)
668
+ }
669
+
670
+ return style
671
+ }
672
+
643
673
  iconForOption(option) {
644
674
  const {toggleOptions} = this.props || {}
645
675
  const toggled = this.getToggled()
@@ -680,7 +710,9 @@ export default memo(shapeComponent(class HayaSelect extends ShapeComponent {
680
710
  }
681
711
  {currentValue.content}
682
712
  {!currentValue.content &&
683
- <Text>{currentValue.text}</Text>
713
+ <Text className="option-presentation-text" style={this.stylingFor("optionPresentationText", {whiteSpace: "nowrap"})}>
714
+ {currentValue.text}
715
+ </Text>
684
716
  }
685
717
  {("html" in currentValue) &&
686
718
  <div dangerouslySetInnerHTML={{__html: digg(currentValue, "html")}} />
@@ -5,7 +5,7 @@ import {View} from "react-native"
5
5
 
6
6
  export default memo(shapeComponent(class Option extends ShapeComponent {
7
7
  static propTypes = {
8
- currentOptions: PropTypes.array.isRequired,
8
+ currentOptionValues: PropTypes.array.isRequired,
9
9
  disabled: PropTypes.bool,
10
10
  icon: PropTypes.string,
11
11
  onOptionClicked: PropTypes.func.isRequired,
@@ -20,8 +20,9 @@ export default memo(shapeComponent(class Option extends ShapeComponent {
20
20
  }
21
21
 
22
22
  render() {
23
- const disabled = Boolean(this.props.option.disabled)
24
- const selected = Boolean(this.props.currentOptions.find((currentOption) => currentOption.value == this.props.option.value))
23
+ const {currentOptionValues, option} = this.p
24
+ const disabled = Boolean(option.disabled)
25
+ const selected = Boolean(currentOptionValues.find((currentOptionValue) => currentOptionValue == option.value))
25
26
  const style = {
26
27
  paddingTop: 4,
27
28
  paddingRight: 8,
@@ -53,12 +54,12 @@ export default memo(shapeComponent(class Option extends ShapeComponent {
53
54
  selected,
54
55
  value: this.props.option.value
55
56
  }}
56
- onClick={this.onClick}
57
- onPointerOver={this.onPointerOver}
58
- onPointerOut={this.onPointerOut}
57
+ onClick={this.tt.onClick}
58
+ onPointerOver={this.tt.onPointerOver}
59
+ onPointerOut={this.tt.onPointerOut}
59
60
  style={style}
60
61
  >
61
- {this.props.presentOption(this.props.option)}
62
+ {this.props.presentOption(option)}
62
63
  </View>
63
64
  )
64
65
  }