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 { ActiveCheckboxType, CheckboxGroupProps } from './CheckboxGroup'
import { CheckboxGroupProps } from './CheckboxGroup'
export type CheckboxGroupContextType = {
activeCheckbox?: ActiveCheckboxType | null
setActiveCheckbox: (value: ActiveCheckboxType) => void
size?: CheckboxGroupProps['size']
}

View File

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