Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add new prop duplicateKeySuffixSeparator for additionalProperties #3048

Merged
merged 5 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ should change the heading of the (upcoming) version to include a major version b
- Fix `ui:field` with anyOf or oneOf no longer rendered twice (#2890)
- **BREAKING CHANGE** Fixed `anyOf` and `oneOf` getting incorrect, potentially duplicate ids when combined with array (https://github.com/rjsf-team/react-jsonschema-form/issues/2197)
- `formContext` is now passed properly to `SchemaField`, fixes (https://github.com/rjsf-team/react-jsonschema-form/issues/2394, https://github.com/rjsf-team/react-jsonschema-form/issues/2274)
- Added `ui:duplicateKeySuffixSeparator` to customize how duplicate object keys are renamed when using `additionalProperties`.

## @rjsf/antd
- Fix esm build to use `@rollup/plugin-replace` to replace `antd/lib` and `rc-picker/lib` with `antd/es` and `rc-picker/es` respectively, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/2962)
Expand Down
6 changes: 6 additions & 0 deletions docs/api-reference/uiSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ const uiSchema = {
}
};
```

## `duplicateKeySuffixSeparator` option

When using `additionalProperties`, key collision is prevented by appending a unique integer suffix to the duplicate key. For example, when you add a key named `myKey` to a form where `myKey` is already defined, then your new key will become `myKey-1`.
You can use `ui:duplicateKeySuffixSeparator` to override the default separator, `"-"` with a string of your choice.

## Theme Options
[Semantic UI](themes/semantic-ui/uiSchema.md)
[Chakra UI](themes/chakra-ui/uiSchema.md)
5 changes: 4 additions & 1 deletion packages/core/src/components/fields/ObjectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ class ObjectField<T = any, F = any> extends Component<
* @returns - The name of the next available key from `preferredKey`
*/
getAvailableKey = (preferredKey: string, formData: T) => {
const { uiSchema } = this.props;
const { duplicateKeySuffixSeparator = "-" } = getUiOptions<T, F>(uiSchema);

let index = 0;
let newKey = preferredKey;
while (newKey in formData) {
newKey = `${preferredKey}-${++index}`;
newKey = `${preferredKey}${duplicateKeySuffixSeparator}${++index}`;
}
return newKey;
};
Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/ObjectField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,29 @@ describe("ObjectField", () => {
});
});

it("uses a custom separator between the duplicate key name and the suffix", () => {
const formData = {
first: 1,
second: 2,
};
const { node, onChange } = createFormComponent({
schema,
formData,
uiSchema: {
"ui:duplicateKeySuffixSeparator": "_",
},
});

const textNode = node.querySelector("#root_first-key");
Simulate.blur(textNode, {
target: { value: "second" },
});

sinon.assert.calledWithMatch(onChange.lastCall, {
formData: { second: 2, second_1: 1 },
});
});

it("should not attach suffix when input is only clicked", () => {
const formData = {
first: 1,
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,10 @@ type UIOptionsBaseType<T = any, F = any> = Partial<
* to look up an implementation from the `widgets` list or an actual one-off widget implementation itself
*/
widget?: Widget<T, F> | string;
/** When using `additionalProperties`, key collision is prevented by appending a unique integer to the duplicate key.
* This option allows you to change the separator between the original key name and the integer. Default is "-"
*/
duplicateKeySuffixSeparator?: string;
};

/** The type that represents the Options potentially provided by `ui:options` */
Expand Down