@veracity/vui 1.3.1-rc.0 → 1.4.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.
- package/dist/cjs/select/select.d.ts.map +1 -1
- package/dist/cjs/select/select.js +7 -6
- package/dist/cjs/select/select.types.d.ts +4 -1
- package/dist/cjs/select/select.types.d.ts.map +1 -1
- package/dist/cjs/select/useSelect.d.ts.map +1 -1
- package/dist/cjs/select/useSelect.js +12 -3
- package/dist/cjs/select/useSelect.types.d.ts +4 -1
- package/dist/cjs/select/useSelect.types.d.ts.map +1 -1
- package/dist/esm/select/select.d.ts.map +1 -1
- package/dist/esm/select/select.js +7 -6
- package/dist/esm/select/select.types.d.ts +4 -1
- package/dist/esm/select/select.types.d.ts.map +1 -1
- package/dist/esm/select/useSelect.d.ts.map +1 -1
- package/dist/esm/select/useSelect.js +13 -4
- package/dist/esm/select/useSelect.types.d.ts +4 -1
- package/dist/esm/select/useSelect.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/select/select.tsx +10 -13
- package/src/select/select.types.ts +5 -1
- package/src/select/useSelect.tsx +14 -4
- package/src/select/useSelect.types.ts +5 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../../src/select/select.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAO5C;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../../src/select/select.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAO5C;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,WAAW,eAoDxC;yBApDe,MAAM;;;;;;;;;AA2DtB,eAAe,MAAM,CAAA"}
|
|
@@ -30,8 +30,9 @@ const useSelect_1 = __importDefault(require("./useSelect"));
|
|
|
30
30
|
* Supports single select and multi select, as well as controlled and uncontrolled modes.
|
|
31
31
|
*/
|
|
32
32
|
function Select(props) {
|
|
33
|
-
|
|
34
|
-
const
|
|
33
|
+
var _a;
|
|
34
|
+
const { children, defaultValue, disabled, isInvalid, isMultiple, name, onChange, options, placeholder = 'Please select', readOnly, selectButton, size, value, variant } = props, rest = __rest(props, ["children", "defaultValue", "disabled", "isInvalid", "isMultiple", "name", "onChange", "options", "placeholder", "readOnly", "selectButton", "size", "value", "variant"]);
|
|
35
|
+
const selectProps = (0, useSelect_1.default)({ defaultValue, isMultiple, onChange, value, options, children });
|
|
35
36
|
if (utils_1.__DEV__ && isMultiple && !Array.isArray(selectProps.value)) {
|
|
36
37
|
console.error('<Select /> is used with isMultiple but its value is not an array: ', value);
|
|
37
38
|
}
|
|
@@ -45,10 +46,10 @@ function Select(props) {
|
|
|
45
46
|
size,
|
|
46
47
|
variant }, selectProps);
|
|
47
48
|
return (react_1.default.createElement(context_1.SelectProvider, { value: context },
|
|
48
|
-
react_1.default.createElement(popover_1.Popover, Object.assign({ matchWidth: true }, rest),
|
|
49
|
-
react_1.default.createElement(selectButton_1.default, null),
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
react_1.default.createElement(popover_1.Popover, Object.assign({ matchWidth: true }, rest),
|
|
50
|
+
react_1.default.createElement(react_1.default.Fragment, null, selectButton !== null && selectButton !== void 0 ? selectButton : react_1.default.createElement(selectButton_1.default, null),
|
|
51
|
+
react_1.default.createElement(selectContent_1.default, null,
|
|
52
|
+
react_1.default.createElement(selectGroup_1.default, null, children !== null && children !== void 0 ? children : (_a = options === null || options === void 0 ? void 0 : options.map) === null || _a === void 0 ? void 0 : _a.call(options, option => react_1.default.createElement(selectOption_1.default, Object.assign({ key: option.value }, option)))))))));
|
|
52
53
|
}
|
|
53
54
|
exports.Select = Select;
|
|
54
55
|
Select.Button = selectButton_1.default;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
1
2
|
import { ButtonProps } from '../button';
|
|
2
3
|
import { ListItemProps, ListProps } from '../list';
|
|
3
4
|
import { PopoverContentProps, PopoverProps } from '../popover';
|
|
@@ -16,11 +17,13 @@ export declare type SelectProps = ThemingProps<'Select'> & PopoverProps & UseSel
|
|
|
16
17
|
/** Name of the input. Used in custom change event to support form library integration. */
|
|
17
18
|
name?: string;
|
|
18
19
|
/** Data prop to display an array of options in the popover. */
|
|
19
|
-
options
|
|
20
|
+
options?: SelectOptionData[];
|
|
20
21
|
/** Displayed inside the trigger when no value is selected. */
|
|
21
22
|
placeholder?: string;
|
|
22
23
|
/** Select cannot be opened and is styled accordingly. */
|
|
23
24
|
readOnly?: boolean;
|
|
25
|
+
/** Custom select button slot. */
|
|
26
|
+
selectButton?: ReactNode;
|
|
24
27
|
};
|
|
25
28
|
export declare type SelectOptionData = {
|
|
26
29
|
/** Option is displayed as disabled. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.types.d.ts","sourceRoot":"","sources":["../../../src/select/select.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAElD,oBAAY,iBAAiB,GAAG,WAAW,CAAA;AAE3C,oBAAY,kBAAkB,GAAG,mBAAmB,CAAA;AAEpD,oBAAY,aAAa,GAAG,IAAI,CAC9B,WAAW,EACX,UAAU,GAAG,WAAW,GAAG,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,aAAa,GAAG,UAAU,GAAG,MAAM,GAAG,S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AE,OAAO,CAAA;IAClB,+DAA+D;IAC/D,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,0FAA0F;IAC1F,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,+DAA+D;IAC/D,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC5B,8DAA8D;IAC9D,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,yDAAyD;IACzD,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,iCAAiC;IACjC,YAAY,CAAC,EAAE,SAAS,CAAA;CACzB,CAAA;AAEH,oBAAY,gBAAgB,GAAG;IAC7B,uCAAuC;IACvC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,gCAAgC;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,+BAA+B;IAC/B,KAAK,EAAE,WAAW,CAAA;CACnB,CAAA;AAED,oBAAY,iBAAiB,GAAG,aAAa,GAAG;IAC9C,mGAAmG;IACnG,KAAK,CAAC,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,oBAAY,WAAW,GAAG,MAAM,GAAG,MAAM,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAA8B,MAAM,UAAU,CAAA;AAClE,OAAO,EAAoB,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAQlD,wFAAwF;AACxF,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,KAAK,EAAE,cAAc;
|
|
1
|
+
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAA8B,MAAM,UAAU,CAAA;AAClE,OAAO,EAAoB,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAQlD,wFAAwF;AACxF,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,KAAK,EAAE,cAAc;kBAiBjB,WAAW;;;EAiBhD;AAED,oBAAY,mBAAmB,GAAG,UAAU,CAAC,OAAO,SAAS,CAAC,CAAA"}
|
|
@@ -8,11 +8,20 @@ const getValueText = (value, options) => {
|
|
|
8
8
|
};
|
|
9
9
|
/** Handles controlled/uncontrolled value state. Supports single and multiple values. */
|
|
10
10
|
function useSelect(props) {
|
|
11
|
-
const { defaultValue, isMultiple, onChange: onChangeProp, value: valueProp, options } = props;
|
|
11
|
+
const { defaultValue, isMultiple, onChange: onChangeProp, value: valueProp, options = [], children } = props;
|
|
12
12
|
// Not using useControlled here, because we need custom value setter when uncontrolled
|
|
13
13
|
const [stateValue, setStateValue] = (0, react_1.useState)(defaultValue);
|
|
14
14
|
const isControlled = valueProp !== undefined;
|
|
15
15
|
const value = isControlled ? valueProp : stateValue;
|
|
16
|
+
// Iterating over the children to dynamically obtain the list of available options
|
|
17
|
+
const dynamicOptions = [];
|
|
18
|
+
react_1.Children.forEach(children, element => {
|
|
19
|
+
if (!(0, react_1.isValidElement)(element))
|
|
20
|
+
return;
|
|
21
|
+
const option = element.props;
|
|
22
|
+
if (option.text && option.value)
|
|
23
|
+
dynamicOptions.push(option);
|
|
24
|
+
});
|
|
16
25
|
/** Sends a change event to the external consumer, but internall keeps the actual value as primitive or array. */
|
|
17
26
|
const onChange = (0, utils_1.useCallbackRef)((e) => {
|
|
18
27
|
onChangeProp === null || onChangeProp === void 0 ? void 0 : onChangeProp(e);
|
|
@@ -21,9 +30,9 @@ function useSelect(props) {
|
|
|
21
30
|
});
|
|
22
31
|
const valueText = isMultiple && Array.isArray(value)
|
|
23
32
|
? value[0]
|
|
24
|
-
? `${getValueText(value[0], options)}${value.length > 1 ? ` (+${value.length - 1})` : ''}`
|
|
33
|
+
? `${getValueText(value[0], [...options, ...dynamicOptions])}${value.length > 1 ? ` (+${value.length - 1})` : ''}`
|
|
25
34
|
: undefined
|
|
26
|
-
: getValueText(value, options);
|
|
35
|
+
: getValueText(value, [...options, ...dynamicOptions]);
|
|
27
36
|
return { onChange, value, valueText };
|
|
28
37
|
}
|
|
29
38
|
exports.default = useSelect;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
1
2
|
import { ChangeEvent } from '../utils';
|
|
2
3
|
import { SelectOptionData, SelectValue } from './select.types';
|
|
3
4
|
export declare type UseSelectProps = {
|
|
@@ -8,7 +9,9 @@ export declare type UseSelectProps = {
|
|
|
8
9
|
/** Allows choosing multiple values. Popover won't close automatically on item click. */
|
|
9
10
|
isMultiple?: boolean;
|
|
10
11
|
/** List of all available options */
|
|
11
|
-
options
|
|
12
|
+
options?: SelectOptionData[];
|
|
13
|
+
/** Select children elements for dynamic handling */
|
|
14
|
+
children?: ReactNode;
|
|
12
15
|
/**
|
|
13
16
|
* Callback triggered when an option is selected.
|
|
14
17
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.types.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE9D,oBAAY,cAAc,GAAG;IAC3B,mDAAmD;IACnD,KAAK,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IACnC,mDAAmD;IACnD,YAAY,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IAC1C,wFAAwF;IACxF,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,oCAAoC;IACpC,OAAO,EAAE,gBAAgB,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"useSelect.types.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEjC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE9D,oBAAY,cAAc,GAAG;IAC3B,mDAAmD;IACnD,KAAK,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IACnC,mDAAmD;IACnD,YAAY,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IAC1C,wFAAwF;IACxF,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,oCAAoC;IACpC,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC5B,oDAAoD;IACpD,QAAQ,CAAC,EAAE,SAAS,CAAA;IACpB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,WAAW,KAAK,IAAI,CAAA;CACpC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../../src/select/select.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAO5C;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../../src/select/select.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAO5C;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,WAAW,eAoDxC;yBApDe,MAAM;;;;;;;;;AA2DtB,eAAe,MAAM,CAAA"}
|
|
@@ -13,8 +13,8 @@ import useSelect from './useSelect';
|
|
|
13
13
|
* Supports single select and multi select, as well as controlled and uncontrolled modes.
|
|
14
14
|
*/
|
|
15
15
|
export function Select(props) {
|
|
16
|
-
const { children, defaultValue, disabled, isInvalid, isMultiple, name, onChange, options, placeholder = 'Please select', readOnly, size, value, variant, ...rest } = props;
|
|
17
|
-
const selectProps = useSelect({ defaultValue, isMultiple, onChange, value, options });
|
|
16
|
+
const { children, defaultValue, disabled, isInvalid, isMultiple, name, onChange, options, placeholder = 'Please select', readOnly, selectButton, size, value, variant, ...rest } = props;
|
|
17
|
+
const selectProps = useSelect({ defaultValue, isMultiple, onChange, value, options, children });
|
|
18
18
|
if (__DEV__ && isMultiple && !Array.isArray(selectProps.value)) {
|
|
19
19
|
console.error('<Select /> is used with isMultiple but its value is not an array: ', value);
|
|
20
20
|
}
|
|
@@ -31,10 +31,11 @@ export function Select(props) {
|
|
|
31
31
|
...selectProps
|
|
32
32
|
};
|
|
33
33
|
return (React.createElement(SelectProvider, { value: context },
|
|
34
|
-
React.createElement(Popover, { matchWidth: true, ...rest },
|
|
35
|
-
React.createElement(
|
|
36
|
-
|
|
37
|
-
React.createElement(
|
|
34
|
+
React.createElement(Popover, { matchWidth: true, ...rest },
|
|
35
|
+
React.createElement(React.Fragment, null,
|
|
36
|
+
selectButton ?? React.createElement(SelectButton, null),
|
|
37
|
+
React.createElement(SelectContent, null,
|
|
38
|
+
React.createElement(SelectGroup, null, children ?? options?.map?.(option => React.createElement(SelectOption, { key: option.value, ...option }))))))));
|
|
38
39
|
}
|
|
39
40
|
Select.Button = SelectButton;
|
|
40
41
|
Select.Content = SelectContent;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
1
2
|
import { ButtonProps } from '../button';
|
|
2
3
|
import { ListItemProps, ListProps } from '../list';
|
|
3
4
|
import { PopoverContentProps, PopoverProps } from '../popover';
|
|
@@ -16,11 +17,13 @@ export declare type SelectProps = ThemingProps<'Select'> & PopoverProps & UseSel
|
|
|
16
17
|
/** Name of the input. Used in custom change event to support form library integration. */
|
|
17
18
|
name?: string;
|
|
18
19
|
/** Data prop to display an array of options in the popover. */
|
|
19
|
-
options
|
|
20
|
+
options?: SelectOptionData[];
|
|
20
21
|
/** Displayed inside the trigger when no value is selected. */
|
|
21
22
|
placeholder?: string;
|
|
22
23
|
/** Select cannot be opened and is styled accordingly. */
|
|
23
24
|
readOnly?: boolean;
|
|
25
|
+
/** Custom select button slot. */
|
|
26
|
+
selectButton?: ReactNode;
|
|
24
27
|
};
|
|
25
28
|
export declare type SelectOptionData = {
|
|
26
29
|
/** Option is displayed as disabled. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.types.d.ts","sourceRoot":"","sources":["../../../src/select/select.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAElD,oBAAY,iBAAiB,GAAG,WAAW,CAAA;AAE3C,oBAAY,kBAAkB,GAAG,mBAAmB,CAAA;AAEpD,oBAAY,aAAa,GAAG,IAAI,CAC9B,WAAW,EACX,UAAU,GAAG,WAAW,GAAG,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,aAAa,GAAG,UAAU,GAAG,MAAM,GAAG,SAAS,CAC/G,GACC,mBAAmB,CAAA;AAErB,oBAAY,gBAAgB,GAAG,SAAS,CAAA;AAExC,oBAAY,WAAW,GAAG,YAAY,CAAC,QAAQ,CAAC,GAC9C,YAAY,GACZ,cAAc,GAAG;IACf,yDAAyD;IACzD,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,+DAA+D;IAC/D,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,0FAA0F;IAC1F,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,+DAA+D;IAC/D,OAAO,EAAE,gBAAgB,EAAE,CAAA;
|
|
1
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AAG;IAC7B,uCAAuC;IACvC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,gCAAgC;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,+BAA+B;IAC/B,KAAK,EAAE,WAAW,CAAA;CACnB,CAAA;AAED,oBAAY,iBAAiB,GAAG,aAAa,GAAG;IAC9C,mGAAmG;IACnG,KAAK,CAAC,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,oBAAY,WAAW,GAAG,MAAM,GAAG,MAAM,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAA8B,MAAM,UAAU,CAAA;AAClE,OAAO,EAAoB,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAQlD,wFAAwF;AACxF,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,KAAK,EAAE,cAAc;
|
|
1
|
+
{"version":3,"file":"useSelect.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAA8B,MAAM,UAAU,CAAA;AAClE,OAAO,EAAoB,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAQlD,wFAAwF;AACxF,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,KAAK,EAAE,cAAc;kBAiBjB,WAAW;;;EAiBhD;AAED,oBAAY,mBAAmB,GAAG,UAAU,CAAC,OAAO,SAAS,CAAC,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
1
|
+
import { Children, isValidElement, useState } from 'react';
|
|
2
2
|
import { toggleItem, useCallbackRef } from '../utils';
|
|
3
3
|
const getValueText = (value, options) => {
|
|
4
4
|
const option = options?.find((o) => o?.value === value);
|
|
@@ -6,11 +6,20 @@ const getValueText = (value, options) => {
|
|
|
6
6
|
};
|
|
7
7
|
/** Handles controlled/uncontrolled value state. Supports single and multiple values. */
|
|
8
8
|
export default function useSelect(props) {
|
|
9
|
-
const { defaultValue, isMultiple, onChange: onChangeProp, value: valueProp, options } = props;
|
|
9
|
+
const { defaultValue, isMultiple, onChange: onChangeProp, value: valueProp, options = [], children } = props;
|
|
10
10
|
// Not using useControlled here, because we need custom value setter when uncontrolled
|
|
11
11
|
const [stateValue, setStateValue] = useState(defaultValue);
|
|
12
12
|
const isControlled = valueProp !== undefined;
|
|
13
13
|
const value = isControlled ? valueProp : stateValue;
|
|
14
|
+
// Iterating over the children to dynamically obtain the list of available options
|
|
15
|
+
const dynamicOptions = [];
|
|
16
|
+
Children.forEach(children, element => {
|
|
17
|
+
if (!isValidElement(element))
|
|
18
|
+
return;
|
|
19
|
+
const option = element.props;
|
|
20
|
+
if (option.text && option.value)
|
|
21
|
+
dynamicOptions.push(option);
|
|
22
|
+
});
|
|
14
23
|
/** Sends a change event to the external consumer, but internall keeps the actual value as primitive or array. */
|
|
15
24
|
const onChange = useCallbackRef((e) => {
|
|
16
25
|
onChangeProp?.(e);
|
|
@@ -19,8 +28,8 @@ export default function useSelect(props) {
|
|
|
19
28
|
});
|
|
20
29
|
const valueText = isMultiple && Array.isArray(value)
|
|
21
30
|
? value[0]
|
|
22
|
-
? `${getValueText(value[0], options)}${value.length > 1 ? ` (+${value.length - 1})` : ''}`
|
|
31
|
+
? `${getValueText(value[0], [...options, ...dynamicOptions])}${value.length > 1 ? ` (+${value.length - 1})` : ''}`
|
|
23
32
|
: undefined
|
|
24
|
-
: getValueText(value, options);
|
|
33
|
+
: getValueText(value, [...options, ...dynamicOptions]);
|
|
25
34
|
return { onChange, value, valueText };
|
|
26
35
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
1
2
|
import { ChangeEvent } from '../utils';
|
|
2
3
|
import { SelectOptionData, SelectValue } from './select.types';
|
|
3
4
|
export declare type UseSelectProps = {
|
|
@@ -8,7 +9,9 @@ export declare type UseSelectProps = {
|
|
|
8
9
|
/** Allows choosing multiple values. Popover won't close automatically on item click. */
|
|
9
10
|
isMultiple?: boolean;
|
|
10
11
|
/** List of all available options */
|
|
11
|
-
options
|
|
12
|
+
options?: SelectOptionData[];
|
|
13
|
+
/** Select children elements for dynamic handling */
|
|
14
|
+
children?: ReactNode;
|
|
12
15
|
/**
|
|
13
16
|
* Callback triggered when an option is selected.
|
|
14
17
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSelect.types.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE9D,oBAAY,cAAc,GAAG;IAC3B,mDAAmD;IACnD,KAAK,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IACnC,mDAAmD;IACnD,YAAY,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IAC1C,wFAAwF;IACxF,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,oCAAoC;IACpC,OAAO,EAAE,gBAAgB,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"useSelect.types.d.ts","sourceRoot":"","sources":["../../../src/select/useSelect.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEjC,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE9D,oBAAY,cAAc,GAAG;IAC3B,mDAAmD;IACnD,KAAK,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IACnC,mDAAmD;IACnD,YAAY,CAAC,EAAE,WAAW,GAAG,WAAW,EAAE,CAAA;IAC1C,wFAAwF;IACxF,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,oCAAoC;IACpC,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC5B,oDAAoD;IACpD,QAAQ,CAAC,EAAE,SAAS,CAAA;IACpB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,WAAW,KAAK,IAAI,CAAA;CACpC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veracity/vui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Veracity UI is a React component library crafted for use within Veracity applications and pages. Based on Styled Components and @xstyled.",
|
|
5
5
|
"module": "./dist/esm/index.js",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
package/src/select/select.tsx
CHANGED
|
@@ -27,13 +27,14 @@ export function Select(props: SelectProps) {
|
|
|
27
27
|
options,
|
|
28
28
|
placeholder = 'Please select',
|
|
29
29
|
readOnly,
|
|
30
|
+
selectButton,
|
|
30
31
|
size,
|
|
31
32
|
value,
|
|
32
33
|
variant,
|
|
33
34
|
...rest
|
|
34
35
|
} = props
|
|
35
36
|
|
|
36
|
-
const selectProps = useSelect({ defaultValue, isMultiple, onChange, value, options })
|
|
37
|
+
const selectProps = useSelect({ defaultValue, isMultiple, onChange, value, options, children })
|
|
37
38
|
|
|
38
39
|
if (__DEV__ && isMultiple && !Array.isArray(selectProps.value)) {
|
|
39
40
|
console.error('<Select /> is used with isMultiple but its value is not an array: ', value)
|
|
@@ -55,18 +56,14 @@ export function Select(props: SelectProps) {
|
|
|
55
56
|
return (
|
|
56
57
|
<SelectProvider value={context}>
|
|
57
58
|
<Popover matchWidth {...rest}>
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
<
|
|
62
|
-
<
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
</SelectGroup>
|
|
67
|
-
</SelectContent>
|
|
68
|
-
</>
|
|
69
|
-
)}
|
|
59
|
+
<>
|
|
60
|
+
{selectButton ?? <SelectButton />}
|
|
61
|
+
<SelectContent>
|
|
62
|
+
<SelectGroup>
|
|
63
|
+
{children ?? options?.map?.(option => <SelectOption key={option.value} {...option} />)}
|
|
64
|
+
</SelectGroup>
|
|
65
|
+
</SelectContent>
|
|
66
|
+
</>
|
|
70
67
|
</Popover>
|
|
71
68
|
</SelectProvider>
|
|
72
69
|
)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ReactNode } from 'react'
|
|
2
|
+
|
|
1
3
|
import { ButtonProps } from '../button'
|
|
2
4
|
import { ListItemProps, ListProps } from '../list'
|
|
3
5
|
import { PopoverContentProps, PopoverProps } from '../popover'
|
|
@@ -27,11 +29,13 @@ export type SelectProps = ThemingProps<'Select'> &
|
|
|
27
29
|
/** Name of the input. Used in custom change event to support form library integration. */
|
|
28
30
|
name?: string
|
|
29
31
|
/** Data prop to display an array of options in the popover. */
|
|
30
|
-
options
|
|
32
|
+
options?: SelectOptionData[]
|
|
31
33
|
/** Displayed inside the trigger when no value is selected. */
|
|
32
34
|
placeholder?: string
|
|
33
35
|
/** Select cannot be opened and is styled accordingly. */
|
|
34
36
|
readOnly?: boolean
|
|
37
|
+
/** Custom select button slot. */
|
|
38
|
+
selectButton?: ReactNode
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
export type SelectOptionData = {
|
package/src/select/useSelect.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState } from 'react'
|
|
1
|
+
import { Children, isValidElement, useState } from 'react'
|
|
2
2
|
|
|
3
3
|
import { ChangeEvent, toggleItem, useCallbackRef } from '../utils'
|
|
4
4
|
import { SelectOptionData, SelectValue } from './select.types'
|
|
@@ -12,13 +12,21 @@ const getValueText = (value: SelectValue, options: SelectOptionData[]) => {
|
|
|
12
12
|
|
|
13
13
|
/** Handles controlled/uncontrolled value state. Supports single and multiple values. */
|
|
14
14
|
export default function useSelect(props: UseSelectProps) {
|
|
15
|
-
const { defaultValue, isMultiple, onChange: onChangeProp, value: valueProp, options } = props
|
|
15
|
+
const { defaultValue, isMultiple, onChange: onChangeProp, value: valueProp, options = [], children } = props
|
|
16
16
|
|
|
17
17
|
// Not using useControlled here, because we need custom value setter when uncontrolled
|
|
18
18
|
const [stateValue, setStateValue] = useState(defaultValue)
|
|
19
19
|
const isControlled = valueProp !== undefined
|
|
20
20
|
const value = isControlled ? valueProp : stateValue
|
|
21
21
|
|
|
22
|
+
// Iterating over the children to dynamically obtain the list of available options
|
|
23
|
+
const dynamicOptions: SelectOptionData[] = []
|
|
24
|
+
Children.forEach(children, element => {
|
|
25
|
+
if (!isValidElement(element)) return
|
|
26
|
+
const option: SelectOptionData = element.props as SelectOptionData
|
|
27
|
+
if (option.text && option.value) dynamicOptions.push(option)
|
|
28
|
+
})
|
|
29
|
+
|
|
22
30
|
/** Sends a change event to the external consumer, but internall keeps the actual value as primitive or array. */
|
|
23
31
|
const onChange = useCallbackRef((e: ChangeEvent) => {
|
|
24
32
|
onChangeProp?.(e)
|
|
@@ -30,9 +38,11 @@ export default function useSelect(props: UseSelectProps) {
|
|
|
30
38
|
const valueText =
|
|
31
39
|
isMultiple && Array.isArray(value)
|
|
32
40
|
? value[0]
|
|
33
|
-
? `${getValueText(value[0], options)}${
|
|
41
|
+
? `${getValueText(value[0], [...options, ...dynamicOptions])}${
|
|
42
|
+
value.length > 1 ? ` (+${value.length - 1})` : ''
|
|
43
|
+
}`
|
|
34
44
|
: undefined
|
|
35
|
-
: getValueText(value as SelectValue, options)
|
|
45
|
+
: getValueText(value as SelectValue, [...options, ...dynamicOptions])
|
|
36
46
|
|
|
37
47
|
return { onChange, value, valueText }
|
|
38
48
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ReactNode } from 'react'
|
|
2
|
+
|
|
1
3
|
import { ChangeEvent } from '../utils'
|
|
2
4
|
import { SelectOptionData, SelectValue } from './select.types'
|
|
3
5
|
|
|
@@ -9,7 +11,9 @@ export type UseSelectProps = {
|
|
|
9
11
|
/** Allows choosing multiple values. Popover won't close automatically on item click. */
|
|
10
12
|
isMultiple?: boolean
|
|
11
13
|
/** List of all available options */
|
|
12
|
-
options
|
|
14
|
+
options?: SelectOptionData[]
|
|
15
|
+
/** Select children elements for dynamic handling */
|
|
16
|
+
children?: ReactNode
|
|
13
17
|
/**
|
|
14
18
|
* Callback triggered when an option is selected.
|
|
15
19
|
*
|