@telus-uds/components-base 3.22.0 → 3.24.0

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/lib/cjs/Button/Button.js +2 -0
  3. package/lib/cjs/Button/ButtonBase.js +10 -5
  4. package/lib/cjs/Button/ButtonDropdown.js +2 -0
  5. package/lib/cjs/Button/ButtonGroup.js +45 -38
  6. package/lib/cjs/Button/propTypes.js +6 -0
  7. package/lib/cjs/Card/CardBase.js +97 -17
  8. package/lib/cjs/Card/PressableCardBase.js +12 -8
  9. package/lib/cjs/Carousel/Carousel.js +52 -19
  10. package/lib/cjs/Carousel/CarouselItem/CarouselItem.js +23 -3
  11. package/lib/cjs/HorizontalScroll/HorizontalScroll.js +5 -2
  12. package/lib/cjs/Icon/Icon.js +11 -11
  13. package/lib/cjs/Icon/IconText.js +0 -1
  14. package/lib/cjs/Listbox/GroupControl.js +44 -44
  15. package/lib/cjs/Listbox/Listbox.js +63 -20
  16. package/lib/cjs/Listbox/ListboxGroup.js +141 -9
  17. package/lib/cjs/Listbox/ListboxOverlay.js +13 -5
  18. package/lib/cjs/Listbox/PressableItem.js +8 -4
  19. package/lib/cjs/Listbox/SecondLevelHeader.js +201 -0
  20. package/lib/cjs/Listbox/dictionary.js +14 -0
  21. package/lib/cjs/Shortcuts/Shortcuts.js +169 -0
  22. package/lib/cjs/Shortcuts/ShortcutsItem.js +280 -0
  23. package/lib/cjs/Shortcuts/index.js +16 -0
  24. package/lib/cjs/TextInput/TextInputBase.js +5 -1
  25. package/lib/cjs/Tooltip/Tooltip.native.js +2 -0
  26. package/lib/cjs/Validator/Validator.js +171 -135
  27. package/lib/cjs/index.js +15 -0
  28. package/lib/esm/Button/Button.js +2 -0
  29. package/lib/esm/Button/ButtonBase.js +10 -5
  30. package/lib/esm/Button/ButtonDropdown.js +2 -0
  31. package/lib/esm/Button/ButtonGroup.js +44 -39
  32. package/lib/esm/Button/propTypes.js +6 -0
  33. package/lib/esm/Card/CardBase.js +97 -17
  34. package/lib/esm/Card/PressableCardBase.js +10 -8
  35. package/lib/esm/Carousel/Carousel.js +52 -19
  36. package/lib/esm/Carousel/CarouselItem/CarouselItem.js +23 -3
  37. package/lib/esm/HorizontalScroll/HorizontalScroll.js +6 -3
  38. package/lib/esm/Icon/Icon.js +11 -11
  39. package/lib/esm/Icon/IconText.js +0 -1
  40. package/lib/esm/Listbox/GroupControl.js +44 -44
  41. package/lib/esm/Listbox/Listbox.js +64 -21
  42. package/lib/esm/Listbox/ListboxGroup.js +143 -11
  43. package/lib/esm/Listbox/ListboxOverlay.js +13 -5
  44. package/lib/esm/Listbox/PressableItem.js +8 -4
  45. package/lib/esm/Listbox/SecondLevelHeader.js +194 -0
  46. package/lib/esm/Listbox/dictionary.js +8 -0
  47. package/lib/esm/Shortcuts/Shortcuts.js +160 -0
  48. package/lib/esm/Shortcuts/ShortcutsItem.js +273 -0
  49. package/lib/esm/Shortcuts/index.js +3 -0
  50. package/lib/esm/TextInput/TextInputBase.js +5 -1
  51. package/lib/esm/Tooltip/Tooltip.native.js +2 -0
  52. package/lib/esm/Validator/Validator.js +171 -135
  53. package/lib/esm/index.js +1 -0
  54. package/lib/package.json +2 -2
  55. package/package.json +2 -2
  56. package/src/Button/Button.jsx +2 -1
  57. package/src/Button/ButtonBase.jsx +18 -12
  58. package/src/Button/ButtonDropdown.jsx +2 -0
  59. package/src/Button/ButtonGroup.jsx +62 -45
  60. package/src/Button/propTypes.js +6 -0
  61. package/src/Card/CardBase.jsx +113 -14
  62. package/src/Card/PressableCardBase.jsx +17 -5
  63. package/src/Carousel/Carousel.jsx +58 -5
  64. package/src/Carousel/CarouselItem/CarouselItem.jsx +31 -3
  65. package/src/HorizontalScroll/HorizontalScroll.jsx +6 -3
  66. package/src/Icon/Icon.jsx +14 -14
  67. package/src/Icon/IconText.jsx +0 -1
  68. package/src/Listbox/GroupControl.jsx +72 -70
  69. package/src/Listbox/Listbox.jsx +67 -11
  70. package/src/Listbox/ListboxGroup.jsx +160 -27
  71. package/src/Listbox/ListboxOverlay.jsx +23 -5
  72. package/src/Listbox/PressableItem.jsx +8 -4
  73. package/src/Listbox/SecondLevelHeader.jsx +182 -0
  74. package/src/Listbox/dictionary.js +8 -0
  75. package/src/Shortcuts/Shortcuts.jsx +174 -0
  76. package/src/Shortcuts/ShortcutsItem.jsx +297 -0
  77. package/src/Shortcuts/index.js +4 -0
  78. package/src/TextInput/TextInputBase.jsx +5 -1
  79. package/src/Tooltip/Tooltip.native.jsx +2 -1
  80. package/src/Validator/Validator.jsx +180 -159
  81. package/src/index.js +1 -0
  82. package/types/Listbox.d.ts +24 -0
  83. package/types/Shortcuts.d.ts +136 -0
  84. package/types/index.d.ts +12 -0
@@ -37,11 +37,10 @@ const Validator = /*#__PURE__*/React.forwardRef((_ref2, ref) => {
37
37
  supportsProps
38
38
  } = selectProps(rest);
39
39
  const strValidation = supportsProps.validation;
40
- const [, setIndividualCodes] = React.useState({});
41
- const [text, setText] = React.useState(value);
42
40
  const validatorsLength = 6;
43
41
  const prefix = 'code';
44
42
  const sufixValidation = 'Validation';
43
+ const [codes, setCodes] = React.useState(() => Array(validatorsLength).fill(''));
45
44
  const [isHover, setIsHover] = React.useState(false);
46
45
  const handleMouseOver = () => {
47
46
  setIsHover(true);
@@ -52,84 +51,87 @@ const Validator = /*#__PURE__*/React.forwardRef((_ref2, ref) => {
52
51
  const themeTokens = useThemeTokens('TextInput', tokens, variant, {
53
52
  hover: isHover
54
53
  });
55
- const [codeReferences, singleCodes] = React.useMemo(() => {
56
- const codes = [];
57
- const valueCodes = {};
58
- Array.from({
54
+
55
+ // Create refs for input elements
56
+ const codeReferences = React.useMemo(() => {
57
+ return Array.from({
59
58
  length: validatorsLength
60
- }, (_, i) => {
61
- codes[prefix + i] = /*#__PURE__*/React.createRef();
62
- valueCodes[prefix + i] = '';
63
- valueCodes[prefix + i + sufixValidation] = '';
64
- return null;
59
+ }, () => /*#__PURE__*/React.createRef());
60
+ }, [validatorsLength]);
61
+
62
+ // Keep onChange and mask in refs to avoid re-creating event listeners
63
+ const onChangeRef = React.useRef(onChange);
64
+ const maskRef = React.useRef(mask);
65
+ React.useEffect(() => {
66
+ onChangeRef.current = onChange;
67
+ maskRef.current = mask;
68
+ }, [onChange, mask]);
69
+
70
+ // Update a single code digit
71
+ const updateCode = React.useCallback((index, digit) => {
72
+ setCodes(prevCodes => {
73
+ const newCodes = [...prevCodes];
74
+ newCodes[index] = digit;
75
+ if (onChangeRef.current) {
76
+ const codeString = newCodes.join('');
77
+ const singleCodesObj = {};
78
+ newCodes.forEach((code, i) => {
79
+ singleCodesObj[prefix + i] = code;
80
+ singleCodesObj[prefix + i + sufixValidation] = code ? 'success' : '';
81
+ });
82
+ onChangeRef.current(codeString, singleCodesObj);
83
+ }
84
+ return newCodes;
65
85
  });
66
- return [codes, valueCodes];
67
- }, [validatorsLength, prefix, sufixValidation]);
68
- const handleSingleCodes = React.useCallback((codeId, val, validation) => {
69
- singleCodes[codeId] = val;
70
- singleCodes[codeId + sufixValidation] = validation;
71
- setIndividualCodes(prev => ({
72
- ...prev,
73
- [codeId]: val
74
- }));
75
- }, [singleCodes, sufixValidation]);
76
- const changeDataMasking = React.useCallback(boxElement => {
77
- let charMasking = '';
78
- const element = boxElement;
79
- if (mask && mask.length === 1) {
80
- charMasking = mask;
81
- } else if (mask && mask.length > 1) {
82
- charMasking = mask.substring(0, 1);
83
- }
84
- if (charMasking && element) {
85
- element.value = charMasking;
86
- }
87
- }, [mask]);
88
- const handleChangeCode = React.useCallback(() => {
89
- const code = Array.from({
90
- length: validatorsLength
91
- }, (_, i) => singleCodes[prefix + i] || '').join('');
92
- if (typeof onChange === 'function') {
93
- onChange(code, singleCodes);
94
- }
95
- }, [validatorsLength, singleCodes, prefix, onChange]);
96
- const handleChangeCodeValues = React.useCallback((event, codeId, nextIndex) => {
97
- const codeElement = codeReferences[codeId]?.current;
86
+ }, [prefix, sufixValidation]);
87
+
88
+ // Handle input change
89
+ const handleInputChange = React.useCallback((index, event) => {
98
90
  const val = event.nativeEvent?.value || event.target?.value;
99
91
 
100
- // Only allow numeric characters and limit to single digit
101
- const numericOnly = val.replace(/\D/g, '').substring(0, 1);
102
- if (codeElement && codeElement.value) {
103
- codeElement.value = numericOnly;
104
- }
105
- handleSingleCodes(codeId, numericOnly, numericOnly ? 'success' : '');
106
- handleChangeCode();
107
- if (nextIndex === validatorsLength) {
108
- codeElement?.blur();
109
- changeDataMasking(codeElement);
92
+ // This prevents the infinite loop where setting element.value triggers another input event
93
+ if (maskRef.current && val === maskRef.current.substring(0, 1)) {
110
94
  return;
111
95
  }
112
- if (numericOnly.length > 0) {
113
- const nextElement = codeReferences[prefix + nextIndex]?.current;
96
+ const numericOnly = val.replace(/\D/g, '').substring(0, 1);
97
+
98
+ // Update state
99
+ updateCode(index, numericOnly);
100
+
101
+ // Update DOM element
102
+ const element = codeReferences[index]?.current;
103
+ if (element) {
104
+ if (maskRef.current && numericOnly) {
105
+ element.value = maskRef.current.substring(0, 1);
106
+ } else {
107
+ element.value = numericOnly;
108
+ }
109
+ }
110
+
111
+ // Move to next field if digit entered
112
+ if (numericOnly && index < validatorsLength - 1) {
113
+ const nextElement = codeReferences[index + 1]?.current;
114
114
  nextElement?.focus();
115
- changeDataMasking(codeElement);
115
+ } else if (index === validatorsLength - 1) {
116
+ element?.blur();
116
117
  }
117
- }, [codeReferences, handleSingleCodes, handleChangeCode, validatorsLength, changeDataMasking, prefix]);
118
- const handleKeyPress = (event, currentIndex, previousIndex) => {
118
+ }, [codeReferences, updateCode, validatorsLength]);
119
+
120
+ // Handle backspace
121
+ const handleKeyPress = React.useCallback((index, event) => {
119
122
  if (!(event.keyCode === 8 || event.code === 'Backspace')) {
120
123
  return;
121
124
  }
122
- if (currentIndex > 0) {
123
- const currentElement = codeReferences[prefix + currentIndex]?.current;
124
- const previousElement = codeReferences[prefix + previousIndex]?.current;
125
- if (currentElement && currentElement.value) {
126
- currentElement.value = '';
127
- }
125
+ const currentElement = codeReferences[index]?.current;
126
+ if (currentElement) {
127
+ currentElement.value = '';
128
+ }
129
+ updateCode(index, '');
130
+ if (index > 0) {
131
+ const previousElement = codeReferences[index - 1]?.current;
128
132
  previousElement?.focus();
129
133
  }
130
- handleSingleCodes(prefix + currentIndex, '', '');
131
- handleChangeCode();
132
- };
134
+ }, [codeReferences, updateCode]);
133
135
  const getCodeComponents = () => {
134
136
  return Array.from({
135
137
  length: validatorsLength
@@ -138,11 +140,14 @@ const Validator = /*#__PURE__*/React.forwardRef((_ref2, ref) => {
138
140
  const codeInputProps = {
139
141
  nativeID: codeId,
140
142
  keyboardType: 'numeric',
141
- ref: codeReferences[codeId] ?? null,
142
- validation: strValidation || singleCodes[codeId + sufixValidation],
143
+ ref: codeReferences[i] ?? null,
144
+ validation: strValidation || (codes[i] ? 'success' : ''),
143
145
  tokens: selectCodeTextInputTokens(themeTokens),
146
+ // Only use secureTextEntry in React Native, web handles mask differently
147
+ secureTextEntry: !!(Platform.OS !== 'web' && mask),
148
+ selectTextOnFocus: Platform.OS !== 'web',
144
149
  onFocus: () => {
145
- const element = codeReferences[codeId]?.current;
150
+ const element = codeReferences[i]?.current;
146
151
  if (Platform.OS === 'web' && element?.select) {
147
152
  return element.select() ?? null;
148
153
  }
@@ -151,11 +156,35 @@ const Validator = /*#__PURE__*/React.forwardRef((_ref2, ref) => {
151
156
  }
152
157
  return null;
153
158
  },
154
- onKeyPress: event => handleKeyPress(event, i, i - 1),
159
+ onKeyPress: event => handleKeyPress(i, event),
155
160
  onMouseOver: handleMouseOver,
156
161
  onMouseOut: handleMouseOut,
157
162
  inactive
158
163
  };
164
+
165
+ // For React Native, use onChangeText and maxLength
166
+ if (Platform.OS !== 'web') {
167
+ codeInputProps.maxLength = 1;
168
+ codeInputProps.value = codes[i];
169
+ codeInputProps.onChange = () => {};
170
+ codeInputProps.onChangeText = text => {
171
+ if (text) {
172
+ updateCode(i, text);
173
+ if (i < validatorsLength - 1) {
174
+ setTimeout(() => {
175
+ codeReferences[i + 1]?.current?.focus();
176
+ }, 50);
177
+ }
178
+ } else {
179
+ updateCode(i, '');
180
+ if (i > 0) {
181
+ setTimeout(() => {
182
+ codeReferences[i - 1]?.current?.focus();
183
+ }, 50);
184
+ }
185
+ }
186
+ };
187
+ }
159
188
  if (!codeInputProps.validation) {
160
189
  delete codeInputProps.validation;
161
190
  }
@@ -167,89 +196,96 @@ const Validator = /*#__PURE__*/React.forwardRef((_ref2, ref) => {
167
196
  }, codeId);
168
197
  });
169
198
  };
199
+
200
+ // Sync external value prop to internal state
170
201
  React.useEffect(() => {
171
- if (Number(value).toString() !== 'NaN') {
172
- setText(value);
202
+ if (value && Number(value).toString() !== 'NaN') {
203
+ const digits = value.split('').slice(0, validatorsLength);
204
+ const newCodes = Array(validatorsLength).fill('');
205
+ digits.forEach((digit, i) => {
206
+ if (/\d/.test(digit)) {
207
+ newCodes[i] = digit;
208
+ }
209
+ });
210
+ setCodes(newCodes);
173
211
  }
174
- }, [value]);
212
+ }, [value, validatorsLength]);
213
+
214
+ // Sync codes state to DOM elements
175
215
  React.useEffect(() => {
176
- Array.from({
177
- length: validatorsLength
178
- }, (_, i) => {
179
- const element = codeReferences[prefix + i]?.current;
216
+ codes.forEach((code, i) => {
217
+ const element = codeReferences[i]?.current;
180
218
  if (element && element.value !== undefined) {
181
- if (mask && text[i]) {
182
- element.value = mask;
219
+ if (mask && code) {
220
+ element.value = mask.substring(0, 1);
183
221
  } else {
184
- element.value = text[i] ?? '';
222
+ element.value = code;
185
223
  }
186
224
  }
187
- handleSingleCodes(prefix + i, text[i] ?? '', text[i] ? 'success' : '');
188
- return null;
189
225
  });
190
- }, [text, mask, validatorsLength, prefix, codeReferences, handleSingleCodes]);
226
+ }, [codes, codeReferences, mask]);
227
+
228
+ // Setup event listeners - only runs once on mount
191
229
  React.useEffect(() => {
192
- const handlePasteCode = event => {
230
+ if (Platform.OS !== 'web') {
231
+ return undefined;
232
+ }
233
+ const handlePaste = event => {
193
234
  event.preventDefault();
194
-
195
- // Clear current state first
196
- setText('');
197
-
198
- // Clear all individual input fields and their state
199
- Array.from({
200
- length: validatorsLength
201
- }, (_, i) => {
202
- const element = codeReferences[prefix + i]?.current;
203
- if (element && element.value !== undefined) {
204
- element.value = '';
205
- }
206
- handleSingleCodes(prefix + i, '', '');
207
- return null;
208
- });
209
235
  const clipBoardText = event.clipboardData.getData('text');
210
-
211
- // Validate that input contains only digits and truncate to 6 characters
212
236
  const numericOnly = clipBoardText.replace(/\D/g, '').substring(0, validatorsLength);
213
- if (numericOnly.length > 0) {
214
- setText(numericOnly);
237
+ const newCodes = Array(validatorsLength).fill('');
238
+ numericOnly.split('').forEach((digit, i) => {
239
+ newCodes[i] = digit;
240
+ });
241
+ setCodes(newCodes);
242
+ if (onChangeRef.current) {
243
+ const singleCodesObj = {};
244
+ newCodes.forEach((code, i) => {
245
+ singleCodesObj[prefix + i] = code;
246
+ singleCodesObj[prefix + i + sufixValidation] = code ? 'success' : '';
247
+ });
248
+ onChangeRef.current(numericOnly, singleCodesObj);
215
249
  }
216
250
  };
217
251
  const handleCopy = event => {
218
- const clipBoardText = Array.from({
219
- length: validatorsLength
220
- }, (_, i) => singleCodes[prefix + i] || '').join('');
221
- event.clipboardData.setData('text/plain', clipBoardText);
222
- event.preventDefault();
252
+ setCodes(currentCodes => {
253
+ const clipBoardText = currentCodes.join('');
254
+ event.clipboardData.setData('text/plain', clipBoardText);
255
+ event.preventDefault();
256
+ return currentCodes;
257
+ });
223
258
  };
224
- if (Platform.OS === 'web') {
225
- Array.from({
226
- length: validatorsLength
227
- }, (_, i) => {
228
- const element = codeReferences[prefix + i]?.current;
229
- if (element && typeof element.addEventListener === 'function') {
230
- element.addEventListener('paste', handlePasteCode);
231
- element.addEventListener('copy', handleCopy);
232
- element.addEventListener('input', event => handleChangeCodeValues(event, prefix + i, i + 1));
259
+
260
+ // Add event listeners to each input
261
+ codeReferences.forEach((inputRef, i) => {
262
+ const element = inputRef?.current;
263
+ if (element && typeof element.addEventListener === 'function') {
264
+ element.addEventListener('paste', handlePaste);
265
+ element.addEventListener('copy', handleCopy);
266
+ element.addEventListener('input', event => handleInputChange(i, event));
267
+ }
268
+ });
269
+
270
+ // Cleanup
271
+ return () => {
272
+ codeReferences.forEach(inputRef => {
273
+ const element = inputRef?.current;
274
+ if (element && typeof element.removeEventListener === 'function') {
275
+ element.removeEventListener('paste', handlePaste);
276
+ element.removeEventListener('copy', handleCopy);
277
+ element.removeEventListener('input', event => handleInputChange(event.target.dataset.index, event));
233
278
  }
234
- return null;
235
279
  });
236
- }
237
- return () => {
238
- if (Platform.OS === 'web') {
239
- Array.from({
240
- length: validatorsLength
241
- }, (_, i) => {
242
- const element = codeReferences[prefix + i]?.current;
243
- if (element && typeof element.removeEventListener === 'function') {
244
- element.removeEventListener('paste', handlePasteCode);
245
- element.removeEventListener('copy', handleCopy);
246
- element.removeEventListener('input', event => handleChangeCodeValues(event, prefix + i, i + 1));
247
- }
248
- return null;
249
- });
250
- }
251
280
  };
252
- }, [validatorsLength, prefix, codeReferences, handleChangeCodeValues, handleSingleCodes, singleCodes]);
281
+ /*
282
+ * codeReferences and handleInputChange are intentionally omitted from dependencies
283
+ * because we want event listeners to be registered ONLY ONCE when the component mounts.
284
+ * codeReferences is stable (created with useMemo) and handleInputChange is stable (useCallback).
285
+ * Including them would cause unnecessary re-registration of listeners on each render.
286
+ */
287
+ // eslint-disable-next-line react-hooks/exhaustive-deps
288
+ }, []);
253
289
  return /*#__PURE__*/_jsx(InputSupports, {
254
290
  ...supportsProps,
255
291
  feedbackProps: {
package/lib/esm/index.js CHANGED
@@ -47,6 +47,7 @@ export { default as RadioCard, RadioCardGroup } from './RadioCard';
47
47
  export { default as Responsive } from './Responsive';
48
48
  export { default as Search } from './Search';
49
49
  export { default as Select } from './Select';
50
+ export { default as Shortcuts, ShortcutsItem } from './Shortcuts';
50
51
  export { default as SideNav } from './SideNav';
51
52
  export { default as Skeleton } from './Skeleton';
52
53
  export { default as SkipLink } from './SkipLink';
package/lib/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "@gorhom/portal": "^1.0.14",
13
13
  "@react-native-picker/picker": "^2.9.0",
14
14
  "@telus-uds/system-constants": "^3.0.0",
15
- "@telus-uds/system-theme-tokens": "^4.15.1",
15
+ "@telus-uds/system-theme-tokens": "^4.17.0",
16
16
  "airbnb-prop-types": "^2.16.0",
17
17
  "css-mediaquery": "^0.1.2",
18
18
  "expo-document-picker": "^13.0.1",
@@ -84,6 +84,6 @@
84
84
  "standard-engine": {
85
85
  "skip": true
86
86
  },
87
- "version": "3.22.0",
87
+ "version": "3.24.0",
88
88
  "types": "types/index.d.ts"
89
89
  }
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "@gorhom/portal": "^1.0.14",
13
13
  "@react-native-picker/picker": "^2.9.0",
14
14
  "@telus-uds/system-constants": "^3.0.0",
15
- "@telus-uds/system-theme-tokens": "^4.15.1",
15
+ "@telus-uds/system-theme-tokens": "^4.17.0",
16
16
  "airbnb-prop-types": "^2.16.0",
17
17
  "css-mediaquery": "^0.1.2",
18
18
  "expo-document-picker": "^13.0.1",
@@ -84,6 +84,6 @@
84
84
  "standard-engine": {
85
85
  "skip": true
86
86
  },
87
- "version": "3.22.0",
87
+ "version": "3.24.0",
88
88
  "types": "types/index.d.ts"
89
89
  }
@@ -11,7 +11,7 @@ import { a11yProps } from '../utils/props'
11
11
  import { useViewport } from '../ViewportProvider'
12
12
 
13
13
  const Button = React.forwardRef(
14
- ({ accessibilityRole = 'button', tokens, variant, ...props }, ref) => {
14
+ ({ accessibilityRole = 'button', tokens, variant, heightFull = true, ...props }, ref) => {
15
15
  const viewport = useViewport()
16
16
  const {
17
17
  themeOptions: { enableMediaQueryStyleSheet }
@@ -29,6 +29,7 @@ const Button = React.forwardRef(
29
29
  <ButtonBase
30
30
  {...props}
31
31
  tokens={getTokens}
32
+ heightFull={heightFull}
32
33
  accessibilityRole={accessibilityRole}
33
34
  ref={ref}
34
35
  viewport={viewport}
@@ -49,21 +49,26 @@ const selectFlexAndWidthStyles = ({ width, flex }) => {
49
49
  return styles
50
50
  }
51
51
 
52
- const selectOuterContainerStyles = ({
52
+ const selectOuterContainerStyles = (
53
+ {
54
+ opacity,
55
+ outerBorderColor,
56
+ outerBorderWidth,
57
+ outerBorderGap,
58
+ borderRadius,
59
+ outerBackgroundColor,
60
+ alignSelf
61
+ },
62
+ heightFull
63
+ ) => ({
64
+ backgroundColor: outerBackgroundColor,
53
65
  opacity,
54
- outerBorderColor,
55
- outerBorderWidth,
56
- outerBorderGap,
57
- borderRadius,
58
- outerBackgroundColor
59
- }) => ({
60
66
  ...Platform.select({
61
67
  native: {
62
- alignSelf: 'flex-start'
63
- }
68
+ alignSelf: alignSelf !== undefined ? alignSelf : 'flex-start'
69
+ },
70
+ web: heightFull ? {} : { alignSelf: 'flex-start' }
64
71
  }),
65
- backgroundColor: outerBackgroundColor,
66
- opacity,
67
72
  ...applyOuterBorder({
68
73
  outerBorderGap,
69
74
  outerBorderWidth,
@@ -219,6 +224,7 @@ const ButtonBase = React.forwardRef(
219
224
  icon,
220
225
  iconPosition = icon ? 'left' : undefined,
221
226
  iconProps,
227
+ heightFull = true,
222
228
  ...rawRest
223
229
  },
224
230
  ref
@@ -284,7 +290,7 @@ const ButtonBase = React.forwardRef(
284
290
  return [
285
291
  staticStyles.row,
286
292
  selectWebOnlyStyles(inactive, themeTokens, systemProps),
287
- selectOuterContainerStyles(themeTokens),
293
+ selectOuterContainerStyles(themeTokens, heightFull),
288
294
  ...(Object.keys(flexAndWidthStyles).length > 0 ? [flexAndWidthStyles] : []),
289
295
  selectOuterSizeStyles(themeTokens)
290
296
  ]
@@ -105,6 +105,7 @@ const ButtonDropdown = React.forwardRef(
105
105
  accessibilityRole = 'radio',
106
106
  description,
107
107
  singleOption,
108
+ heightFull = true,
108
109
  ...props
109
110
  },
110
111
  ref
@@ -151,6 +152,7 @@ const ButtonDropdown = React.forwardRef(
151
152
  {...pressHandlers}
152
153
  onPress={handlePress}
153
154
  tokens={getButtonTokens}
155
+ heightFull={heightFull}
154
156
  inactive={singleOption || inactive}
155
157
  icon={() => null}
156
158
  accessibilityRole={accessibilityRole}
@@ -1,21 +1,20 @@
1
- import React from 'react'
1
+ import React, { useCallback, useMemo } from 'react'
2
2
  import PropTypes from 'prop-types'
3
3
  import ABBPropTypes from 'airbnb-prop-types'
4
- import { Platform } from 'react-native'
4
+ import { Platform, View, StyleSheet } from 'react-native'
5
5
 
6
6
  import ButtonBase from './ButtonBase'
7
- import { StackWrap } from '../StackView'
8
7
  import Fieldset from '../Fieldset'
9
8
  import { useViewport } from '../ViewportProvider'
10
9
  import { useThemeTokens, useThemeTokensCallback } from '../ThemeProvider'
11
10
  import {
11
+ useSpacingScale,
12
12
  a11yProps,
13
13
  containUniqueFields,
14
14
  focusHandlerProps,
15
15
  pressProps,
16
16
  getTokensPropType,
17
17
  selectSystemProps,
18
- selectTokens,
19
18
  useMultipleInputValues,
20
19
  variantProp,
21
20
  viewProps
@@ -30,16 +29,6 @@ const [selectItemProps, selectedItemPropTypes] = selectSystemProps([
30
29
  viewProps
31
30
  ])
32
31
 
33
- const getStackWrapTokens = (variant) => {
34
- return Platform.select({
35
- web: {
36
- justifyContent: variant?.width === 'equal' ? 'space-evenly' : 'flex-start',
37
- width: variant?.width === 'equal' ? '100%' : 'auto'
38
- },
39
- default: {}
40
- })
41
- }
42
-
43
32
  const ButtonGroup = React.forwardRef(
44
33
  (
45
34
  {
@@ -72,25 +61,52 @@ const ButtonGroup = React.forwardRef(
72
61
  ) => {
73
62
  const viewport = useViewport()
74
63
  const themeTokens = useThemeTokens('ButtonGroup', tokens, variant, { viewport })
75
- const themeStackTokens = selectTokens('StackView', themeTokens)
76
- const variantStackTokens = getStackWrapTokens(variant)
77
- const stackTokens = {
78
- ...themeStackTokens,
79
- ...variantStackTokens
80
- }
81
64
  const { direction, space, fieldSpace, borderRadius, backgroundColor, padding, gap } =
82
65
  themeTokens
83
66
 
67
+ const isMobileNonContained =
68
+ Platform.OS !== 'web' && (!variant || variant?.style !== 'contained')
69
+
84
70
  const themeButtonTokensCallback = useThemeTokensCallback('ButtonGroupItem', tokens, variant)
71
+ const gapValue = useSpacingScale(gap || space)
85
72
 
86
- const getButtonTokens = (state) => {
87
- const themeButtonTokens = themeButtonTokensCallback(state)
88
- return {
89
- ...themeButtonTokens,
90
- width: variant?.width === 'equal' ? '100%' : 'auto',
91
- flex: variant?.width === 'equal' ? 1 : undefined
92
- }
93
- }
73
+ const getButtonTokens = useCallback(
74
+ (state) => {
75
+ const themeButtonTokens = themeButtonTokensCallback(state)
76
+
77
+ const shouldUseTransparentBackground =
78
+ isMobileNonContained && !state.selected && !state.pressed && !state.hover && !state.focus
79
+
80
+ return {
81
+ ...themeButtonTokens,
82
+ ...(variant?.width === 'equal' && staticStyles.equalWidth),
83
+ ...(shouldUseTransparentBackground && { backgroundColor: 'transparent' }),
84
+ alignSelf: themeButtonTokens.width ? 'flex-start' : 'center'
85
+ }
86
+ },
87
+ [themeButtonTokensCallback, isMobileNonContained, variant?.width]
88
+ )
89
+
90
+ const fieldsetStyles = useMemo(
91
+ () => ({
92
+ ...staticStyles.fieldsetBase,
93
+ borderRadius,
94
+ backgroundColor: isMobileNonContained ? 'transparent' : backgroundColor || 'transparent',
95
+ padding,
96
+ width: variant?.width === 'equal' ? '100%' : 'auto'
97
+ }),
98
+ [borderRadius, backgroundColor, padding, variant?.width, isMobileNonContained]
99
+ )
100
+
101
+ const viewStyles = useMemo(
102
+ () => ({
103
+ ...staticStyles.viewBase,
104
+ flexDirection: direction === 'column' ? 'column' : 'row',
105
+ gap: gapValue || 0,
106
+ justifyContent: variant?.width === 'equal' ? 'space-evenly' : 'flex-start'
107
+ }),
108
+ [direction, gapValue, variant?.width]
109
+ )
94
110
 
95
111
  const { currentValues, toggleOneValue } = useMultipleInputValues({
96
112
  initialValues,
@@ -130,24 +146,10 @@ const ButtonGroup = React.forwardRef(
130
146
  inactive={inactive}
131
147
  validation={validation}
132
148
  accessibilityRole={accessibilityRole}
133
- style={{
134
- borderRadius,
135
- backgroundColor,
136
- padding,
137
- ...(Platform.OS === 'web'
138
- ? { gap, width: variant?.width === 'equal' ? '100%' : 'auto' }
139
- : { alignSelf: 'flex-start' })
140
- }}
149
+ style={fieldsetStyles}
141
150
  {...selectProps(rest)}
142
151
  >
143
- <StackWrap
144
- accessibilityRole={innerRole}
145
- space={space}
146
- direction={direction}
147
- tokens={stackTokens}
148
- gap={gap}
149
- ref={ref}
150
- >
152
+ <View accessibilityRole={innerRole} style={viewStyles}>
151
153
  {items.map(
152
154
  ({ label, id = label, accessibilityLabel, ref: itemRef, ...itemRest }, index) => {
153
155
  const isSelected = currentValues.includes(id)
@@ -193,7 +195,7 @@ const ButtonGroup = React.forwardRef(
193
195
  )
194
196
  }
195
197
  )}
196
- </StackWrap>
198
+ </View>
197
199
  </Fieldset>
198
200
  )
199
201
  }
@@ -309,4 +311,19 @@ ButtonGroup.propTypes = {
309
311
  copy: PropTypes.oneOf(['en', 'fr'])
310
312
  }
311
313
 
314
+ const staticStyles = StyleSheet.create({
315
+ fieldsetBase: {
316
+ alignSelf: 'flex-start',
317
+ display: 'inline'
318
+ },
319
+ viewBase: {
320
+ flexWrap: 'wrap',
321
+ alignItems: 'center'
322
+ },
323
+ equalWidth: {
324
+ width: '100%',
325
+ flex: 1
326
+ }
327
+ })
328
+
312
329
  export default ButtonGroup