Skip to content

Commit 148f72f

Browse files
feat: Add support for user nameGenerator function (rjsf-team#4773)
* feat: Add `htmlName` prop * feat: Add `idPrefix` arg to `nameGeneratorFunction` * fix: Wrong prop order * feat: Add common name generators * feat: Handle multiple values field(s) * fix: Use consistent variable name Co-authored-by: Heath C <[email protected]> * docs: Describe `nameGenerator` prop & add feature in the migration guide * docs: Add `@rjsf/utils` and `@rjsf/core` changelog entries * feat: Add `htmlName` to antd theme * feat: Add `htmlName` to Chakra UI theme * feat: Add `htmlName` to Daisy UI theme * feat: Add `htmlName` to Fluent UI theme * feat: Add `htmlName` to Mantine theme * feat: Add `htmlName` to Mui theme * feat: Add `htmlName` to Primereact theme * feat: Add `htmlName` to React Bootstrap theme * feat: Add `htmlName` to Semantic UI theme * feat: Add `htmlName` to Shadcn theme * docs: Add theme changes to changelog * chore: Minor standard fixes Authored-by: Heath C <[email protected]> --------- Co-authored-by: Heath C <[email protected]>
1 parent 803c813 commit 148f72f

File tree

82 files changed

+550
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+550
-88
lines changed

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ should change the heading of the (upcoming) version to include a major version b
2121

2222
- Updated most of the widgets to get `formContext` from the `registry` instead of the `props` since it will no longer be passed
2323
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
24+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
2425

2526
## @rjsf/chakra-ui
2627

2728
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
29+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
2830

2931
## @rjsf/core
3032

@@ -33,48 +35,61 @@ should change the heading of the (upcoming) version to include a major version b
3335
- Updated `ObjectField` to refactor the code from a class component to two stateless functional components, replacing the 3 generator-props with the 4 memoized props mentioned in the `@rjsf/utils` changes
3436
- Updated `Form` to "memoize" the `fieldPathId` and `registry` into the `FormState`, adding a `toIChangeEvent()` helper to restrict the state returned on the `IChangeEvent` interface callbacks
3537
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
38+
- Added `nameGenerator` prop to `Form` component to enable custom HTML `name` attribute generation for form fields
3639

3740
## @rjsf/daisyui
3841

3942
- Updated the test mocks to remove `formContext` for the widget mock
4043
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
44+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
4145

4246
## @rjsf/fluentui-rc
4347

4448
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
49+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
4550

4651
## @rjsf/mantine
4752

4853
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
54+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
4955

5056
## @rjsf/mui
5157

5258
- Updated `BaseInputTemplate` and `SelectWidget` to remove `formContext` from the props
5359
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
60+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
5461

5562
## @rjsf/primereact
5663

5764
- Updated `SelectWidget` to remove `formContext` from the props
5865
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
66+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
5967

6068
## @rjsf/react-bootstrap
6169

6270
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
71+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
6372

6473
## @rjsf/semantic-ui
6574

6675
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
76+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `RadioWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute
6777

6878
## @rjsf/shadcn
6979

7080
- Updated the test mocks to remove `formContext` for the widget mock and added `globalFormOptions` in the registry mock
7181
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
82+
- Updated `BaseInputTemplate`, `CheckboxesWidget`, `CheckboxWidget`, `SelectWidget`, and `TextareaWidget` to use `htmlName` for the HTML `name` attribute (Note: `RadioWidget` does not support `htmlName` due to Radix UI RadioGroup limitations)
7283

7384
## @rjsf/utils
7485

7586
- BREAKING CHANGE: Updated `FieldTemplateProps` and `WrapIfAdditionalTemplateProps` to replace the `onKeyChange()` and `onDropPropertyClick()` callback generator props with the `onKeyRename()`, `onKeyRenameBlur()` and `onRemoveProperty()` callback props
7687
- BREAKING CHANGE: Updated `ObjectFieldTemplateProps` to replace the `onAddClick()` callback generator prop with the `onAddProperty()` callback prop
7788
- Added new hook `useDeepCompareMemo()` and its associated tests
89+
- Added `NameGeneratorFunction` type and two built-in name generators: `bracketNameGenerator` and `dotNotationNameGenerator`
90+
- Updated `GlobalFormOptions` type to include optional `nameGenerator` field
91+
- Updated `toFieldPathId()` function to support name generation via the `nameGenerator` option in `GlobalFormOptions`
92+
- Added `htmlName` field to `WidgetProps` interface to provide the generated HTML `name` attribute to widgets
7893

7994
## Dev / docs / playground
8095
- Updated the `formTests.tsx` snapshots to add an `anyOf` of all arrays with different item types and removed the disabling of the optional data controls feature for the optional object with oneOfs
@@ -86,6 +101,7 @@ should change the heading of the (upcoming) version to include a major version b
86101
- Updated the `custom-templates.md` documentation to reflect the `additionalProperties`-based interface props replacement
87102
- Updated the `utility-functions.mf` documenation to add the new `useDeepCompareMemo()` hook
88103
- Updated the `v6.x upgrade guide.md` documentation to add the BREAKING CHANGES to the `FieldTemplateProps`, `ObjectFieldTemplateProps` and `WrapIfAdditionalTemplateProps` interface props changes and the `useDeepCompareMemo()` hook
104+
- Added documentation for the `nameGenerator` prop in `form-props.md` and v6.x upgrade guide
89105

90106
# 6.0.0-beta.21
91107

@@ -209,7 +225,7 @@ should change the heading of the (upcoming) version to include a major version b
209225
- `ObjectField` and `ArrayField` to use `toFieldPathId` instead of `toIdSchema()` to generate the `fieldPathId`s of all its children
210226
- Updated the `onChange` handling of fields to make `path` required and either pass it straight through, or use the `fieldPathId.path` instead of using an empty array or appending path information
211227
- Updated `Form` to use `toFieldPathId()` to generate `fieldPathId` instead of `idSchema`, always providing the `idPrefix` and `idSeparator` in the `globalFormOptions` and make the `path: FieldPathList` required
212-
- Updated `LayoutGridField` to remove the `IdSchema` related code in favor of `FieldPathId` code
228+
- Updated `LayoutGridField` to remove the `IdSchema` related code in favor of `FieldPathId` code
213229
- Also exported the `getTestRegistry()` function from the main `index.ts` to assist developers in creating `registry` object for tests
214230
- Updated all of the test to deal with the `idSchema` -> `fieldPathId` changes
215231

packages/antd/src/templates/BaseInputTemplate/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default function BaseInputTemplate<
3030
disabled,
3131
registry,
3232
id,
33+
htmlName,
3334
onBlur,
3435
onChange,
3536
onChangeOverride,
@@ -60,7 +61,7 @@ export default function BaseInputTemplate<
6061
<InputNumber
6162
disabled={disabled || (readonlyAsDisabled && readonly)}
6263
id={id}
63-
name={id}
64+
name={htmlName || id}
6465
onBlur={!readonly ? handleBlur : undefined}
6566
onChange={!readonly ? handleNumberChange : undefined}
6667
onFocus={!readonly ? handleFocus : undefined}
@@ -75,7 +76,7 @@ export default function BaseInputTemplate<
7576
<Input
7677
disabled={disabled || (readonlyAsDisabled && readonly)}
7778
id={id}
78-
name={id}
79+
name={htmlName || id}
7980
onBlur={!readonly ? handleBlur : undefined}
8081
onChange={!readonly ? handleTextChange : undefined}
8182
onFocus={!readonly ? handleFocus : undefined}

packages/antd/src/widgets/CheckboxWidget/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default function CheckboxWidget<
2020
S extends StrictRJSFSchema = RJSFSchema,
2121
F extends FormContextType = any,
2222
>(props: WidgetProps<T, S, F>) {
23-
const { autofocus, disabled, registry, id, label, hideLabel, onBlur, onChange, onFocus, readonly, value } = props;
23+
const { autofocus, disabled, registry, id, htmlName, label, hideLabel, onBlur, onChange, onFocus, readonly, value } =
24+
props;
2425
const { formContext } = registry;
2526
const { readonlyAsDisabled = true } = formContext as GenericObjectType;
2627

@@ -42,7 +43,7 @@ export default function CheckboxWidget<
4243
checked={typeof value === 'undefined' ? false : value}
4344
disabled={disabled || (readonlyAsDisabled && readonly)}
4445
id={id}
45-
name={id}
46+
name={htmlName || id}
4647
onChange={!readonly ? handleChange : undefined}
4748
{...extraProps}
4849
aria-describedby={ariaDescribedByIds(id)}

packages/antd/src/widgets/CheckboxesWidget/index.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ export default function CheckboxesWidget<
2121
T = any,
2222
S extends StrictRJSFSchema = RJSFSchema,
2323
F extends FormContextType = any,
24-
>({ autofocus, disabled, registry, id, onBlur, onChange, onFocus, options, readonly, value }: WidgetProps<T, S, F>) {
24+
>({
25+
autofocus,
26+
disabled,
27+
registry,
28+
id,
29+
htmlName,
30+
onBlur,
31+
onChange,
32+
onFocus,
33+
options,
34+
readonly,
35+
value,
36+
}: WidgetProps<T, S, F>) {
2537
const { formContext } = registry;
2638
const { readonlyAsDisabled = true } = formContext as GenericObjectType;
2739

@@ -49,7 +61,7 @@ export default function CheckboxesWidget<
4961
<>
5062
<Checkbox.Group
5163
disabled={disabled || (readonlyAsDisabled && readonly)}
52-
name={id}
64+
name={htmlName || id}
5365
onChange={!readonly ? handleChange : undefined}
5466
value={selectedIndexes}
5567
{...extraProps}
@@ -60,7 +72,7 @@ export default function CheckboxesWidget<
6072
<span key={i}>
6173
<Checkbox
6274
id={optionId(id, i)}
63-
name={id}
75+
name={htmlName || id}
6476
autoFocus={i === 0 ? autofocus : false}
6577
disabled={Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1}
6678
value={String(i)}

packages/antd/src/widgets/RadioWidget/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function RadioWidget<T = any, S extends StrictRJSFSchema = RJSFSc
2222
disabled,
2323
registry,
2424
id,
25+
htmlName,
2526
onBlur,
2627
onChange,
2728
onFocus,
@@ -49,7 +50,7 @@ export default function RadioWidget<T = any, S extends StrictRJSFSchema = RJSFSc
4950
<Radio.Group
5051
disabled={disabled || (readonlyAsDisabled && readonly)}
5152
id={id}
52-
name={id}
53+
name={htmlName || id}
5354
onChange={!readonly ? handleChange : undefined}
5455
onBlur={!readonly ? handleBlur : undefined}
5556
onFocus={!readonly ? handleFocus : undefined}
@@ -60,7 +61,7 @@ export default function RadioWidget<T = any, S extends StrictRJSFSchema = RJSFSc
6061
enumOptions.map((option, i) => (
6162
<Radio
6263
id={optionId(id, i)}
63-
name={id}
64+
name={htmlName || id}
6465
autoFocus={i === 0 ? autofocus : false}
6566
disabled={disabled || (Array.isArray(enumDisabled) && enumDisabled.indexOf(option.value) !== -1)}
6667
key={i}

packages/antd/src/widgets/SelectWidget/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default function SelectWidget<
3131
disabled,
3232
registry,
3333
id,
34+
htmlName,
3435
multiple,
3536
onBlur,
3637
onChange,
@@ -67,7 +68,7 @@ export default function SelectWidget<
6768
// Antd's typescript definitions do not contain the following props that are actually necessary and, if provided,
6869
// they are used, so hacking them in via by spreading `extraProps` on the component to avoid typescript errors
6970
const extraProps = {
70-
name: id,
71+
name: htmlName || id,
7172
};
7273

7374
const showPlaceholderOption = !multiple && schema.default === undefined;

packages/antd/src/widgets/TextareaWidget/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ export default function TextareaWidget<
2121
T = any,
2222
S extends StrictRJSFSchema = RJSFSchema,
2323
F extends FormContextType = any,
24-
>({ disabled, registry, id, onBlur, onChange, onFocus, options, placeholder, readonly, value }: WidgetProps<T, S, F>) {
24+
>({
25+
disabled,
26+
registry,
27+
id,
28+
htmlName,
29+
onBlur,
30+
onChange,
31+
onFocus,
32+
options,
33+
placeholder,
34+
readonly,
35+
value,
36+
}: WidgetProps<T, S, F>) {
2537
const { formContext } = registry;
2638
const { readonlyAsDisabled = true } = formContext as GenericObjectType;
2739

@@ -42,7 +54,7 @@ export default function TextareaWidget<
4254
<Input.TextArea
4355
disabled={disabled || (readonlyAsDisabled && readonly)}
4456
id={id}
45-
name={id}
57+
name={htmlName || id}
4658
onBlur={!readonly ? handleBlur : undefined}
4759
onChange={!readonly ? handleChange : undefined}
4860
onFocus={!readonly ? handleFocus : undefined}

packages/chakra-ui/src/BaseInputTemplate/BaseInputTemplate.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default function BaseInputTemplate<
2121
>(props: BaseInputTemplateProps<T, S, F>) {
2222
const {
2323
id,
24+
htmlName,
2425
type,
2526
value,
2627
label,
@@ -60,7 +61,7 @@ export default function BaseInputTemplate<
6061
>
6162
<Input
6263
id={id}
63-
name={id}
64+
name={htmlName || id}
6465
value={value || value === 0 ? value : ''}
6566
onChange={onChangeOverride || _onChange}
6667
onBlur={_onBlur}

packages/chakra-ui/src/CheckboxWidget/CheckboxWidget.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function CheckboxWidget<
2323
>(props: WidgetProps<T, S, F>) {
2424
const {
2525
id,
26+
htmlName,
2627
value,
2728
disabled,
2829
readonly,
@@ -66,7 +67,7 @@ export default function CheckboxWidget<
6667
)}
6768
<Checkbox
6869
id={id}
69-
name={id}
70+
name={htmlName || id}
7071
checked={typeof value === 'undefined' ? false : value}
7172
disabled={disabled || readonly}
7273
onCheckedChange={_onChange}

packages/chakra-ui/src/CheckboxesWidget/CheckboxesWidget.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function CheckboxesWidget<
2222
>(props: WidgetProps<T, S, F>) {
2323
const {
2424
id,
25+
htmlName,
2526
disabled,
2627
options,
2728
value,
@@ -70,7 +71,7 @@ export default function CheckboxesWidget<
7071
<Checkbox
7172
key={index}
7273
id={optionId(id, index)}
73-
name={id}
74+
name={htmlName || id}
7475
value={String(index)}
7576
disabled={disabled || itemDisabled || readonly}
7677
onBlur={_onBlur}

0 commit comments

Comments
 (0)