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

220 lines
5.1 KiB

  1. import React, { forwardRef, memo } from 'react';
  2. import { H1, H2, H3, H4, H5, H6 } from '@expo/html-elements';
  3. import { headingStyle } from './styles';
  4. import type { VariantProps } from '@gluestack-ui/nativewind-utils';
  5. import { cssInterop } from 'nativewind';
  6. type IHeadingProps = VariantProps<typeof headingStyle> &
  7. React.ComponentPropsWithoutRef<typeof H1> & {
  8. as?: React.ElementType;
  9. };
  10. cssInterop(H1, { className: 'style' });
  11. cssInterop(H2, { className: 'style' });
  12. cssInterop(H3, { className: 'style' });
  13. cssInterop(H4, { className: 'style' });
  14. cssInterop(H5, { className: 'style' });
  15. cssInterop(H6, { className: 'style' });
  16. const MappedHeading = memo(
  17. forwardRef<React.ComponentRef<typeof H1>, IHeadingProps>(
  18. function MappedHeading(
  19. {
  20. size,
  21. className,
  22. isTruncated,
  23. bold,
  24. underline,
  25. strikeThrough,
  26. sub,
  27. italic,
  28. highlight,
  29. ...props
  30. },
  31. ref
  32. ) {
  33. switch (size) {
  34. case '5xl':
  35. case '4xl':
  36. case '3xl':
  37. return (
  38. <H1
  39. className={headingStyle({
  40. size,
  41. isTruncated,
  42. bold,
  43. underline,
  44. strikeThrough,
  45. sub,
  46. italic,
  47. highlight,
  48. class: className,
  49. })}
  50. {...props}
  51. // @ts-expect-error : type issue
  52. ref={ref}
  53. />
  54. );
  55. case '2xl':
  56. return (
  57. <H2
  58. className={headingStyle({
  59. size,
  60. isTruncated,
  61. bold,
  62. underline,
  63. strikeThrough,
  64. sub,
  65. italic,
  66. highlight,
  67. class: className,
  68. })}
  69. {...props}
  70. // @ts-expect-error : type issue
  71. ref={ref}
  72. />
  73. );
  74. case 'xl':
  75. return (
  76. <H3
  77. className={headingStyle({
  78. size,
  79. isTruncated,
  80. bold,
  81. underline,
  82. strikeThrough,
  83. sub,
  84. italic,
  85. highlight,
  86. class: className,
  87. })}
  88. {...props}
  89. // @ts-expect-error : type issue
  90. ref={ref}
  91. />
  92. );
  93. case 'lg':
  94. return (
  95. <H4
  96. className={headingStyle({
  97. size,
  98. isTruncated,
  99. bold,
  100. underline,
  101. strikeThrough,
  102. sub,
  103. italic,
  104. highlight,
  105. class: className,
  106. })}
  107. {...props}
  108. // @ts-expect-error : type issue
  109. ref={ref}
  110. />
  111. );
  112. case 'md':
  113. return (
  114. <H5
  115. className={headingStyle({
  116. size,
  117. isTruncated,
  118. bold,
  119. underline,
  120. strikeThrough,
  121. sub,
  122. italic,
  123. highlight,
  124. class: className,
  125. })}
  126. {...props}
  127. // @ts-expect-error : type issue
  128. ref={ref}
  129. />
  130. );
  131. case 'sm':
  132. case 'xs':
  133. return (
  134. <H6
  135. className={headingStyle({
  136. size,
  137. isTruncated,
  138. bold,
  139. underline,
  140. strikeThrough,
  141. sub,
  142. italic,
  143. highlight,
  144. class: className,
  145. })}
  146. {...props}
  147. // @ts-expect-error : type issue
  148. ref={ref}
  149. />
  150. );
  151. default:
  152. return (
  153. <H4
  154. className={headingStyle({
  155. size,
  156. isTruncated,
  157. bold,
  158. underline,
  159. strikeThrough,
  160. sub,
  161. italic,
  162. highlight,
  163. class: className,
  164. })}
  165. {...props}
  166. // @ts-expect-error : type issue
  167. ref={ref}
  168. />
  169. );
  170. }
  171. }
  172. )
  173. );
  174. const Heading = memo(
  175. forwardRef<React.ComponentRef<typeof H1>, IHeadingProps>(function Heading(
  176. { className, size = 'lg', as: AsComp, ...props },
  177. ref
  178. ) {
  179. const {
  180. isTruncated,
  181. bold,
  182. underline,
  183. strikeThrough,
  184. sub,
  185. italic,
  186. highlight,
  187. } = props;
  188. if (AsComp) {
  189. return (
  190. <AsComp
  191. className={headingStyle({
  192. size,
  193. isTruncated,
  194. bold,
  195. underline,
  196. strikeThrough,
  197. sub,
  198. italic,
  199. highlight,
  200. class: className,
  201. })}
  202. {...props}
  203. />
  204. );
  205. }
  206. return (
  207. <MappedHeading className={className} size={size} ref={ref} {...props} />
  208. );
  209. })
  210. );
  211. Heading.displayName = 'Heading';
  212. export { Heading };