Skip to content

Commit

Permalink
refactor: to simplify the judge (#6263)
Browse files Browse the repository at this point in the history
* refactor: to simplify the judge

* refactor: simplify the judge

* fix: add null judge

* fix: add judage case
  • Loading branch information
1587315093 authored Jul 28, 2023
1 parent ace65c1 commit e2f82a6
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions src/components/collapse/collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,44 @@ export const Collapse: FC<CollapseProps> = props => {
panels.push(child)
})

const [activeKey, setActiveKey] = usePropsValue<string[]>(
props.accordion
? {
value:
props.activeKey === undefined
? undefined
: props.activeKey === null
? []
: [props.activeKey],
defaultValue:
props.defaultActiveKey === undefined ||
props.defaultActiveKey === null
? []
: [props.defaultActiveKey],
onChange: v => {
props.onChange?.(v[0] ?? null)
},
}
: {
value: props.activeKey,
defaultValue: props.defaultActiveKey ?? [],
onChange: props.onChange,
}
)
const handlePropsValue = () => {
if (!props.accordion) {
return {
value: props.activeKey,
defaultValue: props.defaultActiveKey ?? [],
onChange: props.onChange,
}
}

const initValue: {
value?: string[]
defaultValue: string[]
onChange: (v: string[]) => void
} = {
value: [],
defaultValue: [],
onChange: v => {
props.onChange?.(v[0] ?? null)
},
}

if (props.activeKey === undefined) {
initValue.value = undefined
} else if (props.activeKey !== null) {
initValue.value = [props.activeKey]
}

if (
![null, undefined].includes(props.defaultActiveKey as null | undefined)
) {
initValue.defaultValue = [props.defaultActiveKey as string]
}

return initValue
}

const [activeKey, setActiveKey] = usePropsValue<string[]>(handlePropsValue())

const activeKeyList =
activeKey === null ? [] : Array.isArray(activeKey) ? activeKey : [activeKey]

Expand Down

0 comments on commit e2f82a6

Please sign in to comment.