|
| 1 | +import { |
| 2 | + chakra, |
| 3 | + useRecipe, |
| 4 | + type HTMLChakraProps, |
| 5 | + type JsxStyleProps, |
| 6 | + type RecipeProps, |
| 7 | +} from '@chakra-ui/react'; |
| 8 | +import { |
| 9 | + Children, |
| 10 | + cloneElement, |
| 11 | + isValidElement, |
| 12 | + useMemo, |
| 13 | + type ReactElement, |
| 14 | + type Ref, |
| 15 | +} from 'react'; |
| 16 | + |
| 17 | +export interface GroupProps |
| 18 | + extends HTMLChakraProps<'div', RecipeProps<'group'>> { |
| 19 | + /** |
| 20 | + * The `alignItems` style property |
| 21 | + */ |
| 22 | + align?: JsxStyleProps['alignItems'] | undefined; |
| 23 | + /** |
| 24 | + * The `justifyContent` style property |
| 25 | + */ |
| 26 | + justify?: JsxStyleProps['justifyContent'] | undefined; |
| 27 | + /** |
| 28 | + * The `flexWrap` style property |
| 29 | + */ |
| 30 | + wrap?: JsxStyleProps['flexWrap'] | undefined; |
| 31 | + /** |
| 32 | + * A function that determines if a child should be skipped |
| 33 | + */ |
| 34 | + skip?: (child: ReactElement) => boolean | undefined; |
| 35 | + /** |
| 36 | + * Ref to the underlying DOM element |
| 37 | + */ |
| 38 | + ref?: Ref<HTMLElement>; |
| 39 | +} |
| 40 | + |
| 41 | +export function Group(props: GroupProps) { |
| 42 | + const recipe = useRecipe({ key: 'group' }); |
| 43 | + const [variantProps, otherProps] = recipe.splitVariantProps(props); |
| 44 | + const styles = recipe(variantProps); |
| 45 | + |
| 46 | + const { |
| 47 | + align = 'center', |
| 48 | + justify = 'flex-start', |
| 49 | + children, |
| 50 | + wrap, |
| 51 | + skip, |
| 52 | + ref, |
| 53 | + ...rest |
| 54 | + } = otherProps; |
| 55 | + |
| 56 | + // eslint-disable-next-line react-hooks/preserve-manual-memoization |
| 57 | + const _children = useMemo(() => { |
| 58 | + const childArray = Children.toArray(children).filter(isValidElement); |
| 59 | + |
| 60 | + if (childArray.length === 1) { |
| 61 | + return childArray; |
| 62 | + } |
| 63 | + |
| 64 | + const validChildArray = childArray.filter(child => !skip?.(child)); |
| 65 | + const validChildCount = validChildArray.length; |
| 66 | + |
| 67 | + if (validChildCount === 1) { |
| 68 | + return childArray; |
| 69 | + } |
| 70 | + |
| 71 | + return childArray.map(child => { |
| 72 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any |
| 73 | + const childProps = child.props as any; |
| 74 | + |
| 75 | + if (skip?.(child)) { |
| 76 | + return child; |
| 77 | + } |
| 78 | + |
| 79 | + const index = validChildArray.indexOf(child); |
| 80 | + |
| 81 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
| 82 | + return cloneElement(child, { |
| 83 | + ...childProps, |
| 84 | + 'data-group-item': '', |
| 85 | + 'data-first': dataAttr(index === 0), |
| 86 | + 'data-last': dataAttr(index === validChildCount - 1), |
| 87 | + 'data-between': dataAttr(index > 0 && index < validChildCount - 1), |
| 88 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 89 | + style: { |
| 90 | + '--group-count': validChildCount, |
| 91 | + '--group-index': index, |
| 92 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 93 | + ...(childProps?.style ?? {}), |
| 94 | + }, |
| 95 | + }); |
| 96 | + }); |
| 97 | + // eslint-disable-next-line react-hooks/preserve-manual-memoization |
| 98 | + }, [children, skip]); |
| 99 | + |
| 100 | + return ( |
| 101 | + <chakra.div |
| 102 | + css={styles} |
| 103 | + ref={ref} |
| 104 | + alignItems={align} |
| 105 | + justifyContent={justify} |
| 106 | + flexWrap={wrap} |
| 107 | + {...rest} |
| 108 | + > |
| 109 | + {_children} |
| 110 | + </chakra.div> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +type Booleanish = boolean | 'true' | 'false'; |
| 115 | + |
| 116 | +function dataAttr(condition: boolean | undefined) { |
| 117 | + return (condition ? '' : undefined) as Booleanish; |
| 118 | +} |
0 commit comments