Last active
October 24, 2024 10:09
-
-
Save Arathi/2aaebc8b24d343398a883631c2068781 to your computer and use it in GitHub Desktop.
Flex,基于React的通用弹性布局函数式组件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from "react"; | |
| export type FlexContainerProperties = { | |
| direction?: React.CSSProperties["flexDirection"]; | |
| wrap?: React.CSSProperties["flexWrap"]; | |
| justify?: React.CSSProperties["justifyContent"]; | |
| align?: React.CSSProperties["alignItems"]; | |
| gap?: React.CSSProperties["gap"]; | |
| }; | |
| export type FlexElementProperties = { | |
| grow?: React.CSSProperties["flexGrow"]; | |
| shrink?: React.CSSProperties["flexShrink"]; | |
| basis?: React.CSSProperties["flexBasis"]; | |
| flex?: React.CSSProperties["flex"]; | |
| }; | |
| export type FlexProps = React.HTMLAttributes<HTMLDivElement> & | |
| FlexContainerProperties & | |
| FlexElementProperties; | |
| const Flex: React.FC<FlexProps> = (props) => { | |
| const { direction, wrap, justify, align, flex, gap } = props; | |
| const items = React.Children.map(props.children, (child) => { | |
| if (React.isValidElement(child)) { | |
| const { props: childProps } = child; | |
| return React.cloneElement(child, { | |
| ...childProps, | |
| }); | |
| } | |
| return child; | |
| }); | |
| return ( | |
| <div | |
| {...props} | |
| style={{ | |
| ...props.style, | |
| display: "flex", | |
| flexDirection: direction, | |
| flexWrap: wrap, | |
| justifyContent: justify, | |
| alignItems: align, | |
| flex: flex, | |
| gap: gap, | |
| }} | |
| > | |
| {items} | |
| </div> | |
| ); | |
| }; | |
| export default Flex; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment