@rjsf/chakra-ui 6.4.2 → 6.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rjsf/chakra-ui",
3
- "version": "6.4.2",
3
+ "version": "6.5.0",
4
4
  "description": "Chakra UI theme, fields, and widgets for react-jsonschema-form",
5
5
  "main": "dist/index.js",
6
6
  "module": "lib/index.js",
@@ -85,21 +85,21 @@
85
85
  },
86
86
  "license": "Apache-2.0",
87
87
  "devDependencies": {
88
- "@chakra-ui/cli": "3.29.0",
89
- "@chakra-ui/react": "3.29.0",
88
+ "@chakra-ui/cli": "^3.34.0",
89
+ "@chakra-ui/react": "^3.34.0",
90
90
  "@emotion/eslint-plugin": "^11.12.0",
91
91
  "@emotion/jest": "^11.14.2",
92
92
  "@emotion/react": "^11.14.0",
93
- "@rjsf/core": "6.4.2",
94
- "@rjsf/snapshot-tests": "6.4.2",
95
- "@rjsf/utils": "6.4.2",
96
- "@rjsf/validator-ajv8": "6.4.2",
93
+ "@rjsf/core": "6.5.0",
94
+ "@rjsf/snapshot-tests": "6.5.0",
95
+ "@rjsf/utils": "6.5.0",
96
+ "@rjsf/validator-ajv8": "6.5.0",
97
97
  "chakra-react-select": "^6.1.1",
98
98
  "eslint": "^8.57.1"
99
99
  },
100
100
  "dependencies": {
101
101
  "lucide-react": "^0.548.0",
102
- "react-icons": "^5.5.0",
102
+ "react-icons": "^5.6.0",
103
103
  "react-select": "^5.10.2"
104
104
  }
105
105
  }
@@ -1,8 +1,10 @@
1
1
  import { CheckboxGroup, FieldsetRoot, Stack, Text, FieldsetLegend } from '@chakra-ui/react';
2
2
  import {
3
3
  ariaDescribedByIds,
4
- enumOptionsIndexForValue,
5
- enumOptionsValueForIndex,
4
+ enumOptionSelectedValue,
5
+ enumOptionValueDecoder,
6
+ enumOptionValueEncoder,
7
+ getOptionValueFormat,
6
8
  FormContextType,
7
9
  optionId,
8
10
  RJSFSchema,
@@ -37,14 +39,15 @@ export default function CheckboxesWidget<
37
39
  uiSchema,
38
40
  } = props;
39
41
  const { enumOptions, enumDisabled, emptyValue } = options;
42
+ const optionValueFormat = getOptionValueFormat(options);
40
43
 
41
44
  const _onBlur = ({ target }: FocusEvent<HTMLInputElement | any>) =>
42
- onBlur(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, emptyValue));
45
+ onBlur(id, enumOptionValueDecoder<S>(target && target.value, enumOptions, optionValueFormat, emptyValue));
43
46
  const _onFocus = ({ target }: FocusEvent<HTMLInputElement | any>) =>
44
- onFocus(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, emptyValue));
47
+ onFocus(id, enumOptionValueDecoder<S>(target && target.value, enumOptions, optionValueFormat, emptyValue));
45
48
 
46
49
  const row = options ? options.inline : false;
47
- const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, true) as string[];
50
+ const selectValue = enumOptionSelectedValue<S>(value, enumOptions, true, optionValueFormat, []) as string[];
48
51
 
49
52
  const chakraProps = getChakra({ uiSchema });
50
53
 
@@ -57,11 +60,13 @@ export default function CheckboxesWidget<
57
60
  >
58
61
  {!hideLabel && label && <FieldsetLegend>{labelValue(label)}</FieldsetLegend>}
59
62
  <CheckboxGroup
60
- onValueChange={(option) => onChange(enumOptionsValueForIndex<S>(option, enumOptions, emptyValue))}
61
- value={selectedIndexes}
63
+ onValueChange={(option) =>
64
+ onChange(enumOptionValueDecoder<S>(option, enumOptions, optionValueFormat, emptyValue))
65
+ }
66
+ value={selectValue}
62
67
  aria-describedby={ariaDescribedByIds(id)}
63
68
  readOnly={readonly}
64
- required={required}
69
+ invalid={required && value.length === 0}
65
70
  >
66
71
  <Stack direction={row ? 'row' : 'column'}>
67
72
  {Array.isArray(enumOptions) &&
@@ -72,7 +77,7 @@ export default function CheckboxesWidget<
72
77
  key={index}
73
78
  id={optionId(id, index)}
74
79
  name={htmlName || id}
75
- value={String(index)}
80
+ value={enumOptionValueEncoder(option.value, index, optionValueFormat)}
76
81
  disabled={disabled || itemDisabled || readonly}
77
82
  onBlur={_onBlur}
78
83
  onFocus={_onFocus}
@@ -2,8 +2,10 @@ import { ChangeEvent, FocusEvent } from 'react';
2
2
  import { Stack } from '@chakra-ui/react';
3
3
  import {
4
4
  ariaDescribedByIds,
5
- enumOptionsIndexForValue,
6
- enumOptionsValueForIndex,
5
+ enumOptionSelectedValue,
6
+ enumOptionValueDecoder,
7
+ enumOptionValueEncoder,
8
+ getOptionValueFormat,
7
9
  labelValue,
8
10
  optionId,
9
11
  FormContextType,
@@ -32,16 +34,17 @@ export default function RadioWidget<T = any, S extends StrictRJSFSchema = RJSFSc
32
34
  uiSchema,
33
35
  }: WidgetProps<T, S, F>) {
34
36
  const { enumOptions, enumDisabled, emptyValue } = options;
37
+ const optionValueFormat = getOptionValueFormat(options);
35
38
 
36
39
  const _onChange = ({ target: { value } }: ChangeEvent<HTMLInputElement>) =>
37
- onChange(enumOptionsValueForIndex<S>(value, enumOptions, emptyValue));
40
+ onChange(enumOptionValueDecoder<S>(value, enumOptions, optionValueFormat, emptyValue));
38
41
  const _onBlur = ({ target: { value } }: FocusEvent<HTMLInputElement>) =>
39
- onBlur(id, enumOptionsValueForIndex<S>(value, enumOptions, emptyValue));
42
+ onBlur(id, enumOptionValueDecoder<S>(value, enumOptions, optionValueFormat, emptyValue));
40
43
  const _onFocus = ({ target: { value } }: FocusEvent<HTMLInputElement>) =>
41
- onFocus(id, enumOptionsValueForIndex<S>(value, enumOptions, emptyValue));
44
+ onFocus(id, enumOptionValueDecoder<S>(value, enumOptions, optionValueFormat, emptyValue));
42
45
 
43
46
  const row = options ? options.inline : false;
44
- const selectedIndex = (enumOptionsIndexForValue<S>(value, enumOptions) as string) ?? null;
47
+ const selectValue = enumOptionSelectedValue<S>(value, enumOptions, false, optionValueFormat, null);
45
48
 
46
49
  const chakraProps = getChakra({ uiSchema });
47
50
 
@@ -58,7 +61,7 @@ export default function RadioWidget<T = any, S extends StrictRJSFSchema = RJSFSc
58
61
  onChange={_onChange}
59
62
  onBlur={_onBlur}
60
63
  onFocus={_onFocus}
61
- value={selectedIndex}
64
+ value={selectValue}
62
65
  name={htmlName || id}
63
66
  aria-describedby={ariaDescribedByIds(id)}
64
67
  >
@@ -69,7 +72,7 @@ export default function RadioWidget<T = any, S extends StrictRJSFSchema = RJSFSc
69
72
 
70
73
  return (
71
74
  <Radio
72
- value={String(index)}
75
+ value={enumOptionValueEncoder(option.value, index, optionValueFormat)}
73
76
  key={index}
74
77
  id={optionId(id, index)}
75
78
  disabled={disabled || itemDisabled || readonly}
@@ -3,8 +3,10 @@ import { FocusEvent, useMemo, useRef } from 'react';
3
3
  import {
4
4
  ariaDescribedByIds,
5
5
  EnumOptionsType,
6
- enumOptionsIndexForValue,
7
- enumOptionsValueForIndex,
6
+ enumOptionSelectedValue,
7
+ enumOptionValueDecoder,
8
+ enumOptionValueEncoder,
9
+ getOptionValueFormat,
8
10
  labelValue,
9
11
  FormContextType,
10
12
  RJSFSchema,
@@ -42,36 +44,32 @@ export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFS
42
44
  uiSchema,
43
45
  } = props;
44
46
  const { enumOptions, enumDisabled, emptyValue } = options;
47
+ const optionValueFormat = getOptionValueFormat(options);
45
48
 
46
49
  const _onMultiChange = ({ value }: SelectValueChangeDetails) => {
47
- return onChange(enumOptionsValueForIndex<S>(value, enumOptions, emptyValue));
50
+ return onChange(enumOptionValueDecoder<S>(value, enumOptions, optionValueFormat, emptyValue));
48
51
  };
49
52
 
50
53
  const _onSingleChange = ({ value }: SelectValueChangeDetails) => {
51
- const selected = enumOptionsValueForIndex<S>(value, enumOptions, emptyValue);
54
+ const selected = enumOptionValueDecoder<S>(value, enumOptions, optionValueFormat, emptyValue);
52
55
  return onChange(Array.isArray(selected) && selected.length === 1 ? selected[0] : selected);
53
56
  };
54
57
 
55
58
  const _onBlur = ({ target }: FocusEvent<HTMLInputElement>) =>
56
- onBlur(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, emptyValue));
59
+ onBlur(id, enumOptionValueDecoder<S>(target && target.value, enumOptions, optionValueFormat, emptyValue));
57
60
 
58
61
  const _onFocus = ({ target }: FocusEvent<HTMLInputElement>) =>
59
- onFocus(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, emptyValue));
62
+ onFocus(id, enumOptionValueDecoder<S>(target && target.value, enumOptions, optionValueFormat, emptyValue));
60
63
 
61
64
  const showPlaceholderOption = !multiple && schema.default === undefined;
62
- const { valueLabelMap, displayEnumOptions } = useMemo((): {
63
- valueLabelMap: Record<string | number, string>;
64
- displayEnumOptions: OptionsOrGroups<any, any>;
65
- } => {
66
- const valueLabelMap: Record<string | number, string> = {};
65
+ const displayEnumOptions = useMemo((): OptionsOrGroups<any, any> => {
67
66
  let displayEnumOptions: OptionsOrGroups<any, any> = [];
68
67
  if (Array.isArray(enumOptions)) {
69
68
  displayEnumOptions = enumOptions.map((option: EnumOptionsType<S>, index: number) => {
70
69
  const { value, label } = option;
71
- valueLabelMap[index] = label || String(value);
72
70
  return {
73
71
  label,
74
- value: String(index),
72
+ value: enumOptionValueEncoder(value, index, optionValueFormat),
75
73
  disabled: Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1,
76
74
  };
77
75
  });
@@ -79,31 +77,18 @@ export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFS
79
77
  (displayEnumOptions as any[]).unshift({ value: '', label: placeholder || '' });
80
78
  }
81
79
  }
82
- return { valueLabelMap: valueLabelMap, displayEnumOptions: displayEnumOptions };
83
- }, [enumDisabled, enumOptions, placeholder, showPlaceholderOption]);
80
+ return displayEnumOptions;
81
+ }, [enumDisabled, enumOptions, placeholder, showPlaceholderOption, optionValueFormat]);
84
82
 
85
83
  const isMultiple = typeof multiple !== 'undefined' && multiple !== false && Boolean(enumOptions);
86
- const selectedIndex = enumOptionsIndexForValue<S>(value, enumOptions, isMultiple);
87
84
 
88
- const getMultiValue = () =>
89
- ((selectedIndex as string[]) || []).map((i: string) => {
90
- return {
91
- label: valueLabelMap[i],
92
- value: i.toString(),
93
- };
94
- });
95
-
96
- const getSingleValue = () =>
97
- typeof selectedIndex !== 'undefined'
98
- ? [
99
- {
100
- label: valueLabelMap[selectedIndex as string] || '',
101
- value: selectedIndex.toString(),
102
- },
103
- ]
104
- : [];
105
-
106
- const formValue = (isMultiple ? getMultiValue() : getSingleValue()).map((item) => item.value);
85
+ // Chakra's SelectRoot always expects a string array, so flatten the helper's
86
+ // single/multiple return shape and strip the empty-single case.
87
+ const formValue = [
88
+ enumOptionSelectedValue<S>(value, enumOptions, isMultiple, optionValueFormat, isMultiple ? [] : ''),
89
+ ]
90
+ .flat()
91
+ .filter((v) => v !== '') as string[];
107
92
 
108
93
  const selectOptions = createListCollection({
109
94
  items: displayEnumOptions.filter((item) => item.value),
@@ -60,6 +60,7 @@ export default function WrapIfAdditionalTemplate<
60
60
  <GridItem colSpan={5} style={{ marginTop: hasDescription ? '36px' : undefined }}>
61
61
  <Field required={required} label={keyLabel}>
62
62
  <Input
63
+ key={label}
63
64
  defaultValue={label}
64
65
  disabled={disabled || readonly}
65
66
  id={`${id}-key`}