mirror of
https://github.com/acid-info/lsd.git
synced 2025-01-27 01:09:59 +00:00
fix: fix Collapse component's controlled state issue
This commit is contained in:
parent
7dd6c8083e
commit
bf40a03e4c
@ -15,13 +15,20 @@ export type CollapseProps = CommonProps &
|
||||
|
||||
export const Collapse: React.FC<CollapseProps> & {
|
||||
classes: typeof collapseClasses
|
||||
} = ({ label, disabled = false, size = 'large', children, ...props }) => {
|
||||
} = ({
|
||||
label,
|
||||
disabled = false,
|
||||
size = 'large',
|
||||
open: openProp,
|
||||
children,
|
||||
...props
|
||||
}) => {
|
||||
const globalProps = useCommonProps(props)
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const [open, setOpen] = useState(props.open ?? false)
|
||||
const [open, setOpen] = useState(openProp ?? false)
|
||||
|
||||
const handleChange = (value: boolean) => {
|
||||
if (typeof props.open === 'undefined') return setOpen(value)
|
||||
if (typeof openProp === 'undefined') return setOpen(value)
|
||||
props.onChange && props.onChange(value)
|
||||
}
|
||||
|
||||
@ -31,6 +38,10 @@ export const Collapse: React.FC<CollapseProps> & {
|
||||
disabled && open && handleChange(false)
|
||||
}, [disabled, open, handleChange])
|
||||
|
||||
useEffect(() => {
|
||||
typeof openProp !== 'undefined' && setOpen(openProp)
|
||||
}, [openProp])
|
||||
|
||||
return (
|
||||
<div
|
||||
{...props}
|
||||
|
Loading…
x
Reference in New Issue
Block a user