mirror of
https://github.com/zhigang1992/react.git
synced 2026-04-26 23:04:55 +08:00
feat(collapse): add component
This commit is contained in:
@@ -65,7 +65,7 @@ export const pickChildrenFirst = (
|
||||
export const setChildrenProps = (
|
||||
children: ReactNode | undefined,
|
||||
props: object = {},
|
||||
targetComponents: Array<React.ElementType> = []
|
||||
targetComponents: Array<React.ElementType> = [],
|
||||
): ReactNode | undefined => {
|
||||
if (React.Children.count(children) === 0) return []
|
||||
const allowAll = targetComponents.length === 0
|
||||
@@ -81,6 +81,27 @@ export const setChildrenProps = (
|
||||
})
|
||||
}
|
||||
|
||||
export const setChildrenIndex = (
|
||||
children: ReactNode | undefined,
|
||||
targetComponents: Array<React.ElementType> = [],
|
||||
): ReactNode | undefined => {
|
||||
if (React.Children.count(children) === 0) return []
|
||||
const allowAll = targetComponents.length === 0
|
||||
const clone = (child: React.ReactElement, props = {}) => React.cloneElement(child, props)
|
||||
let index = 0
|
||||
|
||||
return React.Children.map(children, item => {
|
||||
if (!React.isValidElement(item)) return item
|
||||
index = index + 1
|
||||
if (allowAll) return clone(item, { index })
|
||||
|
||||
const isAllowed = targetComponents.find(child => child === item.type)
|
||||
if (isAllowed) return clone(item, { index })
|
||||
index = index - 1
|
||||
return item
|
||||
})
|
||||
}
|
||||
|
||||
export type ShapeType = {
|
||||
width: number
|
||||
height: number
|
||||
|
||||
Reference in New Issue
Block a user