@loadsmart/loadsmart-ui 5.13.1 → 5.13.2

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": "@loadsmart/loadsmart-ui",
3
- "version": "5.13.1",
3
+ "version": "5.13.2",
4
4
  "description": "Miranda UI, a React UI library",
5
5
  "main": "dist",
6
6
  "files": [
@@ -1,4 +1,4 @@
1
- import { getDisplayValue, getValue } from './useSelect.helpers'
1
+ import { getDisplayValue, getValue, toSelection } from './useSelect.helpers'
2
2
  import generator from '../../tests/generator'
3
3
 
4
4
  import type { SelectAdapter, SelectValue, Option } from './Select.types'
@@ -60,6 +60,15 @@ describe('useSelect helpers', () => {
60
60
 
61
61
  expect(getDisplayValue(adapters, selection, multiple)).toEqual(adapter.getLabel(item))
62
62
  })
63
+
64
+ it('returns empty string as display value when selection label is undefined', () => {
65
+ const selection: SelectValue = new Map().set(undefined, {
66
+ label: undefined,
67
+ value: undefined,
68
+ })
69
+
70
+ expect(getDisplayValue(adapters, selection, multiple)).toEqual('')
71
+ })
63
72
  })
64
73
 
65
74
  describe('multiple selection', () => {
@@ -118,5 +127,58 @@ describe('useSelect helpers', () => {
118
127
 
119
128
  expect(getDisplayValue(adapters, selection, multiple)).toEqual('')
120
129
  })
130
+
131
+ it('returns empty string as display value when selection label is undefined', () => {
132
+ const selection: SelectValue = new Map().set(undefined, {
133
+ label: undefined,
134
+ value: undefined,
135
+ })
136
+
137
+ expect(getDisplayValue(adapters, selection, multiple)).toEqual('')
138
+ })
139
+ })
140
+
141
+ describe('getSelection', () => {
142
+ it('initializes correctly when a null value is provided', () => {
143
+ expect(toSelection(null)).toEqual([])
144
+ })
145
+
146
+ it('initializes correctly when an undefined value is provided', () => {
147
+ expect(toSelection(undefined)).toEqual([])
148
+ })
149
+
150
+ it('initializes correctly when a valid value is provided', () => {
151
+ expect(
152
+ toSelection({
153
+ value: 'some-value',
154
+ label: 'Some label',
155
+ } as Option)
156
+ ).toEqual([
157
+ {
158
+ value: 'some-value',
159
+ label: 'Some label',
160
+ },
161
+ ])
162
+
163
+ expect(
164
+ toSelection([
165
+ {
166
+ value: 'some-value-1',
167
+ label: 'Some label 1',
168
+ } as Option,
169
+ ])
170
+ ).toEqual([
171
+ {
172
+ value: 'some-value-1',
173
+ label: 'Some label 1',
174
+ },
175
+ ])
176
+ })
177
+
178
+ it('initializes correctly when an empty value is provided', () => {
179
+ expect(toSelection([])).toEqual([])
180
+ expect(toSelection({})).toEqual([])
181
+ expect(toSelection([{}])).toEqual([])
182
+ })
121
183
  })
122
184
  })
@@ -1,8 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { GenericAdapter } from './Select.constants'
3
3
  import isEmpty from 'utils/toolset/isEmpty'
4
+ import toArray from 'utils/toolset/toArray'
4
5
 
5
- import type { SelectAdapter, SelectValue, Option, Adapters } from './Select.types'
6
+ import type { SelectAdapter, SelectValue, Option, Adapters, SelectProps } from './Select.types'
6
7
 
7
8
  export function getAdapter(adapters: Adapters, type?: string): SelectAdapter<any> {
8
9
  if (type == null) {
@@ -32,6 +33,12 @@ export function getValue(selection: SelectValue, multiple?: boolean): Option | O
32
33
  }
33
34
  }
34
35
 
36
+ export function toSelection(value: SelectProps['value']): Option[] {
37
+ const safeValue = toArray(value || [])
38
+
39
+ return safeValue.filter((option) => !isEmpty(option))
40
+ }
41
+
35
42
  export function getDisplayValue(
36
43
  adapters: Adapters,
37
44
  selection: SelectValue,
@@ -46,7 +53,7 @@ export function getDisplayValue(
46
53
  } else {
47
54
  const value = getValue(selection, multiple)
48
55
  const adapter = getAdapter(adapters, (value as Option)._type)
49
- return adapter.getLabel(value)
56
+ return adapter.getLabel(value) ?? ''
50
57
  }
51
58
  }
52
59
 
@@ -0,0 +1,207 @@
1
+ import { renderHook } from '@testing-library/react-hooks'
2
+
3
+ import useSelect from './useSelect'
4
+ import type { SelectProps, Option } from './Select.types'
5
+
6
+ describe('useSelect', () => {
7
+ describe('single selection', () => {
8
+ it('initializes correctly when no value is provided', () => {
9
+ const props: SelectProps = {
10
+ multiple: false,
11
+ name: 'anything',
12
+ }
13
+
14
+ const { result } = renderHook(() => useSelect(props))
15
+ const expectedSelected = new Map()
16
+
17
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
18
+ expect(result.current.query).toBe('')
19
+ })
20
+
21
+ it('initializes correctly when a null value is provided', () => {
22
+ const props: SelectProps = {
23
+ multiple: false,
24
+ name: 'anything',
25
+ value: null,
26
+ }
27
+
28
+ const { result } = renderHook(() => useSelect(props))
29
+ const expectedSelected = new Map()
30
+
31
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
32
+ expect(result.current.query).toBe('')
33
+ })
34
+
35
+ it('initializes correctly when an undefined value is provided', () => {
36
+ const props: SelectProps = {
37
+ multiple: false,
38
+ name: 'anything',
39
+ value: undefined,
40
+ }
41
+
42
+ const { result } = renderHook(() => useSelect(props))
43
+ const expectedSelected = new Map()
44
+
45
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
46
+ expect(result.current.query).toBe('')
47
+ })
48
+
49
+ it('initializes correctly when a valid value is provided', () => {
50
+ const props: SelectProps = {
51
+ multiple: false,
52
+ name: 'anything',
53
+ value: {
54
+ value: 'some-value',
55
+ label: 'Some label',
56
+ } as Option,
57
+ }
58
+
59
+ const { result } = renderHook(() => useSelect(props))
60
+ const expectedSelected = new Map().set('some-value', {
61
+ value: 'some-value',
62
+ label: 'Some label',
63
+ })
64
+
65
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
66
+ expect(result.current.query).toBe('Some label')
67
+ })
68
+
69
+ it('initializes correctly when an invalid value is provided', () => {
70
+ const props: SelectProps = {
71
+ multiple: false,
72
+ name: 'anything',
73
+ value: {
74
+ label: undefined,
75
+ value: undefined,
76
+ } as Option,
77
+ }
78
+
79
+ const { result } = renderHook(() => useSelect(props))
80
+ const expectedSelected = new Map().set(undefined, {
81
+ label: undefined,
82
+ value: undefined,
83
+ })
84
+
85
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
86
+ expect(result.current.query).toBe('')
87
+ })
88
+
89
+ it('initializes correctly when an empty value is provided', () => {
90
+ const props: SelectProps = {
91
+ multiple: false,
92
+ name: 'anything',
93
+ value: {},
94
+ }
95
+
96
+ const { result } = renderHook(() => useSelect(props))
97
+ const expectedSelected = new Map()
98
+
99
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
100
+ expect(result.current.query).toBe('')
101
+ })
102
+ })
103
+
104
+ describe('multiple selection', () => {
105
+ it('initializes correctly when no value is provided', () => {
106
+ const props: SelectProps = {
107
+ multiple: true,
108
+ name: 'anything',
109
+ }
110
+
111
+ const { result } = renderHook(() => useSelect(props))
112
+ const expectedSelected = new Map()
113
+
114
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
115
+ expect(result.current.query).toBe('')
116
+ })
117
+
118
+ it('initializes correctly when a null value is provided', () => {
119
+ const props: SelectProps = {
120
+ multiple: true,
121
+ name: 'anything',
122
+ value: null,
123
+ }
124
+
125
+ const { result } = renderHook(() => useSelect(props))
126
+ const expectedSelected = new Map()
127
+
128
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
129
+ expect(result.current.query).toBe('')
130
+ })
131
+
132
+ it('initializes correctly when an undefined value is provided', () => {
133
+ const props: SelectProps = {
134
+ multiple: true,
135
+ name: 'anything',
136
+ value: undefined,
137
+ }
138
+
139
+ const { result } = renderHook(() => useSelect(props))
140
+
141
+ const expectedSelected = new Map()
142
+
143
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
144
+ expect(result.current.query).toBe('')
145
+ })
146
+
147
+ it('initializes correctly when a valid value is provided', () => {
148
+ const props: SelectProps = {
149
+ multiple: true,
150
+ name: 'anything',
151
+ value: [
152
+ {
153
+ value: 'some-value',
154
+ label: 'Some label',
155
+ } as Option,
156
+ ],
157
+ }
158
+
159
+ const { result } = renderHook(() => useSelect(props))
160
+
161
+ const expectedSelected = new Map().set('some-value', {
162
+ value: 'some-value',
163
+ label: 'Some label',
164
+ })
165
+
166
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
167
+ expect(result.current.query).toBe('')
168
+ })
169
+
170
+ it('initializes correctly when an invalid value is provided', () => {
171
+ const props: SelectProps = {
172
+ multiple: true,
173
+ name: 'anything',
174
+ value: [
175
+ {
176
+ label: undefined,
177
+ value: undefined,
178
+ } as Option,
179
+ ],
180
+ }
181
+
182
+ const { result } = renderHook(() => useSelect(props))
183
+ const expectedSelected = new Map().set(undefined, {
184
+ label: undefined,
185
+ value: undefined,
186
+ })
187
+
188
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
189
+ expect(result.current.query).toBe('')
190
+ })
191
+
192
+ it('initializes correctly when an empty value is provided', () => {
193
+ const props: SelectProps = {
194
+ multiple: true,
195
+ name: 'anything',
196
+ value: [{}],
197
+ }
198
+
199
+ const { result } = renderHook(() => useSelect(props))
200
+
201
+ const expectedSelected = new Map()
202
+
203
+ expect(result.current.selectable.selected).toEqual(expectedSelected)
204
+ expect(result.current.query).toBe('')
205
+ })
206
+ })
207
+ })
@@ -13,7 +13,13 @@ import { isThenable } from 'utils/toolset/isThenable'
13
13
  import toArray from 'utils/toolset/toArray'
14
14
  import { GenericAdapter } from './Select.constants'
15
15
  import { useSelectable } from './Select.context'
16
- import { escapeRegExp, getAdapter, getDisplayValue, getValue } from './useSelect.helpers'
16
+ import {
17
+ escapeRegExp,
18
+ getAdapter,
19
+ getDisplayValue,
20
+ getValue,
21
+ toSelection,
22
+ } from './useSelect.helpers'
17
23
 
18
24
  import type { ChangeEvent, FocusEvent } from 'react'
19
25
  import type {
@@ -213,7 +219,7 @@ function useSelect(props: SelectProps): useSelectReturn {
213
219
  const adapters = useMemo<Adapters>(() => extractAdapters(datasources), [datasources])
214
220
 
215
221
  const selectable = useSelectable({
216
- selected: toArray(props.value || []),
222
+ selected: toSelection(props.value),
217
223
  multiple,
218
224
  adapters,
219
225
  onChange: useCallback(