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

169 lines
3.7 KiB

  1. 'use client';
  2. import React from 'react';
  3. import { createProgress } from '@gluestack-ui/progress';
  4. import { View } from 'react-native';
  5. import { tva } from '@gluestack-ui/nativewind-utils/tva';
  6. import {
  7. withStyleContext,
  8. useStyleContext,
  9. } from '@gluestack-ui/nativewind-utils/withStyleContext';
  10. import { cssInterop } from 'nativewind';
  11. import type { VariantProps } from '@gluestack-ui/nativewind-utils';
  12. const SCOPE = 'PROGRESS';
  13. export const UIProgress = createProgress({
  14. Root: withStyleContext(View, SCOPE),
  15. FilledTrack: View,
  16. });
  17. cssInterop(UIProgress, { className: 'style' });
  18. cssInterop(UIProgress.FilledTrack, { className: 'style' });
  19. const progressStyle = tva({
  20. base: 'bg-background-300 rounded-full w-full',
  21. variants: {
  22. orientation: {
  23. horizontal: 'w-full',
  24. vertical: 'h-full',
  25. },
  26. size: {
  27. 'xs': 'h-1',
  28. 'sm': 'h-2',
  29. 'md': 'h-3',
  30. 'lg': 'h-4',
  31. 'xl': 'h-5',
  32. '2xl': 'h-6',
  33. },
  34. },
  35. compoundVariants: [
  36. {
  37. orientation: 'vertical',
  38. size: 'xs',
  39. class: 'h-full w-1 justify-end',
  40. },
  41. {
  42. orientation: 'vertical',
  43. size: 'sm',
  44. class: 'h-full w-2 justify-end',
  45. },
  46. {
  47. orientation: 'vertical',
  48. size: 'md',
  49. class: 'h-full w-3 justify-end',
  50. },
  51. {
  52. orientation: 'vertical',
  53. size: 'lg',
  54. class: 'h-full w-4 justify-end',
  55. },
  56. {
  57. orientation: 'vertical',
  58. size: 'xl',
  59. class: 'h-full w-5 justify-end',
  60. },
  61. {
  62. orientation: 'vertical',
  63. size: '2xl',
  64. class: 'h-full w-6 justify-end',
  65. },
  66. ],
  67. });
  68. const progressFilledTrackStyle = tva({
  69. base: 'bg-primary-500 rounded-full',
  70. parentVariants: {
  71. orientation: {
  72. horizontal: 'w-full',
  73. vertical: 'h-full',
  74. },
  75. size: {
  76. 'xs': 'h-1',
  77. 'sm': 'h-2',
  78. 'md': 'h-3',
  79. 'lg': 'h-4',
  80. 'xl': 'h-5',
  81. '2xl': 'h-6',
  82. },
  83. },
  84. parentCompoundVariants: [
  85. {
  86. orientation: 'vertical',
  87. size: 'xs',
  88. class: 'h-full w-1',
  89. },
  90. {
  91. orientation: 'vertical',
  92. size: 'sm',
  93. class: 'h-full w-2',
  94. },
  95. {
  96. orientation: 'vertical',
  97. size: 'md',
  98. class: 'h-full w-3',
  99. },
  100. {
  101. orientation: 'vertical',
  102. size: 'lg',
  103. class: 'h-full w-4',
  104. },
  105. {
  106. orientation: 'vertical',
  107. size: 'xl',
  108. class: 'h-full w-5',
  109. },
  110. {
  111. orientation: 'vertical',
  112. size: '2xl',
  113. class: 'h-full w-6',
  114. },
  115. ],
  116. });
  117. type IProgressProps = VariantProps<typeof progressStyle> &
  118. React.ComponentProps<typeof UIProgress>;
  119. type IProgressFilledTrackProps = VariantProps<typeof progressFilledTrackStyle> &
  120. React.ComponentProps<typeof UIProgress.FilledTrack>;
  121. const Progress = React.forwardRef<
  122. React.ComponentRef<typeof UIProgress>,
  123. IProgressProps
  124. >(function Progress(
  125. { className, size = 'md', orientation = 'horizontal', ...props },
  126. ref
  127. ) {
  128. return (
  129. <UIProgress
  130. ref={ref}
  131. {...props}
  132. className={progressStyle({ size, orientation, class: className })}
  133. context={{ size, orientation }}
  134. orientation={orientation}
  135. />
  136. );
  137. });
  138. const ProgressFilledTrack = React.forwardRef<
  139. React.ComponentRef<typeof UIProgress.FilledTrack>,
  140. IProgressFilledTrackProps
  141. >(function ProgressFilledTrack({ className, ...props }, ref) {
  142. const { size: parentSize, orientation: parentOrientation } =
  143. useStyleContext(SCOPE);
  144. return (
  145. <UIProgress.FilledTrack
  146. ref={ref}
  147. className={progressFilledTrackStyle({
  148. parentVariants: {
  149. size: parentSize,
  150. orientation: parentOrientation,
  151. },
  152. class: className,
  153. })}
  154. {...props}
  155. />
  156. );
  157. });
  158. export { Progress, ProgressFilledTrack };