UIButton.tsx 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Colors } from '@/constants/theme';
  2. import { isPromise } from '@/utils/tsutils';
  3. import { ActivityIndicator, Icon } from '@ant-design/react-native';
  4. import { type IconNames } from '@ant-design/react-native/lib/icon';
  5. import { clsx } from 'clsx';
  6. import { Href, Link } from "expo-router";
  7. import React, { useCallback, useEffect, useMemo, useState } from "react";
  8. import { Pressable, Text, View, ViewStyle } from "react-native";
  9. interface UIButtonProps {
  10. children?: string | React.ReactNode;
  11. title?: string | React.ReactNode;
  12. onPress?: () => PromiseLike<unknown> | unknown;
  13. disabled?: boolean;
  14. className?: string;
  15. textClassName?: string;
  16. href?: Href
  17. type?: 'primary' | 'second' | 'link'
  18. icon?: IconNames | React.ReactNode;
  19. style?: ViewStyle;
  20. loading?: boolean;
  21. }
  22. function ButtonTextChild({ type, disabled, textClassName, children, loading }: UIButtonProps) {
  23. return loading ? <View className='flex-row justify-center items-center'>
  24. <ActivityIndicator color={disabled ? '#888' : Colors['on-primary']['DEFAULT']} />
  25. <Text className={clsx(
  26. textClassName,
  27. 'ml-1',
  28. 'text-xl font-bold', {
  29. 'text-on-primary': !disabled && type === 'primary',
  30. 'text-primary': !disabled && type !== 'primary',
  31. 'text-secondary': !disabled && type === 'second',
  32. 'text-on-surface': !disabled && !type,
  33. 'text-on-surface-variant/65 font-normal': disabled,
  34. })}>{children}</Text>
  35. </View> : <Text className={clsx(
  36. textClassName,
  37. 'text-xl font-bold', {
  38. 'text-on-primary': !disabled && type === 'primary',
  39. 'text-primary': !disabled && type !== 'primary',
  40. 'text-secondary': !disabled && type === 'second',
  41. 'text-on-surface': !disabled && !type,
  42. 'text-on-surface-variant/65 font-normal': disabled,
  43. })}>{children}</Text>
  44. }
  45. export default function UIButton({ onPress, href, type, icon, className, style, disabled, children, title, textClassName, loading }: UIButtonProps) {
  46. const [ding, setDing] = useState(loading);
  47. useEffect(() => {
  48. setDing(loading);
  49. }, [loading])
  50. children = children || title;
  51. const handlePress = useCallback(() => {
  52. const res = onPress?.() as Promise<any> || undefined;
  53. if (isPromise(res)) {
  54. setDing(true);
  55. res.finally(() => {
  56. setDing(false);
  57. })
  58. }
  59. }, []);
  60. const inner = useMemo(() => <Pressable
  61. className={clsx('h-14 rounded-3xl flex-row justify-center items-center border-2 opacity-100 active:opacity-75 active:scale-95',
  62. className, {
  63. 'bg-primary border-primary': type === 'primary',
  64. 'bg-surface border-primary/25': !type,
  65. 'bg-surface border-on-surface/25': type === 'second',
  66. 'bg-transparent border-transparent': type === 'link',
  67. 'bg-primary-fixed-dim border-primary-fixed-dim': disabled && type === 'primary',
  68. 'bg-gray-300 border-gray-300': disabled && !type,
  69. 'bg-bg-gray-400 border-gray-200': disabled && type === 'second'
  70. })}
  71. disabled={disabled}
  72. onPress={handlePress}
  73. style={style}>
  74. {typeof icon === 'string' ? <Icon name={icon as IconNames} size={24} style={{ marginRight: 4, color: disabled ? Colors['on-surface']['variant'] : type === 'primary' ? '#fff' : type === 'second' ? Colors.secondary.DEFAULT : Colors.primary.DEFAULT }} /> : icon}
  75. {typeof children == 'string' ? <ButtonTextChild type={type} disabled={disabled} loading={loading} textClassName={textClassName}>{children}</ButtonTextChild> : children}
  76. </Pressable >, [children, className, disabled, icon, onPress, style, textClassName, type])
  77. if (typeof href !== 'undefined') {
  78. return <Link href={href} asChild>
  79. {inner}
  80. </Link>
  81. }
  82. return inner;
  83. }