Skip to content

Commit

Permalink
Fix: Chakra-ui SelectWidget show proper selection
Browse files Browse the repository at this point in the history
Fixes: rjsf-team#3422 by using the selected value for the single selection
- Updated the `@rjsf/chakra-ui` `SelectWidget` to properly use the selected value rather than a hard-coded `0` value
- Updated the `CHANGELOG.md` accordingly
  • Loading branch information
heath-freenome committed Feb 3, 2023
1 parent 7b91e1e commit ea3cd1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
should change the heading of the (upcoming) version to include a major version bump.
-->
# 5.0.3

## @rjsf/chakra-ui
- Fixed the `SelectWidget` to allow the proper display of the selected value, fixing [#3422](https://github.com/rjsf-team/react-jsonschema-form/issues/3422)

# 5.0.2

## @rjsf/utils
Expand Down
18 changes: 12 additions & 6 deletions packages/chakra-ui/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormControl, FormLabel } from "@chakra-ui/react";
import {
ariaDescribedByIds,
EnumOptionsType,
enumOptionsIndexForValue,
enumOptionsValueForIndex,
FormContextType,
RJSFSchema,
Expand Down Expand Up @@ -70,7 +71,7 @@ export default function SelectWidget<
)
? enumOptions.map((option: EnumOptionsType<S>, index: number) => {
const { value, label } = option;
_valueLabelMap[index] = label;
_valueLabelMap[index] = label || String(value);
return {
label,
value: String(index),
Expand All @@ -81,16 +82,21 @@ export default function SelectWidget<
: [];

const isMultiple = typeof multiple !== "undefined" && Boolean(enumOptions);
const selectedIndex = enumOptionsIndexForValue<S>(
value,
enumOptions,
isMultiple
);
const formValue: any = isMultiple
? (value || []).map((v: any, index: number) => {
? (selectedIndex as string[] || []).map((i: string) => {
return {
label: _valueLabelMap[index] || v,
value: index,
label: _valueLabelMap[i],
value: i,
};
})
: {
label: _valueLabelMap[0] || value || "",
value: 0,
label: _valueLabelMap[selectedIndex as string] || "",
selectedIndex,
};

return (
Expand Down
13 changes: 5 additions & 8 deletions packages/chakra-ui/test/__snapshots__/Form.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2503,7 +2503,7 @@ exports[`single fields select field 1`] = `
<div
className=" emotion-7"
>
foo
</div>
<div
className=" emotion-8"
Expand Down Expand Up @@ -2560,7 +2560,6 @@ exports[`single fields select field 1`] = `
<input
name="root"
type="hidden"
value={0}
/>
</div>
</div>
Expand Down Expand Up @@ -3646,12 +3645,12 @@ exports[`single fields select field multiple choice formData 1`] = `
<input
name="root"
type="hidden"
value={0}
value="0"
/>
<input
name="root"
type="hidden"
value={1}
value="1"
/>
</div>
</div>
Expand Down Expand Up @@ -4238,7 +4237,7 @@ exports[`single fields select field single choice enumDisabled 1`] = `
<div
className=" emotion-7"
>
foo
</div>
<div
className=" emotion-8"
Expand Down Expand Up @@ -4295,7 +4294,6 @@ exports[`single fields select field single choice enumDisabled 1`] = `
<input
name="root"
type="hidden"
value={0}
/>
</div>
</div>
Expand Down Expand Up @@ -4557,7 +4555,7 @@ exports[`single fields select field single choice formData 1`] = `
<div
className=" emotion-7"
>
foo
bar
</div>
<div
className=" emotion-8"
Expand Down Expand Up @@ -4614,7 +4612,6 @@ exports[`single fields select field single choice formData 1`] = `
<input
name="root"
type="hidden"
value={0}
/>
</div>
</div>
Expand Down

0 comments on commit ea3cd1f

Please sign in to comment.