refactor: refactor checkbox group component

This commit is contained in:
jinhojang6 2023-03-07 02:34:53 +09:00 committed by Jinho Jang
parent 5063763727
commit b181444d6c
2 changed files with 5 additions and 21 deletions

View File

@ -1,10 +1,7 @@
import React from 'react' import React from 'react'
import { ActiveCheckboxType, CheckboxGroupProps } from './CheckboxGroup' import { CheckboxGroupProps } from './CheckboxGroup'
export type CheckboxGroupContextType = { export type CheckboxGroupContextType = {
activeCheckbox?: ActiveCheckboxType | null
setActiveCheckbox: (value: ActiveCheckboxType) => void
size?: CheckboxGroupProps['size'] size?: CheckboxGroupProps['size']
} }

View File

@ -1,5 +1,5 @@
import clsx from 'clsx' import clsx from 'clsx'
import React, { useEffect, useRef, useState } from 'react' import React from 'react'
import { CheckboxGroupContext } from './CheckboxGroup.context' import { CheckboxGroupContext } from './CheckboxGroup.context'
import { checkboxGroupClasses } from './CheckboxGroup.classes' import { checkboxGroupClasses } from './CheckboxGroup.classes'
import { Typography } from '../Typography' import { Typography } from '../Typography'
@ -7,29 +7,16 @@ import { Typography } from '../Typography'
export type ActiveCheckboxType = string | number | readonly string[] export type ActiveCheckboxType = string | number | readonly string[]
export type CheckboxGroupProps = React.HTMLAttributes<HTMLDivElement> & { export type CheckboxGroupProps = React.HTMLAttributes<HTMLDivElement> & {
activeCheckbox?: ActiveCheckboxType | null
size?: 'small' | 'medium' | 'large' size?: 'small' | 'medium' | 'large'
label?: string label?: string
} }
export const CheckboxGroup: React.FC<CheckboxGroupProps> & { export const CheckboxGroup: React.FC<CheckboxGroupProps> & {
classes: typeof checkboxGroupClasses classes: typeof checkboxGroupClasses
} = ({ size = 'large', label, activeCheckbox, children, ...props }) => { } = ({ size = 'large', label, children, ...props }) => {
const ref = useRef<HTMLDivElement>(null)
const [value, setValue] = useState(activeCheckbox)
const setActiveCheckbox = (radioButton: ActiveCheckboxType) => {
setValue(radioButton)
}
useEffect(() => setValue(activeCheckbox), [activeCheckbox])
return ( return (
<CheckboxGroupContext.Provider <CheckboxGroupContext.Provider value={{ size }}>
value={{ activeCheckbox: value, setActiveCheckbox, size }}
>
<div <div
ref={ref}
{...props} {...props}
className={clsx(props.className, checkboxGroupClasses.root)} className={clsx(props.className, checkboxGroupClasses.root)}
> >
@ -38,7 +25,7 @@ export const CheckboxGroup: React.FC<CheckboxGroupProps> & {
variant={size === 'small' ? 'label2' : 'label1'} variant={size === 'small' ? 'label2' : 'label1'}
className={checkboxGroupClasses.label} className={checkboxGroupClasses.label}
> >
{label && label} {label}
</Typography> </Typography>
{children} {children}
</div> </div>