廓形仪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.

562 lines
14 KiB

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