feat(collapse): add component

This commit is contained in:
unix
2020-03-29 04:00:10 +08:00
parent d00007628d
commit 1c40cdbc12
8 changed files with 354 additions and 1 deletions

View File

@@ -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