Skip to content

Commit

Permalink
fix: Remove duplicate title for SelectWidget and description for Chec…
Browse files Browse the repository at this point in the history
…kboxWidget (#3655)

Fixes #3594
- Updated `@rjsf/antd` to remove the duplicate title for `SelectWidget` and description for `CheckboxWidget`
- Updated the snapshots due to the changes made above
- Updated the `CHANGELOG.md` accordinglt
  • Loading branch information
heath-freenome committed May 10, 2023
1 parent 3186c39 commit 1c045cc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 123 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ should change the heading of the (upcoming) version to include a major version b
## @rjsf/antd

- Fix [#3608](https://github.com/rjsf-team/react-jsonschema-form/issues/3608) by ensuring the root field is always wrapped in Form.Item
- Fix [#3594](https://github.com/rjsf-team/react-jsonschema-form/issues/3594) by removing the duplicate title for `SelectWidget` and description for `CheckboxWidget`

## @rjsf/core

Expand Down
64 changes: 13 additions & 51 deletions packages/antd/src/widgets/CheckboxWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { FocusEvent } from 'react';
import Checkbox, { CheckboxChangeEvent } from 'antd/lib/checkbox';
import {
ariaDescribedByIds,
descriptionId,
getTemplate,
labelValue,
FormContextType,
RJSFSchema,
Expand All @@ -22,29 +20,8 @@ export default function CheckboxWidget<
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(props: WidgetProps<T, S, F>) {
const {
autofocus,
disabled,
formContext,
id,
label,
hideLabel,
onBlur,
onChange,
onFocus,
readonly,
value,
registry,
options,
schema,
uiSchema,
} = props;
const { autofocus, disabled, formContext, id, label, hideLabel, onBlur, onChange, onFocus, readonly, value } = props;
const { readonlyAsDisabled = true } = formContext as GenericObjectType;
const DescriptionFieldTemplate = getTemplate<'DescriptionFieldTemplate', T, S, F>(
'DescriptionFieldTemplate',
registry,
options
);

const handleChange = ({ target }: CheckboxChangeEvent) => onChange(target.checked);

Expand All @@ -58,33 +35,18 @@ export default function CheckboxWidget<
onBlur: !readonly ? handleBlur : undefined,
onFocus: !readonly ? handleFocus : undefined,
};
const description = options.description ?? schema.description;

return (
<>
{!hideLabel && !!description && (
<div>
<DescriptionFieldTemplate
id={descriptionId<T>(id)}
description={description}
schema={schema}
uiSchema={uiSchema}
registry={registry}
/>
</div>
)}
<Checkbox
autoFocus={autofocus}
checked={typeof value === 'undefined' ? false : value}
disabled={disabled || (readonlyAsDisabled && readonly)}
id={id}
name={id}
onChange={!readonly ? handleChange : undefined}
{...extraProps}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{labelValue(label, hideLabel, '')}
</Checkbox>
</>
<Checkbox
autoFocus={autofocus}
checked={typeof value === 'undefined' ? false : value}
disabled={disabled || (readonlyAsDisabled && readonly)}
id={id}
name={id}
onChange={!readonly ? handleChange : undefined}
{...extraProps}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{labelValue(label, hideLabel, '')}
</Checkbox>
);
}
67 changes: 27 additions & 40 deletions packages/antd/src/widgets/SelectWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
ariaDescribedByIds,
enumOptionsIndexForValue,
enumOptionsValueForIndex,
getTemplate,
titleId,
FormContextType,
GenericObjectType,
RJSFSchema,
Expand All @@ -31,22 +29,16 @@ export default function SelectWidget<
disabled,
formContext = {} as F,
id,
label,
hideLabel,
multiple,
onBlur,
onChange,
onFocus,
options,
placeholder,
readonly,
registry,
schema,
uiSchema,
value,
}: WidgetProps<T, S, F>) {
const { readonlyAsDisabled = true } = formContext as GenericObjectType;
const TitleFieldTemplate = getTemplate<'TitleFieldTemplate', T, S, F>('TitleFieldTemplate', registry, options);

const { enumOptions, enumDisabled, emptyValue } = options;

Expand Down Expand Up @@ -74,37 +66,32 @@ export default function SelectWidget<
name: id,
};
return (
<>
{!hideLabel && !!label && (
<TitleFieldTemplate id={titleId<T>(id)} title={label} schema={schema} uiSchema={uiSchema} registry={registry} />
)}
<Select
autoFocus={autofocus}
disabled={disabled || (readonlyAsDisabled && readonly)}
getPopupContainer={getPopupContainer}
id={id}
mode={multiple ? 'multiple' : undefined}
onBlur={!readonly ? handleBlur : undefined}
onChange={!readonly ? handleChange : undefined}
onFocus={!readonly ? handleFocus : undefined}
placeholder={placeholder}
style={SELECT_STYLE}
value={selectedIndexes}
{...extraProps}
filterOption={filterOption}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{Array.isArray(enumOptions) &&
enumOptions.map(({ value: optionValue, label: optionLabel }, index) => (
<Select.Option
disabled={Array.isArray(enumDisabled) && enumDisabled.indexOf(optionValue) !== -1}
key={String(index)}
value={String(index)}
>
{optionLabel}
</Select.Option>
))}
</Select>
</>
<Select
autoFocus={autofocus}
disabled={disabled || (readonlyAsDisabled && readonly)}
getPopupContainer={getPopupContainer}
id={id}
mode={multiple ? 'multiple' : undefined}
onBlur={!readonly ? handleBlur : undefined}
onChange={!readonly ? handleChange : undefined}
onFocus={!readonly ? handleFocus : undefined}
placeholder={placeholder}
style={SELECT_STYLE}
value={selectedIndexes}
{...extraProps}
filterOption={filterOption}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{Array.isArray(enumOptions) &&
enumOptions.map(({ value: optionValue, label: optionLabel }, index) => (
<Select.Option
disabled={Array.isArray(enumDisabled) && enumDisabled.indexOf(optionValue) !== -1}
key={String(index)}
value={String(index)}
>
{optionLabel}
</Select.Option>
))}
</Select>
);
}
32 changes: 0 additions & 32 deletions packages/antd/test/__snapshots__/Array.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2530,14 +2530,6 @@ exports[`with title and description checkboxes 1`] = `
<div
className="ant-form-item-control-input-content"
>
<label
className=""
htmlFor="root__title"
onClick={[Function]}
title="Test field"
>
Test field
</label>
<div
aria-describedby="root__error root__description root__help"
className="ant-select ant-select-in-form-item ant-select-multiple ant-select-show-search"
Expand Down Expand Up @@ -3916,14 +3908,6 @@ exports[`with title and description from both checkboxes 1`] = `
<div
className="ant-form-item-control-input-content"
>
<label
className=""
htmlFor="root__title"
onClick={[Function]}
title="My Field"
>
My Field
</label>
<div
aria-describedby="root__error root__description root__help"
className="ant-select ant-select-in-form-item ant-select-multiple ant-select-show-search"
Expand Down Expand Up @@ -5302,14 +5286,6 @@ exports[`with title and description from uiSchema checkboxes 1`] = `
<div
className="ant-form-item-control-input-content"
>
<label
className=""
htmlFor="root__title"
onClick={[Function]}
title="My Field"
>
My Field
</label>
<div
aria-describedby="root__error root__description root__help"
className="ant-select ant-select-in-form-item ant-select-multiple ant-select-show-search"
Expand Down Expand Up @@ -6634,14 +6610,6 @@ exports[`with title and description with global label off checkboxes 1`] = `
<div
className="ant-form-item-control-input-content"
>
<label
className=""
htmlFor="root__title"
onClick={[Function]}
title="Test field"
>
Test field
</label>
<div
aria-describedby="root__error root__description root__help"
className="ant-select ant-select-in-form-item ant-select-multiple ant-select-show-search"
Expand Down

0 comments on commit 1c045cc

Please sign in to comment.