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.

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