IconPicker.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { useMemo, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { ChevronDown, Search } from 'lucide-react'
  4. import { ALLOWED_ICONS, ICON_CATEGORIES, ICON_OVERRIDES } from '@/components/DeviceRow'
  5. import { Input } from '@/components/ui/input'
  6. import { cn } from '@/lib/utils'
  7. interface IconPickerProps {
  8. value: string
  9. onChange: (icon: string) => void
  10. disabled?: boolean
  11. columns?: 5 | 6
  12. }
  13. /** Selector de iconos compartido: búsqueda sobre una rejilla por categorías.
  14. Las cabeceras plegan su rejilla (la primera categoría y la que contiene la
  15. selección actual empiezan abiertas), y al escribir se aplana a las
  16. coincidencias de todas las categorías. Auto es la primera casilla de la
  17. primera categoría. Sustituye las dos rejillas inline que duplicaban este
  18. marcado en DeviceEditSheet y OnboardingIntake. */
  19. export function IconPicker({ value, onChange, disabled, columns = 5 }: IconPickerProps) {
  20. const { t } = useTranslation()
  21. const [query, setQuery] = useState('')
  22. const q = query.trim().toLowerCase()
  23. const gridCols = columns === 6 ? 'grid-cols-6' : 'grid-cols-5'
  24. const label = (n: string) => t(`devices.edit.iconNames.${n}`, { defaultValue: n })
  25. const selectable = useMemo(() => ALLOWED_ICONS.filter((n) => n !== 'help-circle'), [])
  26. const [collapsed, setCollapsed] = useState<Record<string, boolean>>(() => {
  27. const firstId = ICON_CATEGORIES[0]?.id ?? 'general'
  28. const owner = ICON_CATEGORIES.find((c) => c.icons.includes(value))?.id ?? firstId
  29. return Object.fromEntries(
  30. ICON_CATEGORIES.map((c) => [c.id, c.id !== owner && c.id !== firstId]),
  31. )
  32. })
  33. const cell = (n: string) => {
  34. const Icon = ICON_OVERRIDES[n]!
  35. const name = label(n)
  36. return (
  37. <button
  38. key={n}
  39. type="button"
  40. title={name}
  41. aria-label={name}
  42. aria-pressed={value === n}
  43. onClick={() => onChange(n)}
  44. disabled={disabled}
  45. className={cn(
  46. 'flex h-11 items-center justify-center rounded-lg border transition-colors hover:bg-hover',
  47. value === n ? 'border-accent bg-accent-soft text-accent' : 'border-border bg-elevated text-text-secondary'
  48. )}
  49. >
  50. <Icon className="h-5 w-5" strokeWidth={1.75} />
  51. </button>
  52. )
  53. }
  54. const autoCell = (
  55. <button
  56. type="button"
  57. title={t('devices.edit.auto')}
  58. aria-label={t('devices.edit.auto')}
  59. aria-pressed={value === ''}
  60. onClick={() => onChange('')}
  61. disabled={disabled}
  62. className={cn(
  63. 'flex h-11 items-center justify-center rounded-lg border text-xs font-medium transition-colors hover:bg-hover',
  64. value === '' ? 'border-accent bg-accent-soft text-accent' : 'border-border bg-elevated text-text-secondary'
  65. )}
  66. >
  67. {t('devices.edit.auto')}
  68. </button>
  69. )
  70. const matches = q
  71. ? selectable.filter((n) => n.toLowerCase().includes(q) || label(n).toLowerCase().includes(q))
  72. : []
  73. return (
  74. <div className="space-y-2">
  75. <div className="relative">
  76. <Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-text-muted" strokeWidth={1.75} />
  77. <Input
  78. type="search"
  79. value={query}
  80. onChange={(e) => setQuery(e.target.value)}
  81. placeholder={t('devices.edit.iconSearch')}
  82. aria-label={t('devices.edit.iconSearch')}
  83. disabled={disabled}
  84. className="h-9 rounded-lg border-border bg-elevated pl-9 text-sm text-text-primary placeholder:text-text-muted focus-visible:border-accent/50"
  85. />
  86. </div>
  87. <div className="max-h-64 space-y-1.5 overflow-y-auto pr-1">
  88. {q ? (
  89. matches.length > 0 ? (
  90. <div className={cn('grid gap-2', gridCols)}>{matches.map(cell)}</div>
  91. ) : (
  92. <p className="py-4 text-center text-caption text-text-muted">{t('devices.edit.iconNoResults')}</p>
  93. )
  94. ) : (
  95. ICON_CATEGORIES.map((cat, i) => {
  96. const icons = cat.icons.filter((n) => selectable.includes(n))
  97. if (icons.length === 0) return null
  98. const open = !collapsed[cat.id]
  99. return (
  100. <div key={cat.id} className="space-y-1.5">
  101. <button
  102. type="button"
  103. onClick={() => setCollapsed((s) => ({ ...s, [cat.id]: !s[cat.id] }))}
  104. aria-expanded={open}
  105. className="flex w-full items-center justify-between rounded-md px-1 py-0.5 text-[11px] font-medium uppercase tracking-wide text-text-muted transition-colors hover:bg-hover hover:text-text-secondary"
  106. >
  107. <span>{t(`devices.edit.iconCategories.${cat.id}`, { defaultValue: cat.id })}</span>
  108. <span className="flex items-center gap-1">
  109. {!open && <span className="text-[10px] normal-case">{icons.length}</span>}
  110. <ChevronDown
  111. className={cn('h-3.5 w-3.5 transition-transform', !open && '-rotate-90')}
  112. strokeWidth={1.75}
  113. />
  114. </span>
  115. </button>
  116. {open && (
  117. <div className={cn('grid gap-2', gridCols)}>
  118. {i === 0 && autoCell}
  119. {icons.map(cell)}
  120. </div>
  121. )}
  122. </div>
  123. )
  124. })
  125. )}
  126. </div>
  127. </div>
  128. )
  129. }