Skip to content

Commit ddabac6

Browse files
authored
fix: Make sure the actual value is set in radio groups
Fixes the React Vanilla and React Material radio groups to set the value handed over via the options instead of retrieving it from the change handler. Thereby we avoid setting stringified conversions of the values.
1 parent 4eff17b commit ddabac6

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ In this case, you can try to clean the repository with `git clean -dfx`. Beware
5151
- Build (all packages): `pnpm run build`
5252
- Test (all packages): `pnpm run test`
5353
- Clean (delete `dist` folder of all packages): `pnpm run clean`
54-
- Run React Vanilla examples: `cd packages/vanilla && pnpm run dev`
55-
- Run React Material examples: `cd packages/material && pnpm run dev`
54+
- Run React Vanilla examples: `cd packages/vanilla-renderers && pnpm run dev`
55+
- Run React Material examples: `cd packages/material-renderers && pnpm run dev`
5656
- Run Angular Material examples: `cd packages/angular-material && pnpm run dev`
5757
- Run Vue Vanilla dev setup: `cd packages/vue-vanilla && pnpm run serve`
5858

packages/material-renderers/src/controls/MaterialRadioGroup.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export const MaterialRadioGroup = (props: ControlProps & OwnPropsOfEnum) => {
6565
focused,
6666
appliedUiSchemaOptions.showUnfocusedDescription
6767
);
68-
const onChange = (_ev: any, value: any) => handleChange(path, value);
6968

7069
return (
7170
<Hidden xsUp={!visible}>
@@ -87,12 +86,17 @@ export const MaterialRadioGroup = (props: ControlProps & OwnPropsOfEnum) => {
8786
{label}
8887
</FormLabel>
8988

90-
<RadioGroup value={props.data ?? ''} onChange={onChange} row={true}>
89+
<RadioGroup value={props.data ?? ''} row={true}>
9190
{options.map((option) => (
9291
<FormControlLabel
9392
value={option.value}
9493
key={option.label}
95-
control={<Radio checked={data === option.value} />}
94+
control={
95+
<Radio
96+
checked={data === option.value}
97+
onChange={() => handleChange(path, option.value)}
98+
/>
99+
}
96100
label={option.label}
97101
disabled={!enabled}
98102
/>

packages/vanilla-renderers/rollup.example.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import typescript from 'rollup-plugin-typescript2';
1010
* @type {import('rollup').RollupOptions}
1111
*/
1212
const config = {
13-
input: 'example/index.ts',
13+
input: 'example/index.tsx',
1414
output: {
1515
file: 'example/dist/bundle.js',
1616
format: 'iife',

packages/vanilla-renderers/src/controls/RadioGroup.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export const RadioGroup = ({
111111
id={option.value}
112112
name={id}
113113
checked={data === option.value}
114-
onChange={(ev) => handleChange(path, ev.currentTarget.value)}
114+
onChange={(_) => handleChange(path, option.value)}
115115
disabled={!enabled}
116116
className={radioInput}
117117
/>

packages/vanilla-renderers/test/renderers/RadioGroupControl.test.tsx

+39
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,43 @@ describe('Radio group control', () => {
215215
expect(currentlyChecked).toHaveLength(1);
216216
expect((currentlyChecked.getDOMNode() as HTMLInputElement).value).toBe('B');
217217
});
218+
219+
test('Radio group should preserve option type', () => {
220+
const renderers = [
221+
{ tester: rankWith(10, isEnumControl), renderer: RadioGroupControl },
222+
];
223+
const schema = {
224+
type: 'object',
225+
properties: {
226+
amount: {
227+
type: 'integer',
228+
enum: [50, 100, 250, 500],
229+
},
230+
},
231+
};
232+
const uischema = {
233+
type: 'Control',
234+
scope: '#/properties/amount',
235+
options: {
236+
format: 'radio',
237+
},
238+
};
239+
const core = initCore(schema, uischema, fixture.data);
240+
wrapper = mount(
241+
<JsonFormsStateProvider initState={{ core, renderers }}>
242+
<RadioGroupControl schema={schema} uischema={uischema} />
243+
</JsonFormsStateProvider>
244+
);
245+
246+
// Simulate a click event to change the selection
247+
const radioInput = wrapper.find('input[value=100]');
248+
radioInput.simulate('change');
249+
250+
const currentlyChecked = wrapper.find('input[checked=true]');
251+
expect(currentlyChecked).toHaveLength(1);
252+
253+
// Check the type of the selected radio element's value
254+
const selectedValue = currentlyChecked.props().value;
255+
expect(typeof selectedValue).toBe('number');
256+
});
218257
});

0 commit comments

Comments
 (0)