index.tsx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import type { CSSProperties } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { RiCloseCircleFill, RiErrorWarningLine, RiSearchLine } from '@remixicon/react'
  5. import { type VariantProps, cva } from 'class-variance-authority'
  6. import cn from '@/utils/classnames'
  7. export const inputVariants = cva(
  8. '',
  9. {
  10. variants: {
  11. size: {
  12. regular: 'px-3 radius-md system-sm-regular',
  13. large: 'px-4 radius-lg system-md-regular',
  14. },
  15. },
  16. defaultVariants: {
  17. size: 'regular',
  18. },
  19. },
  20. )
  21. export type InputProps = {
  22. showLeftIcon?: boolean
  23. showClearIcon?: boolean
  24. onClear?: () => void
  25. disabled?: boolean
  26. destructive?: boolean
  27. wrapperClassName?: string
  28. styleCss?: CSSProperties
  29. unit?: string
  30. } & React.InputHTMLAttributes<HTMLInputElement> & VariantProps<typeof inputVariants>
  31. const Input = ({
  32. size,
  33. disabled,
  34. destructive,
  35. showLeftIcon,
  36. showClearIcon,
  37. onClear,
  38. wrapperClassName,
  39. className,
  40. styleCss,
  41. value,
  42. placeholder,
  43. onChange,
  44. unit,
  45. ...props
  46. }: InputProps) => {
  47. const { t } = useTranslation()
  48. return (
  49. <div className={cn('relative w-full', wrapperClassName)}>
  50. {showLeftIcon && <RiSearchLine className={cn('absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-components-input-text-placeholder')} />}
  51. <input
  52. style={styleCss}
  53. className={cn(
  54. 'w-full py-[7px] bg-components-input-bg-normal border border-transparent text-components-input-text-filled hover:bg-components-input-bg-hover hover:border-components-input-border-hover focus:bg-components-input-bg-active focus:border-components-input-border-active focus:shadow-xs placeholder:text-components-input-text-placeholder appearance-none outline-none caret-primary-600',
  55. inputVariants({ size }),
  56. showLeftIcon && 'pl-[26px]',
  57. showLeftIcon && size === 'large' && 'pl-7',
  58. showClearIcon && value && 'pr-[26px]',
  59. showClearIcon && value && size === 'large' && 'pr-7',
  60. destructive && 'pr-[26px]',
  61. destructive && size === 'large' && 'pr-7',
  62. disabled && 'bg-components-input-bg-disabled border-transparent text-components-input-text-filled-disabled cursor-not-allowed hover:bg-components-input-bg-disabled hover:border-transparent',
  63. destructive && 'bg-components-input-bg-destructive border-components-input-border-destructive text-components-input-text-filled hover:bg-components-input-bg-destructive hover:border-components-input-border-destructive focus:bg-components-input-bg-destructive focus:border-components-input-border-destructive',
  64. className,
  65. )}
  66. placeholder={placeholder ?? (showLeftIcon
  67. ? (t('common.operation.search') || '')
  68. : (t('common.placeholder.input') || ''))}
  69. value={value}
  70. onChange={onChange}
  71. disabled={disabled}
  72. {...props}
  73. />
  74. {showClearIcon && value && !disabled && !destructive && (
  75. <div className={cn('absolute right-2 top-1/2 -translate-y-1/2 group p-[1px] cursor-pointer')} onClick={onClear}>
  76. <RiCloseCircleFill className='w-3.5 h-3.5 text-text-quaternary cursor-pointer group-hover:text-text-tertiary' />
  77. </div>
  78. )}
  79. {destructive && (
  80. <RiErrorWarningLine className='absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-text-destructive-secondary' />
  81. )}
  82. {
  83. unit && (
  84. <div className='absolute right-2 top-1/2 -translate-y-1/2 system-sm-regular text-text-tertiary'>
  85. {unit}
  86. </div>
  87. )
  88. }
  89. </div>
  90. )
  91. }
  92. export default Input