Skip to content

Commit

Permalink
♻️ - refactor: adjust the types returned by radio input an inputs wit…
Browse files Browse the repository at this point in the history
…h multiple items with various types
  • Loading branch information
svenvandescheur committed Jul 2, 2024
1 parent 46d8cd4 commit cd2d329
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 118 deletions.
119 changes: 51 additions & 68 deletions src/components/form/form/form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ export const FormComponent: Story = {
fields: [
{ label: "First name", name: "first_name", required: true },
{ label: "Last name", name: "last_name", required: true },
{ label: "Age", name: "age", type: "number", required: true },
{ label: "Address", name: "address", required: true },
{ label: "Address (addition)", name: "address", required: true },
{
label: "Address (addition)",
name: "address",
type: "number",
required: true,
},
{ label: "Date of birth", name: "date_of_birth", type: "date" },
{
label: "Select school year",
Expand Down Expand Up @@ -62,6 +68,12 @@ export const FormComponent: Story = {
{ label: "No", value: "no" },
],
},
{
label: "I have read and accept the terms and conditions",
name: "accept_tos",
type: "checkbox",
required: true,
},
],
validate: validateForm,
validateOnChange: true,
Expand All @@ -70,19 +82,28 @@ export const FormComponent: Story = {
const canvas = within(canvasElement);
const firstName = canvas.getByLabelText("First name");
const lastName = canvas.getByLabelText("Last name");
const age = canvas.getByLabelText("Age");
const schoolYear = canvas.getByLabelText("Select school year");
const address = canvas.getByLabelText("Address");
const address_addition = canvas.getByLabelText("Address (addition)");
const dateOfBirth = canvas.getByLabelText("Date of birth");
const english = canvas.getByLabelText("English");
const math = canvas.getByLabelText("Math");
const yes = canvas.getByLabelText("Yes");
const no = canvas.getByLabelText("No");
const acceptTos = canvas.getByLabelText(
"I have read and accept the terms and conditions",
);

await userEvent.clear(firstName);
await userEvent.type(firstName, "John", { delay: 10 });

await userEvent.clear(lastName);
await userEvent.type(lastName, "Doe", { delay: 10 });

await userEvent.clear(age);
await userEvent.type(age, "33", { delay: 10 });

await userEvent.click(schoolYear, { delay: 10 });
const junior = await canvas.findByText("Junior");
await userEvent.click(junior, { delay: 10 });
Expand All @@ -97,86 +118,44 @@ export const FormComponent: Story = {
await userEvent.type(dateOfBirth, "2023-09-15", { delay: 10 });
await userEvent.type(dateOfBirth, "{enter}");

await userEvent.click(schoolYear);
const senior = await canvas.findByText("Senior");
await userEvent.click(senior);

await userEvent.click(english, { delay: 10 });
await userEvent.click(math, { delay: 10 });

await userEvent.click(yes, { delay: 10 });
await userEvent.click(no, { delay: 10 });

await userEvent.click(acceptTos, { delay: 10 });
},
};

export const TypedResults: Story = {
...FormComponent,
args: {
debug: true,
fields: [
{ label: "Name", name: "name" },
{ label: "Age", name: "age", type: "number" },
{
label: "Select school year",
name: "school_year",
options: [
{ label: "Freshman" },
{ label: "Sophomore" },
{ label: "Junior" },
{ label: "Senior" },
{ label: "Graduate" },
],
},
{
label: "Select courses",
name: "courses",
type: "checkbox",
required: true,
options: [
{ label: "English", value: "english" },
{ label: "Math", value: "math" },
{ label: "Science", value: "science" },
],
},
{
label: "Receive newsletter",
name: "subscribe_newsletter",
type: "radio",
required: true,
options: [
{ label: "Yes", value: "yes" },
{ label: "No", value: "no" },
],
},
],
...FormComponent.args,
useTypedResults: true,
validate: validateForm,
validateOnChange: true,
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const name = canvas.getByLabelText("Name");
const age = canvas.getByLabelText("Age");
const schoolYear = canvas.getByLabelText("Select school year");
const english = canvas.getByLabelText("English");
const math = canvas.getByLabelText("Math");
const newsletter = canvas.getByLabelText("Receive newsletter");

await userEvent.clear(name);
await userEvent.type(name, "John", { delay: 10 });

await userEvent.clear(age);
await userEvent.type(age, "33", { delay: 10 });

await userEvent.click(schoolYear, { delay: 10 });
const junior = await canvas.findByText("Junior");
await userEvent.click(junior, { delay: 10 });

await userEvent.click(english, { delay: 10 });
await userEvent.click(math, { delay: 10 });
await userEvent.click(newsletter, { delay: 10 });
},
};

export const UsageWithFormik: Story = {
...FormComponent,
args: {
...FormComponent.args,
debug: true,
fields: [
{ label: "First name", name: "first_name", required: true },
{ label: "Last name", name: "last_name", required: true },
{ label: "Age", name: "age", type: "number", required: true },
{ label: "Address", name: "address[0]", required: true },
{
label: "Address (addition)",
name: "address[1]",
type: "number",
required: true,
},
{ label: "Date of birth", name: "date_of_birth", type: "date" },
{
label: "Select school year",
name: "school_year",
Expand All @@ -189,10 +168,6 @@ export const UsageWithFormik: Story = {
{ label: "Graduate" },
],
},
{ label: "Address", name: "address[0]", required: true },
{ label: "Address (addition)", name: "address[1]", required: true },
{ label: "Date of birth", name: "date_of_birth", type: "date" },

{
label: "Select courses",
name: "courses",
Expand All @@ -214,7 +189,15 @@ export const UsageWithFormik: Story = {
{ label: "No", value: "no" },
],
},
{
label: "I have read and accept the terms and conditions",
name: "accept_tos",
type: "checkbox",
required: true,
},
],
validate: validateForm,
validateOnChange: true,
},
render: (args) => {
return (
Expand Down
11 changes: 8 additions & 3 deletions src/components/form/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ export const Input: React.FC<InputProps> = ({
// This conditionalizes the presence of the "value" props.
const valueProps =
type.toLowerCase() === "checkbox" || type.toLowerCase() === "radio"
? { checked: props.checked, value: valueState }
: { value: valueState };

? typeof value === "undefined"
? {}
: {
value: value,
}
: {
value: valueState,
};
const input = (
<input
ref={inputRef}
Expand Down
2 changes: 1 addition & 1 deletion src/components/form/radio/radio.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const RadioComponent: Story = {
play: async ({ canvasElement, args }) => {
const canvas = within(canvasElement);
const pre = await canvas.findByRole("log");
const input = canvas.getByLabelText(args.children);
const input = canvas.getByLabelText(args.children as string);

// On
await userEvent.click(input);
Expand Down
10 changes: 3 additions & 7 deletions src/components/form/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ export type RadioProps = InputProps & {
* @param props
* @constructor
*/
export const Radio: React.FC<RadioProps> = ({
children,
value,
checked,
...props
}) => {
export const Radio: React.FC<RadioProps> = ({ children, value, ...props }) => {
const id = useId();
const _id = props.id || id;
const _props = value ? { ...props, value } : props;
return (
<div className="mykn-radio">
<Input id={_id} {...props} type="radio" value={value} checked={checked} />
<Input id={_id} {..._props} type="radio" />
{children && (
<Label bold={false} htmlFor={_id}>
{children}
Expand Down
4 changes: 2 additions & 2 deletions src/components/form/radio/radiogroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const RadioGroupComponent: Story = {

data = JSON.parse(pre?.textContent || "{}");

await expect(data.school_year[0]).toEqual("Graduate");
await expect(data.school_year).toEqual("Graduate");

await inputs
.filter((_, i) => i % 2 !== 0)
Expand All @@ -53,6 +53,6 @@ export const RadioGroupComponent: Story = {
);

data = JSON.parse(pre?.textContent || "{}");
await expect(data.school_year[0]).toEqual("Senior");
await expect(data.school_year).toEqual("Senior");
},
};
10 changes: 5 additions & 5 deletions src/components/form/radio/radiogroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ export const RadioGroup: React.FC<RadioGroupProps> = ({
}) => {
const reactId = useId();
const _id = id || reactId;
const [selectedValue, setSelectedValue] = useState<
const [selectedValueState, setSelectedValueState] = useState<
string | number | undefined
>(options.find((option) => option.selected)?.value);
>();

useEffect(() => {
const initialSelected = options.find((option) => option.selected)?.value;
if (initialSelected !== undefined) {
setSelectedValue(initialSelected);
setSelectedValueState(initialSelected);
}
}, [options]);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSelectedValue(event.target.value);
setSelectedValueState(event.target.value);
onChange?.(event);
};

Expand All @@ -46,7 +46,7 @@ export const RadioGroup: React.FC<RadioGroupProps> = ({
name={name}
value={optionValue}
variant={variant}
checked={selectedValue === optionValue}
checked={selectedValueState === optionValue}
onChange={handleChange}
>
{option.label}
Expand Down
Loading

0 comments on commit cd2d329

Please sign in to comment.