Skip to content

Commit

Permalink
feat : checkboxGroup 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
GulSam00 committed Jul 2, 2023
1 parent 74eca90 commit e87547b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/Checkbox/Checkbox.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Story = StoryObj<Component>;
export const Primary: Story = {
// Add your story args here
args: {
labelText: 'label',
children: 'label',
checked: false,
disabled: false,
},
Expand Down
20 changes: 15 additions & 5 deletions src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { ChangeEvent } from 'react';
import { ChangeEvent, useContext } from 'react';
import cx from 'classnames';
import { twMerge } from 'tailwind-merge';
import CheckIcon from '@material-design-icons/svg/outlined/check.svg';
import { CheckboxContext } from './CheckboxGroup';

import { Text } from '../Text';

interface Props {
labelText: string;
children: string;
checked?: boolean;
disabled?: boolean;
value: string;
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
}

export function Checkbox({ labelText, checked = false, disabled = false, onChange }: Props) {
export function Checkbox({ children, checked = false, disabled = false, value, onChange }: Props) {

const context = useContext(CheckboxContext);

if (context) {
checked = context.selectedValue.includes(value);
onChange = context.onChange;
}

return (
<label className={cx('flex flex-row items-center gap-2', { 'opacity-[0.3]': disabled })}>
<input type="checkbox" className="hidden" checked={checked} onChange={onChange} />
<input type="checkbox" className="hidden" checked={checked} value={value} onChange={onChange} />
<div
className={cx(
' flex h-[20px] w-[20px] items-center justify-center rounded-md border-2 border-border-primary transition-colors duration-200 hover:border-color-system_200 dark:border-border-primary_dark dark:hover:border-color-system_200',
Expand All @@ -31,7 +41,7 @@ export function Checkbox({ labelText, checked = false, disabled = false, onChang
)}
/>
</div>
<Text text={labelText} size="body2" weight="regular" />
<Text text={children} size="body2" weight="regular" />
</label>
);
}
40 changes: 40 additions & 0 deletions src/components/Checkbox/CheckboxGroup.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Meta, StoryObj } from '@storybook/react';
import { CheckboxGroup } from './CheckboxGroup';
import { Checkbox } from './Checkbox';
import { Children } from 'react';

type Component = typeof CheckboxGroup;
const meta: Meta<Component> = {
title: 'rookies/CheckboxGroup',
component: CheckboxGroup,
};

type Story = StoryObj<Component>;

export const Primary: Story = {
args: {
selectedValue: ["label1", "label3"],
children: (
<>
<Checkbox value="label1">children</Checkbox>
<Checkbox value="label2">children</Checkbox>
<Checkbox value="label3">children</Checkbox>
</>
),
},
};

export const WithDisabled: Story = {
args: {
selectedValue: ["label1", "label3"],
children: (
<>
<Checkbox value="label1">children</Checkbox>
<Checkbox value="label2">children</Checkbox>
<Checkbox value="label3" disabled>children</Checkbox>
</>
),
},
};

export default meta;
19 changes: 19 additions & 0 deletions src/components/Checkbox/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ReactNode, ChangeEventHandler, createContext } from 'react';

interface Props {
children: ReactNode;
selectedValue: string[];
onChange?: ChangeEventHandler<HTMLInputElement>;
}

type CheckboxContextType = Omit<Props, 'children'>;

export const CheckboxContext = createContext<CheckboxContextType | null>(null);

export function CheckboxGroup({ children, ...restProps }: Props) {
return (
<CheckboxContext.Provider value={{ ...restProps }}>
<fieldset className="flex flex-col gap-2">{children}</fieldset>
</CheckboxContext.Provider>
);
}
1 change: 1 addition & 0 deletions src/components/Checkbox/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { Checkbox } from './Checkbox';
export { CheckboxGroup } from './CheckboxGroup';

1 comment on commit e87547b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alpha-115 - e87547b

check the deploy status here

Check it out!

Or you can try...

yarn add @rookies-team/design@https://pages.rookies.kr/design/0.21.0-alpha115-e87547b/rookies-team-design-v0.21.0-alpha115-e87547b.tgz

Please sign in to comment.