廓形仪rn版本-技术调研
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
14 KiB

  1. 'use client';
  2. import React from 'react';
  3. import { H4 } from '@expo/html-elements';
  4. import { createActionsheet } from '@gluestack-ui/actionsheet';
  5. import {
  6. Pressable,
  7. View,
  8. Text,
  9. ScrollView,
  10. VirtualizedList,
  11. FlatList,
  12. SectionList,
  13. PressableProps,
  14. ViewStyle,
  15. } from 'react-native';
  16. import { PrimitiveIcon, UIIcon } from '@gluestack-ui/icon';
  17. import { tva } from '@gluestack-ui/nativewind-utils/tva';
  18. import type { VariantProps } from '@gluestack-ui/nativewind-utils';
  19. import { cssInterop } from 'nativewind';
  20. import {
  21. Motion,
  22. AnimatePresence,
  23. createMotionAnimatedComponent,
  24. MotionComponentProps,
  25. } from '@legendapp/motion';
  26. const ItemWrapper = React.forwardRef<
  27. React.ComponentRef<typeof Pressable>,
  28. PressableProps
  29. >(function ItemWrapper({ ...props }, ref) {
  30. return <Pressable {...props} ref={ref} />;
  31. });
  32. type IMotionViewProps = React.ComponentProps<typeof View> &
  33. MotionComponentProps<typeof View, ViewStyle, unknown, unknown, unknown>;
  34. const MotionView = Motion.View as React.ComponentType<IMotionViewProps>;
  35. type IAnimatedPressableProps = React.ComponentProps<typeof Pressable> &
  36. MotionComponentProps<typeof Pressable, ViewStyle, unknown, unknown, unknown>;
  37. const AnimatedPressable = createMotionAnimatedComponent(
  38. Pressable
  39. ) as React.ComponentType<IAnimatedPressableProps>;
  40. export const UIActionsheet = createActionsheet({
  41. Root: View,
  42. Content: MotionView,
  43. Item: ItemWrapper,
  44. ItemText: Text,
  45. DragIndicator: View,
  46. IndicatorWrapper: View,
  47. Backdrop: AnimatedPressable,
  48. ScrollView: ScrollView,
  49. VirtualizedList: VirtualizedList,
  50. FlatList: FlatList,
  51. SectionList: SectionList,
  52. SectionHeaderText: H4,
  53. Icon: UIIcon,
  54. AnimatePresence: AnimatePresence,
  55. });
  56. cssInterop(UIActionsheet, { className: 'style' });
  57. cssInterop(UIActionsheet.Content, { className: 'style' });
  58. cssInterop(ItemWrapper, { className: 'style' });
  59. cssInterop(UIActionsheet.ItemText, { className: 'style' });
  60. cssInterop(UIActionsheet.DragIndicator, { className: 'style' });
  61. cssInterop(UIActionsheet.DragIndicatorWrapper, { className: 'style' });
  62. cssInterop(UIActionsheet.Backdrop, { className: 'style' });
  63. cssInterop(UIActionsheet.ScrollView, {
  64. className: 'style',
  65. contentContainerClassName: 'contentContainerStyle',
  66. indicatorClassName: 'indicatorStyle',
  67. });
  68. cssInterop(UIActionsheet.VirtualizedList, {
  69. className: 'style',
  70. ListFooterComponentClassName: 'ListFooterComponentStyle',
  71. ListHeaderComponentClassName: 'ListHeaderComponentStyle',
  72. contentContainerClassName: 'contentContainerStyle',
  73. indicatorClassName: 'indicatorStyle',
  74. });
  75. cssInterop(UIActionsheet.FlatList, {
  76. className: 'style',
  77. ListFooterComponentClassName: 'ListFooterComponentStyle',
  78. ListHeaderComponentClassName: 'ListHeaderComponentStyle',
  79. columnWrapperClassName: 'columnWrapperStyle',
  80. contentContainerClassName: 'contentContainerStyle',
  81. indicatorClassName: 'indicatorStyle',
  82. });
  83. cssInterop(UIActionsheet.SectionList, { className: 'style' });
  84. cssInterop(UIActionsheet.SectionHeaderText, { className: 'style' });
  85. cssInterop(PrimitiveIcon, {
  86. className: {
  87. target: 'style',
  88. nativeStyleToProp: {
  89. height: true,
  90. width: true,
  91. fill: true,
  92. color: 'classNameColor',
  93. stroke: true,
  94. },
  95. },
  96. });
  97. const actionsheetStyle = tva({ base: 'w-full h-full web:pointer-events-none' });
  98. const actionsheetContentStyle = tva({
  99. base: 'items-center rounded-tl-3xl rounded-tr-3xl p-5 pt-2 bg-background-0 web:pointer-events-auto web:select-none shadow-hard-5 border border-b-0 border-outline-100',
  100. });
  101. const actionsheetItemStyle = tva({
  102. base: 'w-full flex-row items-center p-3 rounded-sm data-[disabled=true]:opacity-40 data-[disabled=true]:web:pointer-events-auto data-[disabled=true]:web:cursor-not-allowed hover:bg-background-50 active:bg-background-100 data-[focus=true]:bg-background-100 web:data-[focus-visible=true]:bg-background-100 web:data-[focus-visible=true]:outline-indicator-primary gap-2',
  103. });
  104. const actionsheetItemTextStyle = tva({
  105. base: 'text-typography-700 font-normal font-body',
  106. variants: {
  107. isTruncated: {
  108. true: '',
  109. },
  110. bold: {
  111. true: 'font-bold',
  112. },
  113. underline: {
  114. true: 'underline',
  115. },
  116. strikeThrough: {
  117. true: 'line-through',
  118. },
  119. size: {
  120. '2xs': 'text-2xs',
  121. 'xs': 'text-xs',
  122. 'sm': 'text-sm',
  123. 'md': 'text-base',
  124. 'lg': 'text-lg',
  125. 'xl': 'text-xl',
  126. '2xl': 'text-2xl',
  127. '3xl': 'text-3xl',
  128. '4xl': 'text-4xl',
  129. '5xl': 'text-5xl',
  130. '6xl': 'text-6xl',
  131. },
  132. },
  133. });
  134. const actionsheetDragIndicatorStyle = tva({
  135. base: 'w-16 h-1 bg-background-400 rounded-full',
  136. });
  137. const actionsheetDragIndicatorWrapperStyle = tva({
  138. base: 'w-full py-1 items-center',
  139. });
  140. const actionsheetBackdropStyle = tva({
  141. base: 'absolute left-0 top-0 right-0 bottom-0 bg-background-dark web:cursor-default web:pointer-events-auto',
  142. });
  143. const actionsheetScrollViewStyle = tva({
  144. base: 'w-full h-auto',
  145. });
  146. const actionsheetVirtualizedListStyle = tva({
  147. base: 'w-full h-auto',
  148. });
  149. const actionsheetFlatListStyle = tva({
  150. base: 'w-full h-auto',
  151. });
  152. const actionsheetSectionListStyle = tva({
  153. base: 'w-full h-auto',
  154. });
  155. const actionsheetSectionHeaderTextStyle = tva({
  156. base: 'leading-5 font-bold font-heading my-0 text-typography-500 p-3 uppercase',
  157. variants: {
  158. isTruncated: {
  159. true: '',
  160. },
  161. bold: {
  162. true: 'font-bold',
  163. },
  164. underline: {
  165. true: 'underline',
  166. },
  167. strikeThrough: {
  168. true: 'line-through',
  169. },
  170. size: {
  171. '5xl': 'text-5xl',
  172. '4xl': 'text-4xl',
  173. '3xl': 'text-3xl',
  174. '2xl': 'text-2xl',
  175. 'xl': 'text-xl',
  176. 'lg': 'text-lg',
  177. 'md': 'text-base',
  178. 'sm': 'text-sm',
  179. 'xs': 'text-xs',
  180. },
  181. sub: {
  182. true: 'text-xs',
  183. },
  184. italic: {
  185. true: 'italic',
  186. },
  187. highlight: {
  188. true: 'bg-yellow500',
  189. },
  190. },
  191. defaultVariants: {
  192. size: 'xs',
  193. },
  194. });
  195. const actionsheetIconStyle = tva({
  196. base: 'text-background-500 fill-none',
  197. variants: {
  198. size: {
  199. '2xs': 'h-3 w-3',
  200. 'xs': 'h-3.5 w-3.5',
  201. 'sm': 'h-4 w-4',
  202. 'md': 'w-[18px] h-[18px]',
  203. 'lg': 'h-5 w-5',
  204. 'xl': 'h-6 w-6',
  205. },
  206. },
  207. });
  208. type IActionsheetProps = VariantProps<typeof actionsheetStyle> &
  209. React.ComponentPropsWithoutRef<typeof UIActionsheet>;
  210. type IActionsheetContentProps = VariantProps<typeof actionsheetContentStyle> &
  211. React.ComponentPropsWithoutRef<typeof UIActionsheet.Content> & {
  212. className?: string;
  213. };
  214. type IActionsheetItemProps = VariantProps<typeof actionsheetItemStyle> &
  215. React.ComponentPropsWithoutRef<typeof UIActionsheet.Item>;
  216. type IActionsheetItemTextProps = VariantProps<typeof actionsheetItemTextStyle> &
  217. React.ComponentPropsWithoutRef<typeof UIActionsheet.ItemText>;
  218. type IActionsheetDragIndicatorProps = VariantProps<
  219. typeof actionsheetDragIndicatorStyle
  220. > &
  221. React.ComponentPropsWithoutRef<typeof UIActionsheet.DragIndicator>;
  222. type IActionsheetDragIndicatorWrapperProps = VariantProps<
  223. typeof actionsheetDragIndicatorWrapperStyle
  224. > &
  225. React.ComponentPropsWithoutRef<typeof UIActionsheet.DragIndicatorWrapper>;
  226. type IActionsheetBackdropProps = VariantProps<typeof actionsheetBackdropStyle> &
  227. React.ComponentPropsWithoutRef<typeof UIActionsheet.Backdrop> & {
  228. className?: string;
  229. };
  230. type IActionsheetScrollViewProps = VariantProps<
  231. typeof actionsheetScrollViewStyle
  232. > &
  233. React.ComponentPropsWithoutRef<typeof UIActionsheet.ScrollView>;
  234. type IActionsheetVirtualizedListProps = VariantProps<
  235. typeof actionsheetVirtualizedListStyle
  236. > &
  237. React.ComponentPropsWithoutRef<typeof UIActionsheet.VirtualizedList>;
  238. type IActionsheetFlatListProps = VariantProps<typeof actionsheetFlatListStyle> &
  239. React.ComponentPropsWithoutRef<typeof UIActionsheet.FlatList>;
  240. type IActionsheetSectionListProps = VariantProps<
  241. typeof actionsheetSectionListStyle
  242. > &
  243. React.ComponentPropsWithoutRef<typeof UIActionsheet.SectionList>;
  244. type IActionsheetSectionHeaderTextProps = VariantProps<
  245. typeof actionsheetSectionHeaderTextStyle
  246. > &
  247. React.ComponentPropsWithoutRef<typeof UIActionsheet.SectionHeaderText>;
  248. type IActionsheetIconProps = VariantProps<typeof actionsheetIconStyle> &
  249. React.ComponentPropsWithoutRef<typeof UIActionsheet.Icon> & {
  250. className?: string;
  251. as?: React.ElementType;
  252. height?: number;
  253. width?: number;
  254. };
  255. const Actionsheet = React.forwardRef<
  256. React.ComponentRef<typeof UIActionsheet>,
  257. IActionsheetProps
  258. >(function Actionsheet({ className, ...props }, ref) {
  259. return (
  260. <UIActionsheet
  261. className={actionsheetStyle({
  262. class: className,
  263. })}
  264. ref={ref}
  265. {...props}
  266. />
  267. );
  268. });
  269. const ActionsheetContent = React.forwardRef<
  270. React.ComponentRef<typeof UIActionsheet.Content>,
  271. IActionsheetContentProps
  272. >(function ActionsheetContent({ className, ...props }, ref) {
  273. return (
  274. <UIActionsheet.Content
  275. className={actionsheetContentStyle({
  276. class: className,
  277. })}
  278. ref={ref}
  279. {...props}
  280. />
  281. );
  282. });
  283. const ActionsheetItem = React.forwardRef<
  284. React.ComponentRef<typeof UIActionsheet.Item>,
  285. IActionsheetItemProps
  286. >(function ActionsheetItem({ className, ...props }, ref) {
  287. return (
  288. <UIActionsheet.Item
  289. className={actionsheetItemStyle({
  290. class: className,
  291. })}
  292. ref={ref}
  293. {...props}
  294. />
  295. );
  296. });
  297. const ActionsheetItemText = React.forwardRef<
  298. React.ComponentRef<typeof UIActionsheet.ItemText>,
  299. IActionsheetItemTextProps
  300. >(function ActionsheetItemText(
  301. {
  302. isTruncated,
  303. bold,
  304. underline,
  305. strikeThrough,
  306. size = 'sm',
  307. className,
  308. ...props
  309. },
  310. ref
  311. ) {
  312. return (
  313. <UIActionsheet.ItemText
  314. className={actionsheetItemTextStyle({
  315. class: className,
  316. isTruncated,
  317. bold,
  318. underline,
  319. strikeThrough,
  320. size,
  321. })}
  322. ref={ref}
  323. {...props}
  324. />
  325. );
  326. });
  327. const ActionsheetDragIndicator = React.forwardRef<
  328. React.ComponentRef<typeof UIActionsheet.DragIndicator>,
  329. IActionsheetDragIndicatorProps
  330. >(function ActionsheetDragIndicator({ className, ...props }, ref) {
  331. return (
  332. <UIActionsheet.DragIndicator
  333. className={actionsheetDragIndicatorStyle({
  334. class: className,
  335. })}
  336. ref={ref}
  337. {...props}
  338. />
  339. );
  340. });
  341. const ActionsheetDragIndicatorWrapper = React.forwardRef<
  342. React.ComponentRef<typeof UIActionsheet.DragIndicatorWrapper>,
  343. IActionsheetDragIndicatorWrapperProps
  344. >(function ActionsheetDragIndicatorWrapper({ className, ...props }, ref) {
  345. return (
  346. <UIActionsheet.DragIndicatorWrapper
  347. className={actionsheetDragIndicatorWrapperStyle({
  348. class: className,
  349. })}
  350. ref={ref}
  351. {...props}
  352. />
  353. );
  354. });
  355. const ActionsheetBackdrop = React.forwardRef<
  356. React.ComponentRef<typeof UIActionsheet.Backdrop>,
  357. IActionsheetBackdropProps
  358. >(function ActionsheetBackdrop({ className, ...props }, ref) {
  359. return (
  360. <UIActionsheet.Backdrop
  361. initial={{
  362. opacity: 0,
  363. }}
  364. animate={{
  365. opacity: 0.5,
  366. }}
  367. exit={{
  368. opacity: 0,
  369. }}
  370. {...props}
  371. className={actionsheetBackdropStyle({
  372. class: className,
  373. })}
  374. ref={ref}
  375. />
  376. );
  377. });
  378. const ActionsheetScrollView = React.forwardRef<
  379. React.ComponentRef<typeof UIActionsheet.ScrollView>,
  380. IActionsheetScrollViewProps
  381. >(function ActionsheetScrollView({ className, ...props }, ref) {
  382. return (
  383. <UIActionsheet.ScrollView
  384. className={actionsheetScrollViewStyle({
  385. class: className,
  386. })}
  387. ref={ref}
  388. {...props}
  389. />
  390. );
  391. });
  392. const ActionsheetVirtualizedList = React.forwardRef<
  393. React.ComponentRef<typeof UIActionsheet.VirtualizedList>,
  394. IActionsheetVirtualizedListProps
  395. >(function ActionsheetVirtualizedList({ className, ...props }, ref) {
  396. return (
  397. <UIActionsheet.VirtualizedList
  398. className={actionsheetVirtualizedListStyle({
  399. class: className,
  400. })}
  401. ref={ref}
  402. {...props}
  403. />
  404. );
  405. });
  406. const ActionsheetFlatList = React.forwardRef<
  407. React.ComponentRef<typeof UIActionsheet.FlatList>,
  408. IActionsheetFlatListProps
  409. >(function ActionsheetFlatList({ className, ...props }, ref) {
  410. return (
  411. <UIActionsheet.FlatList
  412. className={actionsheetFlatListStyle({
  413. class: className,
  414. })}
  415. ref={ref}
  416. {...props}
  417. />
  418. );
  419. });
  420. const ActionsheetSectionList = React.forwardRef<
  421. React.ComponentRef<typeof UIActionsheet.SectionList>,
  422. IActionsheetSectionListProps
  423. >(function ActionsheetSectionList({ className, ...props }, ref) {
  424. return (
  425. <UIActionsheet.SectionList
  426. className={actionsheetSectionListStyle({
  427. class: className,
  428. })}
  429. ref={ref}
  430. {...props}
  431. />
  432. );
  433. });
  434. const ActionsheetSectionHeaderText = React.forwardRef<
  435. React.ComponentRef<typeof UIActionsheet.SectionHeaderText>,
  436. IActionsheetSectionHeaderTextProps
  437. >(function ActionsheetSectionHeaderText(
  438. {
  439. className,
  440. isTruncated,
  441. bold,
  442. underline,
  443. strikeThrough,
  444. size,
  445. sub,
  446. italic,
  447. highlight,
  448. ...props
  449. },
  450. ref
  451. ) {
  452. return (
  453. <UIActionsheet.SectionHeaderText
  454. className={actionsheetSectionHeaderTextStyle({
  455. class: className,
  456. isTruncated,
  457. bold,
  458. underline,
  459. strikeThrough,
  460. size,
  461. sub,
  462. italic,
  463. highlight,
  464. })}
  465. ref={ref}
  466. {...props}
  467. />
  468. );
  469. });
  470. const ActionsheetIcon = React.forwardRef<
  471. React.ComponentRef<typeof UIActionsheet.Icon>,
  472. IActionsheetIconProps
  473. >(function ActionsheetIcon({ className, size = 'sm', ...props }, ref) {
  474. if (typeof size === 'number') {
  475. return (
  476. <UIActionsheet.Icon
  477. ref={ref}
  478. {...props}
  479. className={actionsheetIconStyle({ class: className })}
  480. size={size}
  481. />
  482. );
  483. } else if (
  484. (props.height !== undefined || props.width !== undefined) &&
  485. size === undefined
  486. ) {
  487. return (
  488. <UIActionsheet.Icon
  489. ref={ref}
  490. {...props}
  491. className={actionsheetIconStyle({ class: className })}
  492. />
  493. );
  494. }
  495. return (
  496. <UIActionsheet.Icon
  497. className={actionsheetIconStyle({
  498. class: className,
  499. size,
  500. })}
  501. ref={ref}
  502. {...props}
  503. />
  504. );
  505. });
  506. export {
  507. Actionsheet,
  508. ActionsheetContent,
  509. ActionsheetItem,
  510. ActionsheetItemText,
  511. ActionsheetDragIndicator,
  512. ActionsheetDragIndicatorWrapper,
  513. ActionsheetBackdrop,
  514. ActionsheetScrollView,
  515. ActionsheetVirtualizedList,
  516. ActionsheetFlatList,
  517. ActionsheetSectionList,
  518. ActionsheetSectionHeaderText,
  519. ActionsheetIcon,
  520. };