Skip to content

Commit

Permalink
fix(RadioGroup): support for initialValue prop (#92) (#93)
Browse files Browse the repository at this point in the history
fixes #92
  • Loading branch information
MiroslavPetrik authored Jan 18, 2024
1 parent 6e787ea commit f08db15
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
19 changes: 18 additions & 1 deletion src/components/radio-group/RadioGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,24 @@ export const RequiredNumber = radioGroupStory({
options: ratingOptions,
getValue: (value) => value,
getLabel: (value) => Array(value + 1).join("⭐"),
// label="How do you like the RadioGroup component?"
},
});

export const InitializedNumber = radioGroupStory({
parameters: {
docs: {
description: {
story:
"The `initialValue` initializes the field, accepting values returned from the `getValue` prop.",
},
},
},
args: {
field: numberField(),
initialValue: 4,
options: ratingOptions,
getValue: (value) => value,
getLabel: (value) => Array(value + 1).join("⭐"),
},
});

Expand Down
23 changes: 23 additions & 0 deletions src/components/radio-group/RadioGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,27 @@ describe("<RadioGroup />", () => {
expect(onSubmit).toHaveBeenCalledWith({ field: "some" });
});
});

describe("initialValue prop", () => {
it("sets the field value", async () => {
const props = {
field: stringField(),
options: ["none", "some", "all"],
getLabel: (val: string) => val,
getValue: (val: string) => val,
};
const form = formAtom({ field: props.field });
const { result } = renderHook(() => useFormSubmit(form));
render(<RadioGroup {...props} initialValue="some" />);

expect(screen.getByLabelText("some")).toBeChecked();

const onSubmit = vi.fn();
await act(async () => {
result.current(onSubmit)();
});

expect(onSubmit).toHaveBeenCalledWith({ field: "some" });
});
});
});
25 changes: 14 additions & 11 deletions src/components/radio-group/RadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import {
SelectField,
SelectFieldProps,
UseOptionsProps,
UseSelectFieldProps,
useOptions,
useSelectFieldProps,
} from "../../hooks";

export type RadioGroupProps<Option, Field extends SelectField> = Omit<
UseOptionsProps<Option>,
"field"
> &
UseSelectFieldProps<Option, Field>;
export type RadioGroupProps<
Option,
Field extends SelectField,
> = SelectFieldProps<Option, Field> & UseOptionsProps<Option>;

export const RadioGroup = <Option, Field extends SelectField>({
field,
options,
getValue,
getLabel,
initialValue,
}: RadioGroupProps<Option, Field>) => {
/**
* ref for multiple inputs not needed.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { ref, ...props } = useSelectFieldProps({
field,
options,
getValue,
});
const { ref, ...props } = useSelectFieldProps(
{
field,
options,
getValue,
},
{ initialValue },
);

const { renderOptions } = useOptions({ field, options, getLabel });

Expand Down

0 comments on commit f08db15

Please sign in to comment.