Input.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import type { FC } from 'react'
  2. import { CheckCircle } from '@/app/components/base/icons/src/vender/solid/general'
  3. type InputProps = {
  4. value?: string
  5. onChange: (v: string) => void
  6. onFocus?: () => void
  7. placeholder?: string
  8. validated?: boolean
  9. className?: string
  10. disabled?: boolean
  11. type?: string
  12. min?: number
  13. max?: number
  14. }
  15. const Input: FC<InputProps> = ({
  16. value,
  17. onChange,
  18. onFocus,
  19. placeholder,
  20. validated,
  21. className,
  22. disabled,
  23. type = 'text',
  24. min,
  25. max,
  26. }) => {
  27. const toLimit = (v: string) => {
  28. const minNum = Number.parseFloat(`${min}`)
  29. const maxNum = Number.parseFloat(`${max}`)
  30. if (!isNaN(minNum) && Number.parseFloat(v) < minNum) {
  31. onChange(`${min}`)
  32. return
  33. }
  34. if (!isNaN(maxNum) && Number.parseFloat(v) > maxNum)
  35. onChange(`${max}`)
  36. }
  37. return (
  38. <div className='relative'>
  39. <input
  40. tabIndex={0}
  41. className={`
  42. block h-8 w-full appearance-none rounded-lg border border-transparent bg-components-input-bg-normal px-3 text-sm
  43. text-components-input-text-filled caret-primary-600 outline-none
  44. placeholder:text-sm placeholder:text-text-tertiary
  45. hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active
  46. focus:bg-components-input-bg-active focus:shadow-xs
  47. ${validated && 'pr-[30px]'}
  48. ${className}
  49. `}
  50. placeholder={placeholder || ''}
  51. onChange={e => onChange(e.target.value)}
  52. onBlur={e => toLimit(e.target.value)}
  53. onFocus={onFocus}
  54. value={value}
  55. disabled={disabled}
  56. type={type}
  57. min={min}
  58. max={max}
  59. />
  60. {
  61. validated && (
  62. <div className='absolute right-2.5 top-2.5'>
  63. <CheckCircle className='h-4 w-4 text-[#039855]' />
  64. </div>
  65. )
  66. }
  67. </div>
  68. )
  69. }
  70. export default Input