index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. } & React.InputHTMLAttributes<HTMLInputElement> & VariantProps<typeof inputVariants>
  30. const Input = ({
  31. size,
  32. disabled,
  33. destructive,
  34. showLeftIcon,
  35. showClearIcon,
  36. onClear,
  37. wrapperClassName,
  38. className,
  39. styleCss,
  40. value,
  41. placeholder,
  42. onChange,
  43. ...props
  44. }: InputProps) => {
  45. const { t } = useTranslation()
  46. return (
  47. <div className={cn('relative w-full', wrapperClassName)}>
  48. {showLeftIcon && <RiSearchLine className={cn('absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-components-input-text-placeholder')} />}
  49. <input
  50. style={styleCss}
  51. className={cn(
  52. 'w-full py-[7px] bg-gray-200 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',
  53. inputVariants({ size }),
  54. showLeftIcon && 'pl-[26px]',
  55. showLeftIcon && size === 'large' && 'pl-7',
  56. showClearIcon && value && 'pr-[26px]',
  57. showClearIcon && value && size === 'large' && 'pr-7',
  58. destructive && 'pr-[26px]',
  59. destructive && size === 'large' && 'pr-7',
  60. 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',
  61. 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',
  62. className,
  63. )}
  64. placeholder={placeholder ?? (showLeftIcon
  65. ? (t('common.operation.search') || '')
  66. : (t('common.placeholder.input') || ''))}
  67. value={value}
  68. onChange={onChange}
  69. disabled={disabled}
  70. {...props}
  71. />
  72. {showClearIcon && value && !disabled && !destructive && (
  73. <div className={cn('absolute right-2 top-1/2 -translate-y-1/2 group p-[1px] cursor-pointer')} onClick={onClear}>
  74. <RiCloseCircleFill className='w-3.5 h-3.5 text-text-quaternary cursor-pointer group-hover:text-text-tertiary' />
  75. </div>
  76. )}
  77. {destructive && (
  78. <RiErrorWarningLine className='absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 text-text-destructive-secondary' />
  79. )}
  80. </div>
  81. )
  82. }
  83. export default Input