label.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { View, Text } from '@tarojs/components'
  2. import { cn } from '@/utils/cn'
  3. import { cva, type VariantProps } from 'class-variance-authority'
  4. import { forwardRef } from 'react'
  5. const labelVariants = cva(
  6. 'text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70',
  7. {
  8. variants: {
  9. variant: {
  10. default: 'text-gray-900',
  11. secondary: 'text-gray-600',
  12. destructive: 'text-red-600',
  13. },
  14. size: {
  15. default: 'text-sm',
  16. sm: 'text-xs',
  17. lg: 'text-base',
  18. },
  19. },
  20. defaultVariants: {
  21. variant: 'default',
  22. size: 'default',
  23. },
  24. }
  25. )
  26. export interface LabelProps {
  27. className?: string
  28. variant?: VariantProps<typeof labelVariants>['variant']
  29. size?: VariantProps<typeof labelVariants>['size']
  30. children: React.ReactNode
  31. required?: boolean
  32. htmlFor?: string
  33. }
  34. const Label = forwardRef<HTMLLabelElement, LabelProps>(
  35. ({ className, variant, size, children, required, htmlFor, ...props }, ref) => {
  36. return (
  37. <View className="mb-2">
  38. <Text
  39. className={cn(labelVariants({ variant, size, className }))}
  40. {...props}
  41. >
  42. {children}
  43. {required && <Text className="text-red-500 ml-1">*</Text>}
  44. </Text>
  45. </View>
  46. )
  47. }
  48. )
  49. Label.displayName = 'Label'
  50. export { Label, labelVariants }