|
1 | 1 | import { ReactElement } from 'react'; |
| 2 | +import { PressableProps as GesturePressableProps } from 'react-native-gesture-handler'; |
2 | 3 | import { cn } from '@open-webui-react-native/mobile/shared/ui/styles'; |
3 | 4 | import { Icon, IconProps } from '../icon/component'; |
4 | 5 | import { IconName } from '../icon/types'; |
5 | | -import { AppPressable, AppPressableProps } from '../pressable'; |
| 6 | +import { AppPressable, AppPressableProps, GestureAppPressable } from '../pressable'; |
6 | 7 | import { AppSpinner } from '../spinner'; |
7 | 8 |
|
8 | | -export interface IconButtonProps extends AppPressableProps { |
| 9 | +interface BaseIconButtonProps { |
9 | 10 | iconName: IconName; |
10 | 11 | iconProps?: Omit<IconProps, 'name'>; |
11 | 12 | isLoading?: boolean; |
| 13 | + className?: string; |
12 | 14 | } |
13 | 15 |
|
14 | | -export function IconButton({ |
15 | | - className, |
| 16 | +export type IconButtonProps = |
| 17 | + | (BaseIconButtonProps & |
| 18 | + AppPressableProps & { |
| 19 | + useGestureHandler?: false; |
| 20 | + }) |
| 21 | + | (BaseIconButtonProps & |
| 22 | + GesturePressableProps & { |
| 23 | + useGestureHandler: true; |
| 24 | + }); |
| 25 | + |
| 26 | +const IconButtonContent = ({ |
| 27 | + isLoading, |
16 | 28 | iconName, |
17 | 29 | iconProps, |
18 | | - isLoading, |
19 | | - disabled, |
20 | | - ...restProps |
21 | | -}: IconButtonProps): ReactElement { |
| 30 | +}: Pick<BaseIconButtonProps, 'isLoading' | 'iconName' | 'iconProps'>): ReactElement => |
| 31 | + isLoading ? <AppSpinner size='small' /> : <Icon name={iconName} {...iconProps} />; |
| 32 | + |
| 33 | +export function IconButton(props: IconButtonProps): ReactElement { |
| 34 | + const baseClassName = cn('p-8 disabled:opacity-30 items-center justify-center', props.className); |
| 35 | + |
| 36 | + // NOTE: Pressable from react-native does not work correctly with react-native-modal - https://github.com/react-native-modal/react-native-modal/issues/582#issuecomment-1156723062 |
| 37 | + if (props.useGestureHandler) { |
| 38 | + const { useGestureHandler, iconName, iconProps, isLoading, className, ...gestureProps } = props; |
| 39 | + |
| 40 | + return ( |
| 41 | + <GestureAppPressable |
| 42 | + {...gestureProps} |
| 43 | + hitSlop={8} |
| 44 | + className={baseClassName} |
| 45 | + disabled={gestureProps.disabled || isLoading}> |
| 46 | + <IconButtonContent |
| 47 | + iconName={iconName} |
| 48 | + iconProps={iconProps} |
| 49 | + isLoading={isLoading} /> |
| 50 | + </GestureAppPressable> |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + const { useGestureHandler, iconName, iconProps, isLoading, className, ...rnProps } = props; |
| 55 | + |
22 | 56 | return ( |
23 | 57 | <AppPressable |
| 58 | + {...rnProps} |
24 | 59 | hitSlop={8} |
25 | | - disabled={disabled || isLoading} |
26 | | - className={cn('p-8 disabled:opacity-30 items-center justify-center', className)} |
27 | | - {...restProps}> |
28 | | - {isLoading ? <AppSpinner size='small' /> : <Icon name={iconName} {...iconProps} />} |
| 60 | + className={baseClassName} |
| 61 | + disabled={rnProps.disabled || isLoading}> |
| 62 | + <IconButtonContent |
| 63 | + iconName={iconName} |
| 64 | + iconProps={iconProps} |
| 65 | + isLoading={isLoading} /> |
29 | 66 | </AppPressable> |
30 | 67 | ); |
31 | 68 | } |
0 commit comments