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

202 lines
4.1 KiB

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