Skip to content

feat: Implemented RadioGroup component to Inputs group #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Components/Inputs/RadioGroup/RadioGroup.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { faker } from "@faker-js/faker";
import { action } from "@storybook/addon-actions";
import { useState } from "react";

import RadioGroup from ".";
import { OptionValue } from "../Select/types";

export default {
title: "Input/RadioGroup",
component: RadioGroup,
parameters: {
layout: "centered"
},
argTypes: {
onChange: { action: "changed" }
}
};

const dummyRadios = Array(5)
.fill(0)
.map(() => ({
value: Math.floor(Math.random() * 1000000),
label: faker.lorem.words(Math.floor(Math.random() * 10) + 1)
}));

export const Default = () => {
const [value, setValue] = useState<OptionValue>(1);
return (
<RadioGroup
value={value}
onChange={value => {
setValue(value);
action("onChange")(value);
}}
options={dummyRadios}
/>
);
};
55 changes: 55 additions & 0 deletions src/Components/Inputs/RadioGroup/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import classNames from "classnames";
import { FC } from "react";

import InputError from "Components/Inputs/Common/InputError";
import InputHeader from "Components/Inputs/Common/InputHeader";

import { Option, OptionValue } from "../Select/types";
import { IRadioGroup } from "./types";

const RadioGroup: FC<IRadioGroup> = (
props = {
value: "",
onChange: () => null,
options: []
}
) => {
const { value, onChange, inputWrapperClassName, options } = props;

const handleChange = (value: OptionValue) => {
if (!onChange) {
throw new Error("💣 Input is missing onChange handler 💣");
}

onChange?.(value);
};

return (
<div className={classNames(inputWrapperClassName)}>
<InputHeader {...props} />
<div className='form-control items-start yl-flex yl-gap-2 yl-flex-wrap'>
{options?.map((option, index) => (
<label
className='label yl-flex yl-cursor-pointer yl-items-center yl-gap-2'
key={index}
>
<input
type='radio'
value={option.value}
checked={
options?.findIndex((opt: Option) => opt.value === value) ===
index
}
onChange={() => handleChange(option.value)}
className='yl-cursor-pointer yl-text-primary focus:outline-primary'
/>
<span className='yl-text-primary-text-color'>{option.label}</span>
</label>
))}
</div>
{props.error && <InputError error={props.error} />}
</div>
);
};

export default RadioGroup;
10 changes: 10 additions & 0 deletions src/Components/Inputs/RadioGroup/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Option, OptionValue } from "../Select/types";

export interface IRadioGroup {
name?: string;
value?: OptionValue;
onChange?: (value: OptionValue) => void;
inputWrapperClassName?: string;
error?: string;
options?: Option[];
}
4 changes: 3 additions & 1 deletion src/Components/Inputs/Select/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PropsValue } from "react-select";

export type OptionValue = string | number;

export type Option = {
value: string | number;
value: OptionValue;
label: string;
};

Expand Down
1 change: 1 addition & 0 deletions src/Components/Inputs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { default as AsyncSelect } from "./AsyncSelect";
export { default as Checkbox } from "./Checkbox";
export { default as InputError } from "./Common/InputError";
export { default as RadioGroup } from "./RadioGroup";
export { default as InputHeader } from "./Common/InputHeader";
export { default as DomainInput } from "./DomainInput";
export { default as ImageInput } from "./ImageInput";
Expand Down
2 changes: 2 additions & 0 deletions src/Components/Inputs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as CheckboxTypes from "./Checkbox/types";
import * as CommonTypes from "./Common/types";
import * as DomainInputTypes from "./DomainInput/types";
import * as InputTypes from "./Input/types";
import * as RadioGroupTypes from "./RadioGroup/types";
import * as SelectTypes from "./Select/types";
import * as SwitchTypes from "./Switch/types";
import * as TextareaTypes from "./Textarea/types";
Expand All @@ -13,6 +14,7 @@ export {
CheckboxTypes,
CommonTypes,
DomainInputTypes,
RadioGroupTypes,
InputTypes,
SelectTypes,
SwitchTypes,
Expand Down