|
| 1 | +import { ReactElement } from "react"; |
| 2 | +import { StyleProp, ViewStyle } from "react-native"; |
| 3 | +import { Box, useTheme } from "@adaptui/react-native-tailwind"; |
| 4 | + |
| 5 | +import { createComponent, cx, styleAdapter } from "../../utils"; |
| 6 | +import { Button, ButtonProps } from "../button"; |
| 7 | + |
| 8 | +interface DividerProps { |
| 9 | + /** |
| 10 | + * Label name |
| 11 | + */ |
| 12 | + label: string; |
| 13 | + /** |
| 14 | + * The orientation of the divider |
| 15 | + * @default horizontal |
| 16 | + */ |
| 17 | + orientation: "horizontal" | "vertical"; |
| 18 | + /** |
| 19 | + * Pass custom component instead of deafult component. |
| 20 | + */ |
| 21 | + slot: ReactElement; |
| 22 | + /** |
| 23 | + * The position of the label/slot |
| 24 | + * @default start |
| 25 | + */ |
| 26 | + labelPosition: "start" | "center" | "end"; |
| 27 | + /** |
| 28 | + * A prop that is passed for the button component |
| 29 | + */ |
| 30 | + buttonProps: ButtonProps; |
| 31 | + /** |
| 32 | + * A style prop that is passed for the divider line |
| 33 | + */ |
| 34 | + dividerStyle: StyleProp<ViewStyle>; |
| 35 | +} |
| 36 | + |
| 37 | +const RNDivider = (props: Partial<DividerProps>) => { |
| 38 | + const { |
| 39 | + label, |
| 40 | + orientation = "horizontal", |
| 41 | + slot, |
| 42 | + labelPosition = "start", |
| 43 | + buttonProps, |
| 44 | + dividerStyle, |
| 45 | + } = props; |
| 46 | + const tailwind = useTheme(); |
| 47 | + const dividerTheme = useTheme("divider"); |
| 48 | + return ( |
| 49 | + <Box |
| 50 | + style={tailwind.style( |
| 51 | + cx(dividerTheme[orientation].orientaion), |
| 52 | + orientation === "vertical" ? "overflow-hidden" : {}, |
| 53 | + )} |
| 54 | + > |
| 55 | + <Box |
| 56 | + style={[ |
| 57 | + tailwind.style(cx(dividerTheme[orientation].lines)), |
| 58 | + styleAdapter(dividerStyle), |
| 59 | + ]} |
| 60 | + /> |
| 61 | + |
| 62 | + <Box |
| 63 | + style={tailwind.style( |
| 64 | + cx(dividerTheme[orientation]?.label[labelPosition]), |
| 65 | + )} |
| 66 | + > |
| 67 | + {label ? ( |
| 68 | + <Button themeColor="base" variant="outline" {...buttonProps}> |
| 69 | + {label} |
| 70 | + </Button> |
| 71 | + ) : slot ? ( |
| 72 | + slot |
| 73 | + ) : null} |
| 74 | + </Box> |
| 75 | + </Box> |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +export const Divider = createComponent<Partial<DividerProps>>(RNDivider, { |
| 80 | + shouldMemo: true, |
| 81 | +}); |
0 commit comments