|
1 | 1 | import React, { memo, forwardRef } from 'react';
|
2 |
| -import { Button } from '../../primitives/Button'; |
3 |
| -import type { IActionsheetItemProps } from './types'; |
| 2 | +import { Pressable } from '../../primitives/Pressable'; |
| 3 | +import Box from '../../primitives/Box'; |
| 4 | +import { HStack } from '../../primitives/Stack'; |
| 5 | +import Spinner from '../../primitives/Spinner'; |
4 | 6 | import { usePropsResolution } from '../../../hooks';
|
5 | 7 | import { useHasResponsiveProps } from '../../../hooks/useHasResponsiveProps';
|
| 8 | +import type { IActionsheetItemProps } from './types'; |
6 | 9 |
|
7 |
| -const ActionsheetItem = (props: IActionsheetItemProps, ref?: any) => { |
8 |
| - const resolvedProps = usePropsResolution( |
9 |
| - 'ActionsheetItem', |
10 |
| - props, |
11 |
| - undefined, |
12 |
| - { |
13 |
| - cascadePseudoProps: true, |
14 |
| - } |
15 |
| - ); |
| 10 | +const ActionsheetItem = ( |
| 11 | + { |
| 12 | + //@ts-ignore |
| 13 | + children, |
| 14 | + startIcon, |
| 15 | + rightIcon, |
| 16 | + leftIcon, |
| 17 | + endIcon, |
| 18 | + spinner, |
| 19 | + isDisabled, |
| 20 | + isLoading, |
| 21 | + spinnerPlacement = 'start', |
| 22 | + ...props |
| 23 | + }: IActionsheetItemProps, |
| 24 | + ref: any |
| 25 | +) => { |
| 26 | + const { |
| 27 | + _text, |
| 28 | + _stack, |
| 29 | + _icon, |
| 30 | + _spinner, |
| 31 | + isLoadingText, |
| 32 | + ...resolvedProps |
| 33 | + } = usePropsResolution('ActionsheetItem', props, undefined, { |
| 34 | + cascadePseudoProps: true, |
| 35 | + }); |
16 | 36 | //TODO: refactor for responsive prop
|
17 | 37 | if (useHasResponsiveProps(props)) {
|
18 | 38 | return null;
|
19 | 39 | }
|
20 | 40 |
|
21 |
| - return <Button {...resolvedProps} ref={ref} />; |
| 41 | + if (leftIcon) { |
| 42 | + startIcon = leftIcon; |
| 43 | + } |
| 44 | + if (rightIcon) { |
| 45 | + endIcon = rightIcon; |
| 46 | + } |
| 47 | + if (endIcon && React.isValidElement(endIcon)) { |
| 48 | + endIcon = React.Children.map( |
| 49 | + endIcon, |
| 50 | + (child: JSX.Element, index: number) => { |
| 51 | + return React.cloneElement(child, { |
| 52 | + key: `button-end-icon-${index}`, |
| 53 | + ..._icon, |
| 54 | + ...child.props, |
| 55 | + }); |
| 56 | + } |
| 57 | + ); |
| 58 | + } |
| 59 | + if (startIcon && React.isValidElement(startIcon)) { |
| 60 | + startIcon = React.Children.map( |
| 61 | + startIcon, |
| 62 | + (child: JSX.Element, index: number) => { |
| 63 | + return React.cloneElement(child, { |
| 64 | + key: `button-start-icon-${index}`, |
| 65 | + ..._icon, |
| 66 | + ...child.props, |
| 67 | + }); |
| 68 | + } |
| 69 | + ); |
| 70 | + } |
| 71 | + const spinnerElement = spinner ? ( |
| 72 | + spinner |
| 73 | + ) : ( |
| 74 | + <Spinner color={_text?.color} {..._spinner} /> |
| 75 | + ); |
| 76 | + |
| 77 | + const boxChildren = (child: any) => { |
| 78 | + return child ? <Box _text={_text}>{child}</Box> : null; |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <Pressable disabled={isDisabled || isLoading} {...resolvedProps} ref={ref}> |
| 83 | + <HStack {..._stack} test={true}> |
| 84 | + {startIcon && !isLoading ? startIcon : null} |
| 85 | + {isLoading && spinnerPlacement === 'start' ? spinnerElement : null} |
| 86 | + {isLoading |
| 87 | + ? isLoadingText |
| 88 | + ? boxChildren(isLoadingText) |
| 89 | + : null |
| 90 | + : boxChildren(children)} |
| 91 | + |
| 92 | + {endIcon && !isLoading ? endIcon : null} |
| 93 | + {isLoading && spinnerPlacement === 'end' ? spinnerElement : null} |
| 94 | + </HStack> |
| 95 | + </Pressable> |
| 96 | + ); |
22 | 97 | };
|
23 | 98 |
|
24 | 99 | export default memo(forwardRef(ActionsheetItem));
|
0 commit comments