From 1acafdd1f273bccc1604773eed60b275bbdf1be8 Mon Sep 17 00:00:00 2001 From: unix Date: Thu, 2 Apr 2020 05:20:35 +0800 Subject: [PATCH] fix: rename defaultChecked to initialChecked feat(toggle): add component --- components/checkbox/checkbox.tsx | 12 ++++++------ components/index.ts | 1 + pages/docs/components/checkbox.mdx | 19 +++++++++++++++++-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/components/checkbox/checkbox.tsx b/components/checkbox/checkbox.tsx index 811f66e..7d44766 100644 --- a/components/checkbox/checkbox.tsx +++ b/components/checkbox/checkbox.tsx @@ -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, keyof Props> export type CheckboxProps = Props & typeof defaultProps & NativeAttrs const Checkbox: React.FC = 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(initialChecked) const { updateState, inGroup, disabledAll, values } = useCheckbox() const isDisabled = inGroup ? disabledAll || disabled : disabled @@ -83,9 +82,10 @@ const Checkbox: React.FC = React.memo(({ }, [updateState, onChange, isDisabled, selfChecked]) useEffect(() => { + if (checked === undefined) return setSelfChecked(checked) }, [checked]) - + return (