Skip to content

Commit

Permalink
refactor: simplify the judge
Browse files Browse the repository at this point in the history
  • Loading branch information
1587315093 committed Jul 21, 2023
1 parent 042c218 commit b52a32b
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/components/collapse/collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,41 @@ export const Collapse: FC<CollapseProps> = props => {
panels.push(child)
})

const [activeKey, setActiveKey] = usePropsValue<string[]>(
props.accordion
? {
value: {
[props.activeKey as string]: [props.activeKey as string],
null: [],
undefined: undefined,
}[props.activeKey as string],
defaultValue: [null, undefined].includes(
props.defaultActiveKey as null | undefined
)
? []
: [props.defaultActiveKey as string],
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) {
initValue.value = [props.activeKey]

Check warning on line 154 in src/components/collapse/collapse.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/collapse/collapse.tsx#L154

Added line #L154 was not covered by tests
} else if (props.activeKey === undefined) {
initValue.value = undefined
}

if (props.defaultActiveKey) {
initValue.defaultValue = [props.defaultActiveKey]
}

return initValue
}

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

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

0 comments on commit b52a32b

Please sign in to comment.