fix: rename defaultChecked to initialChecked

feat(toggle): add component
This commit is contained in:
unix
2020-04-02 05:20:35 +08:00
parent f5ae55c772
commit 1acafdd1f2
3 changed files with 24 additions and 8 deletions

View File

@@ -18,16 +18,15 @@ export interface CheckboxEvent {
interface Props {
checked?: boolean
disabled?: boolean
defaultChecked?: boolean
initialChecked?: boolean
onChange?: (e: CheckboxEvent) => void
className?: string
value?: string
}
const defaultProps = {
checked: false,
disabled: false,
defaultChecked: false,
initialChecked: false,
className: '',
value: '',
}
@@ -36,9 +35,9 @@ type NativeAttrs = Omit<React.LabelHTMLAttributes<any>, keyof Props>
export type CheckboxProps = Props & typeof defaultProps & NativeAttrs
const Checkbox: React.FC<CheckboxProps> = React.memo(({
checked, defaultChecked, disabled, onChange, className, children, value, ...props
checked, initialChecked, disabled, onChange, className, children, value, ...props
}) => {
const [selfChecked, setSelfChecked] = useState(defaultChecked)
const [selfChecked, setSelfChecked] = useState<boolean>(initialChecked)
const { updateState, inGroup, disabledAll, values } = useCheckbox()
const isDisabled = inGroup ? disabledAll || disabled : disabled
@@ -83,9 +82,10 @@ const Checkbox: React.FC<CheckboxProps> = React.memo(({
}, [updateState, onChange, isDisabled, selfChecked])
useEffect(() => {
if (checked === undefined) return
setSelfChecked(checked)
}, [checked])
return (
<label className={`${className}`} {...props}>
<CheckboxIcon disabled={isDisabled} checked={selfChecked} />

View File

@@ -43,3 +43,4 @@ export { default as Collapse } from './collapse'
export { default as Loading } from './loading'
export { default as Textarea } from './textarea'
export { default as Table } from './table'
export { default as Toggle } from './toggle'