@asteby/metacore-runtime-react 53.26.0 → 53.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/action-modal-dispatcher.d.ts.map +1 -1
- package/dist/action-modal-dispatcher.js +5 -0
- package/dist/business/customer-picker.d.ts +10 -1
- package/dist/business/customer-picker.d.ts.map +1 -1
- package/dist/business/customer-picker.js +3 -2
- package/dist/dialogs/create-record-dialog.d.ts +1 -1
- package/dist/dialogs/create-record-dialog.d.ts.map +1 -1
- package/dist/dialogs/create-record-dialog.js +2 -2
- package/dist/dialogs/dynamic-record.d.ts +13 -1
- package/dist/dialogs/dynamic-record.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.js +41 -23
- package/dist/dialogs/types.d.ts +2 -0
- package/dist/dialogs/types.d.ts.map +1 -1
- package/dist/dynamic-crud-page.d.ts.map +1 -1
- package/dist/dynamic-crud-page.js +3 -0
- package/dist/dynamic-form.d.ts.map +1 -1
- package/dist/dynamic-form.js +13 -5
- package/dist/dynamic-line-items.d.ts.map +1 -1
- package/dist/dynamic-line-items.js +5 -0
- package/dist/dynamic-multi-select-field.d.ts.map +1 -1
- package/dist/dynamic-multi-select-field.js +5 -5
- package/dist/field-widget-registry.d.ts +6 -0
- package/dist/field-widget-registry.d.ts.map +1 -1
- package/dist/host-api-client.d.ts +17 -0
- package/dist/host-api-client.d.ts.map +1 -1
- package/dist/host-api-client.js +10 -4
- package/dist/host-api.d.ts +14 -0
- package/dist/host-api.d.ts.map +1 -1
- package/dist/host-api.js +23 -3
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/model-labels.d.ts +26 -0
- package/dist/model-labels.d.ts.map +1 -1
- package/dist/model-labels.js +22 -5
- package/dist/record-picker-dialog.d.ts +4 -1
- package/dist/record-picker-dialog.d.ts.map +1 -1
- package/dist/record-picker-dialog.js +2 -2
- package/dist/record-picker.d.ts +11 -0
- package/dist/record-picker.d.ts.map +1 -1
- package/dist/record-picker.js +17 -4
- package/dist/searchable-select-field.d.ts +33 -0
- package/dist/searchable-select-field.d.ts.map +1 -0
- package/dist/searchable-select-field.js +50 -0
- package/dist/searchable-select.d.ts +45 -0
- package/dist/searchable-select.d.ts.map +1 -0
- package/dist/searchable-select.js +72 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/use-host-api-client.js +1 -1
- package/package.json +5 -5
- package/src/__tests__/dynamic-record-texts.test.tsx +22 -0
- package/src/__tests__/field-widget-set-values.test.tsx +65 -0
- package/src/__tests__/host-api-client.test.tsx +12 -0
- package/src/__tests__/host-api.test.ts +41 -0
- package/src/__tests__/model-labels.test.tsx +41 -1
- package/src/__tests__/record-picker-open-on-focus.test.tsx +49 -0
- package/src/__tests__/searchable-select.test.tsx +247 -0
- package/src/action-modal-dispatcher.tsx +16 -0
- package/src/business/customer-picker.tsx +13 -0
- package/src/dialogs/create-record-dialog.tsx +2 -0
- package/src/dialogs/dynamic-record.tsx +74 -26
- package/src/dialogs/types.ts +2 -0
- package/src/dynamic-crud-page.tsx +3 -0
- package/src/dynamic-form.tsx +26 -2
- package/src/dynamic-line-items.tsx +17 -0
- package/src/dynamic-multi-select-field.tsx +5 -4
- package/src/field-widget-registry.tsx +6 -0
- package/src/host-api-client.ts +24 -5
- package/src/host-api.ts +28 -3
- package/src/index.ts +4 -2
- package/src/model-labels.ts +45 -5
- package/src/record-picker-dialog.tsx +5 -0
- package/src/record-picker.tsx +42 -3
- package/src/searchable-select-field.tsx +134 -0
- package/src/searchable-select.ts +87 -0
- package/src/types.ts +22 -0
- package/src/use-host-api-client.ts +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { resolveModelLabels, defaultSingular } from '../model-labels'
|
|
2
|
+
import { resolveModelLabels, defaultSingular, resolveModelNoun, modelVerbTitle } from '../model-labels'
|
|
3
3
|
|
|
4
4
|
const makeT = (dict: Record<string, string>) => (key: string, opts?: Record<string, unknown>) =>
|
|
5
5
|
dict[key] ?? (opts?.defaultValue as string | undefined) ?? key
|
|
@@ -29,3 +29,43 @@ describe('resolveModelLabels', () => {
|
|
|
29
29
|
})
|
|
30
30
|
it('singular helper', () => expect(defaultSingular('orders')).toBe('Order'))
|
|
31
31
|
})
|
|
32
|
+
|
|
33
|
+
describe('backend-declared names (singular_label / new_label / gender)', () => {
|
|
34
|
+
it('singular_label replaces the naive title-minus-s derivation', () => {
|
|
35
|
+
const l = resolveModelLabels(makeT({ 'common.new': 'Nuevo' }), {
|
|
36
|
+
model: 'addresses', title: 'Direcciones', singularLabel: 'dirección',
|
|
37
|
+
})
|
|
38
|
+
expect(l.singular).toBe('dirección')
|
|
39
|
+
expect(l.newLabel).toBe('Nuevo dirección')
|
|
40
|
+
})
|
|
41
|
+
it('gender picks common.new_<gender> before common.new', () => {
|
|
42
|
+
const t = makeT({ 'common.new': 'Nuevo', 'common.new_f': 'Nueva', 'common.new_m': 'Nuevo' })
|
|
43
|
+
expect(resolveModelLabels(t, { model: 'addresses', title: 'Direcciones', singularLabel: 'dirección', gender: 'f' }).newLabel)
|
|
44
|
+
.toBe('Nueva dirección')
|
|
45
|
+
expect(resolveModelLabels(t, { model: 'x', title: 'X', singularLabel: 'cliente', gender: 'm' }).newLabel).toBe('Nuevo cliente')
|
|
46
|
+
})
|
|
47
|
+
it('backend new_label is used as is; host models.<model>.new still wins', () => {
|
|
48
|
+
expect(resolveModelLabels(makeT({}), { model: 'x', title: 'X', newLabelMeta: 'Nueva figura' }).newLabel).toBe('Nueva figura')
|
|
49
|
+
expect(resolveModelLabels(makeT({ 'models.x.new': 'Alta' }), { model: 'x', title: 'X', newLabelMeta: 'Nueva figura' }).newLabel).toBe('Alta')
|
|
50
|
+
})
|
|
51
|
+
it('host models.<model>.singular overrides the backend singular_label', () => {
|
|
52
|
+
const l = resolveModelLabels(makeT({ 'models.x.singular': 'Socio' }), { model: 'x', title: 'X', singularLabel: 'cliente' })
|
|
53
|
+
expect(l.singular).toBe('Socio')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
describe('resolveModelNoun / modelVerbTitle', () => {
|
|
58
|
+
it('order: host singular > singular_label > titleKey > title', () => {
|
|
59
|
+
expect(resolveModelNoun(makeT({ 'models.c.singular': 'Cliente' }), { model: 'c', singularLabel: 'cliente', title: 'Client' })).toBe('Cliente')
|
|
60
|
+
expect(resolveModelNoun(makeT({}), { model: 'c', singularLabel: 'cliente', title: 'Client' })).toBe('cliente')
|
|
61
|
+
expect(resolveModelNoun(makeT({ 'k.t': 'Socio' }), { model: 'c', titleKey: 'k.t', title: 'Client' })).toBe('Socio')
|
|
62
|
+
expect(resolveModelNoun(makeT({}), { model: 'c', title: 'Client' })).toBe('Client')
|
|
63
|
+
expect(resolveModelNoun(makeT({}), { model: 'c' })).toBe('')
|
|
64
|
+
})
|
|
65
|
+
it('verb titles are i18n-able with a Spanish default', () => {
|
|
66
|
+
const t = (k: string, o?: Record<string, unknown>) =>
|
|
67
|
+
k === 'dynamic.edit_title' ? `Edit ${o?.name}` : (o?.defaultValue as string)
|
|
68
|
+
expect(modelVerbTitle(t, 'edit', 'customer', 'Editar customer')).toBe('Edit customer')
|
|
69
|
+
expect(modelVerbTitle(t, 'create', 'cliente', 'Crear cliente')).toBe('Crear cliente')
|
|
70
|
+
})
|
|
71
|
+
})
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
//
|
|
3
|
+
// RecordPicker trigger="input" + openOnFocus: la lista se abre al enfocar el cuadro
|
|
4
|
+
// (sin teclear ni hacer clic aparte); sin la opcion queda cerrada como antes.
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
6
|
+
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
|
7
|
+
|
|
8
|
+
vi.mock('react-i18next', () => {
|
|
9
|
+
const t = (k: string, o?: Record<string, any>) => o?.defaultValue ?? k
|
|
10
|
+
const value = { t, i18n: { language: 'es' } }
|
|
11
|
+
return { useTranslation: () => value }
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
import { RecordPicker } from '../record-picker'
|
|
15
|
+
|
|
16
|
+
afterEach(cleanup)
|
|
17
|
+
const rows = [
|
|
18
|
+
{ id: 'a', name: 'Alfa' },
|
|
19
|
+
{ id: 'b', name: 'Beta' },
|
|
20
|
+
]
|
|
21
|
+
function Picker({ openOnFocus }: { openOnFocus?: boolean }) {
|
|
22
|
+
return (
|
|
23
|
+
<RecordPicker<(typeof rows)[number]>
|
|
24
|
+
trigger="input"
|
|
25
|
+
items={rows}
|
|
26
|
+
minChars={0}
|
|
27
|
+
getKey={(r) => r.id}
|
|
28
|
+
getLabel={(r) => r.name}
|
|
29
|
+
value={null}
|
|
30
|
+
selected={null}
|
|
31
|
+
onSelect={() => {}}
|
|
32
|
+
openOnFocus={openOnFocus}
|
|
33
|
+
entityLabel="Elemento"
|
|
34
|
+
/>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('RecordPicker openOnFocus', () => {
|
|
39
|
+
it('abre la lista al enfocar', async () => {
|
|
40
|
+
render(<Picker openOnFocus />)
|
|
41
|
+
fireEvent.focus(screen.getByRole('combobox'))
|
|
42
|
+
expect(await screen.findByText('Alfa')).toBeTruthy()
|
|
43
|
+
})
|
|
44
|
+
it('sin openOnFocus no abre al enfocar', () => {
|
|
45
|
+
render(<Picker />)
|
|
46
|
+
fireEvent.focus(screen.getByRole('combobox'))
|
|
47
|
+
expect(screen.queryByText('Alfa')).toBeNull()
|
|
48
|
+
})
|
|
49
|
+
})
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
//
|
|
3
|
+
// Searchable select: a STATIC `select` with many options (or `searchable: true`)
|
|
4
|
+
// renders a combobox with a search box instead of a flat dropdown.
|
|
5
|
+
// - pure rules: threshold, accent/case-insensitive filter by label, code and
|
|
6
|
+
// description, ranking, windowing;
|
|
7
|
+
// - the component: filters while typing, highlights, "Sin resultados",
|
|
8
|
+
// keyboard (arrows / Enter / Esc), selection mark, clear when optional;
|
|
9
|
+
// - wiring: EditField (DynamicRecordDialog) and DynamicForm choose combobox vs
|
|
10
|
+
// flat select by option count and `searchable`.
|
|
11
|
+
import { useState } from 'react'
|
|
12
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
13
|
+
import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'
|
|
14
|
+
|
|
15
|
+
vi.mock('react-i18next', () => {
|
|
16
|
+
const t = (k: string, o?: Record<string, any>) => o?.defaultValue ?? k
|
|
17
|
+
const value = { t, i18n: { language: 'es' } }
|
|
18
|
+
return { useTranslation: () => value }
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
SEARCHABLE_SELECT_THRESHOLD,
|
|
23
|
+
filterSearchableOptions,
|
|
24
|
+
isSearchableSelect,
|
|
25
|
+
windowOptions,
|
|
26
|
+
} from '../searchable-select'
|
|
27
|
+
import { SearchableSelectField } from '../searchable-select-field'
|
|
28
|
+
import { EditField } from '../dialogs/dynamic-record'
|
|
29
|
+
import { DynamicForm } from '../dynamic-form'
|
|
30
|
+
|
|
31
|
+
afterEach(cleanup)
|
|
32
|
+
|
|
33
|
+
const REGIMENES = [
|
|
34
|
+
['601', 'General de Ley Personas Morales'],
|
|
35
|
+
['603', 'Personas Morales con Fines no Lucrativos'],
|
|
36
|
+
['605', 'Sueldos y Salarios e Ingresos Asimilados a Salarios'],
|
|
37
|
+
['606', 'Arrendamiento'],
|
|
38
|
+
['607', 'Régimen de Enajenación o Adquisición de Bienes'],
|
|
39
|
+
['608', 'Demás ingresos'],
|
|
40
|
+
['610', 'Residentes en el Extranjero sin Establecimiento Permanente en México'],
|
|
41
|
+
['611', 'Ingresos por Dividendos (socios y accionistas)'],
|
|
42
|
+
['612', 'Personas Físicas con Actividades Empresariales y Profesionales'],
|
|
43
|
+
['614', 'Ingresos por intereses'],
|
|
44
|
+
['615', 'Régimen de los ingresos por obtención de premios'],
|
|
45
|
+
['616', 'Sin obligaciones fiscales'],
|
|
46
|
+
['620', 'Sociedades Cooperativas de Producción que optan por diferir sus ingresos'],
|
|
47
|
+
['621', 'Incorporación Fiscal'],
|
|
48
|
+
['622', 'Actividades Agrícolas, Ganaderas, Silvícolas y Pesqueras'],
|
|
49
|
+
['623', 'Opcional para Grupos de Sociedades'],
|
|
50
|
+
['624', 'Coordinados'],
|
|
51
|
+
['625', 'Actividades Empresariales con ingresos a través de Plataformas Tecnológicas'],
|
|
52
|
+
['626', 'Régimen Simplificado de Confianza'],
|
|
53
|
+
['628', 'Hidrocarburos'],
|
|
54
|
+
['629', 'De los Regímenes Fiscales Preferentes y de las Empresas Multinacionales'],
|
|
55
|
+
['630', 'Enajenación de acciones en bolsa de valores'],
|
|
56
|
+
].map(([value, label]) => ({ value, label: `${value} - ${label}` }))
|
|
57
|
+
|
|
58
|
+
describe('isSearchableSelect (umbral)', () => {
|
|
59
|
+
it('por defecto solo por encima de 8 opciones', () => {
|
|
60
|
+
expect(SEARCHABLE_SELECT_THRESHOLD).toBe(8)
|
|
61
|
+
expect(isSearchableSelect({}, 8)).toBe(false)
|
|
62
|
+
expect(isSearchableSelect({}, 9)).toBe(true)
|
|
63
|
+
expect(isSearchableSelect(undefined, 3)).toBe(false)
|
|
64
|
+
})
|
|
65
|
+
it('searchable explícito gana en ambos sentidos', () => {
|
|
66
|
+
expect(isSearchableSelect({ searchable: true }, 2)).toBe(true)
|
|
67
|
+
expect(isSearchableSelect({ searchable: false }, 500)).toBe(false)
|
|
68
|
+
})
|
|
69
|
+
it('searchThreshold por campo; valores inválidos caen al default', () => {
|
|
70
|
+
expect(isSearchableSelect({ searchThreshold: 3 }, 4)).toBe(true)
|
|
71
|
+
expect(isSearchableSelect({ searchThreshold: 30 }, 25)).toBe(false)
|
|
72
|
+
expect(isSearchableSelect({ searchThreshold: -1 }, 5)).toBe(false)
|
|
73
|
+
expect(isSearchableSelect({ searchThreshold: Number.NaN }, 9)).toBe(true)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe('filterSearchableOptions', () => {
|
|
78
|
+
it('encuentra por código, palabra y sin acentos', () => {
|
|
79
|
+
expect(filterSearchableOptions(REGIMENES, '630').map((o) => o.value)).toEqual(['630'])
|
|
80
|
+
expect(filterSearchableOptions(REGIMENES, 'BOLSA').map((o) => o.value)).toEqual(['630'])
|
|
81
|
+
expect(filterSearchableOptions(REGIMENES, 'enajenacion').map((o) => o.value)).toEqual(['607', '630'])
|
|
82
|
+
expect(filterSearchableOptions(REGIMENES, 'regimen simplificado').map((o) => o.value)).toEqual(['626'])
|
|
83
|
+
})
|
|
84
|
+
it('busca también en description', () => {
|
|
85
|
+
const opts = [
|
|
86
|
+
{ value: 'a', label: 'Alpha', description: 'primer elemento' },
|
|
87
|
+
{ value: 'b', label: 'Beta', description: 'segundo' },
|
|
88
|
+
]
|
|
89
|
+
expect(filterSearchableOptions(opts, 'SEGUNDO').map((o) => o.value)).toEqual(['b'])
|
|
90
|
+
})
|
|
91
|
+
it('sin resultados devuelve vacío; query vacía no filtra ni muta', () => {
|
|
92
|
+
expect(filterSearchableOptions(REGIMENES, 'zzzz')).toEqual([])
|
|
93
|
+
const all = filterSearchableOptions(REGIMENES, ' ')
|
|
94
|
+
expect(all).toHaveLength(REGIMENES.length)
|
|
95
|
+
expect(all).not.toBe(REGIMENES)
|
|
96
|
+
})
|
|
97
|
+
it('ordena: empieza con la consulta antes que contiene', () => {
|
|
98
|
+
const opts = [
|
|
99
|
+
{ value: '1', label: 'Fiscal general' },
|
|
100
|
+
{ value: '2', label: 'General de ley' },
|
|
101
|
+
{ value: '3', label: 'Regimen general' },
|
|
102
|
+
]
|
|
103
|
+
expect(filterSearchableOptions(opts, 'general').map((o) => o.value)).toEqual(['2', '1', '3'])
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
describe('windowOptions', () => {
|
|
108
|
+
it('no recorta bajo el tope; recorta y conserva la opción seleccionada', () => {
|
|
109
|
+
const rows = Array.from({ length: 300 }, (_, i) => i)
|
|
110
|
+
expect(windowOptions(rows.slice(0, 50), 100, () => false)).toEqual({ rows: rows.slice(0, 50), hidden: 0 })
|
|
111
|
+
const w = windowOptions(rows, 100, (n) => n === 250)
|
|
112
|
+
expect(w.rows).toHaveLength(101)
|
|
113
|
+
expect(w.rows).toContain(250)
|
|
114
|
+
expect(w.hidden).toBe(199)
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
function Harness({ required = false, initial = '' }: { required?: boolean; initial?: string }) {
|
|
119
|
+
const [v, setV] = useState(initial)
|
|
120
|
+
return (
|
|
121
|
+
<>
|
|
122
|
+
<SearchableSelectField
|
|
123
|
+
id="regimen"
|
|
124
|
+
ariaLabel="Régimen fiscal"
|
|
125
|
+
options={REGIMENES}
|
|
126
|
+
value={v}
|
|
127
|
+
onChange={setV}
|
|
128
|
+
required={required}
|
|
129
|
+
/>
|
|
130
|
+
<output data-testid="value">{v}</output>
|
|
131
|
+
</>
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const open = () => fireEvent.click(screen.getByRole('combobox', { name: 'Régimen fiscal' }))
|
|
136
|
+
const search = () => screen.getAllByRole('combobox').find((el) => el.tagName === 'INPUT') as HTMLInputElement
|
|
137
|
+
const type = (text: string) => fireEvent.change(search(), { target: { value: text } })
|
|
138
|
+
|
|
139
|
+
describe('SearchableSelectField', () => {
|
|
140
|
+
it('abre con buscador, filtra por código y por palabra sin acentos, y resalta', () => {
|
|
141
|
+
render(<Harness />)
|
|
142
|
+
open()
|
|
143
|
+
expect(screen.getAllByRole('option').length).toBe(REGIMENES.length)
|
|
144
|
+
type('630')
|
|
145
|
+
const opts = screen.getAllByRole('option')
|
|
146
|
+
expect(opts).toHaveLength(1)
|
|
147
|
+
expect(opts[0]!.textContent).toContain('Enajenación de acciones en bolsa de valores')
|
|
148
|
+
expect(opts[0]!.querySelector('mark')?.textContent).toBe('630')
|
|
149
|
+
type('bolsa')
|
|
150
|
+
expect(screen.getAllByRole('option')).toHaveLength(1)
|
|
151
|
+
expect(screen.getByRole('option').querySelector('mark')?.textContent).toBe('bolsa')
|
|
152
|
+
type('ENAJENACION')
|
|
153
|
+
expect(screen.getAllByRole('option')).toHaveLength(2)
|
|
154
|
+
expect(screen.getAllByRole('option')[1]!.querySelector('mark')?.textContent).toBe('Enajenación')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('muestra "Sin resultados" cuando nada coincide', () => {
|
|
158
|
+
render(<Harness />)
|
|
159
|
+
open()
|
|
160
|
+
type('qwerty')
|
|
161
|
+
expect(screen.queryAllByRole('option')).toHaveLength(0)
|
|
162
|
+
expect(screen.getByText('Sin resultados')).toBeTruthy()
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('teclado: flechas + Enter eligen, Esc cierra, ARIA listbox/combobox', () => {
|
|
166
|
+
render(<Harness />)
|
|
167
|
+
open()
|
|
168
|
+
const input = search()
|
|
169
|
+
expect(input.getAttribute('aria-autocomplete')).toBe('list')
|
|
170
|
+
expect(screen.getByRole('listbox', { name: 'Régimen fiscal' })).toBeTruthy()
|
|
171
|
+
type('ingresos')
|
|
172
|
+
fireEvent.keyDown(input, { key: 'ArrowDown' })
|
|
173
|
+
const active = input.getAttribute('aria-activedescendant')
|
|
174
|
+
expect(active).toBeTruthy()
|
|
175
|
+
expect(document.getElementById(active!)?.getAttribute('data-active')).toBe('true')
|
|
176
|
+
fireEvent.keyDown(input, { key: 'Enter' })
|
|
177
|
+
expect(screen.getByTestId('value').textContent).not.toBe('')
|
|
178
|
+
expect(screen.queryByRole('listbox')).toBeNull()
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('elegir marca la opción y reabrir la deja seleccionada y activa', () => {
|
|
182
|
+
render(<Harness initial="626" />)
|
|
183
|
+
const trigger = screen.getByRole('combobox', { name: 'Régimen fiscal' })
|
|
184
|
+
expect(trigger.textContent).toContain('626 - Régimen Simplificado de Confianza')
|
|
185
|
+
fireEvent.click(trigger)
|
|
186
|
+
const sel = screen.getAllByRole('option').find((o) => o.getAttribute('data-selected') === 'true')
|
|
187
|
+
expect(sel?.textContent).toContain('626')
|
|
188
|
+
expect(sel?.getAttribute('data-active')).toBe('true')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('limpiar solo cuando no es requerido', () => {
|
|
192
|
+
const { unmount } = render(<Harness initial="601" />)
|
|
193
|
+
fireEvent.click(screen.getByLabelText('Quitar selección'))
|
|
194
|
+
expect(screen.getByTestId('value').textContent).toBe('')
|
|
195
|
+
unmount()
|
|
196
|
+
render(<Harness initial="601" required />)
|
|
197
|
+
expect(screen.queryByLabelText('Quitar selección')).toBeNull()
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
it('lista enorme: se ventanea y avisa cuántas hay', () => {
|
|
201
|
+
const big = Array.from({ length: 400 }, (_, i) => ({ value: `v${i}`, label: `Opción ${i}` }))
|
|
202
|
+
render(<SearchableSelectField options={big} value="v399" onChange={() => {}} ariaLabel="Grande" />)
|
|
203
|
+
fireEvent.click(screen.getByRole('combobox', { name: 'Grande' }))
|
|
204
|
+
expect(screen.getAllByRole('option')).toHaveLength(101)
|
|
205
|
+
expect(screen.getByText(/Mostrando 101 de 400/)).toBeTruthy()
|
|
206
|
+
expect(screen.getAllByRole('option').some((o) => o.getAttribute('data-selected') === 'true')).toBe(true)
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
describe('cableado por umbral', () => {
|
|
211
|
+
const opts = (n: number) => Array.from({ length: n }, (_, i) => ({ value: `v${i}`, label: `Opción ${i}` }))
|
|
212
|
+
|
|
213
|
+
it('EditField: 25 opciones => combobox con buscador', () => {
|
|
214
|
+
render(<EditField field={{ key: 'regimen', label: 'Régimen fiscal', type: 'select', options: REGIMENES }} value="" onChange={() => {}} />)
|
|
215
|
+
fireEvent.click(screen.getByRole('combobox'))
|
|
216
|
+
expect(search()).toBeTruthy()
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
it('EditField: pocas opciones => select plano (sin buscador)', () => {
|
|
220
|
+
render(<EditField field={{ key: 'k', label: 'K', type: 'select', options: opts(5) }} value="" onChange={() => {}} />)
|
|
221
|
+
const trigger = screen.getByRole('combobox')
|
|
222
|
+
expect(trigger.getAttribute('aria-haspopup')).not.toBe('listbox') // Radix Select, no RecordPicker
|
|
223
|
+
expect(trigger.closest('[data-slot="searchable-select"]')).toBeNull()
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
it('EditField: searchable true fuerza el buscador con pocas; false lo quita con muchas', () => {
|
|
227
|
+
const { unmount } = render(
|
|
228
|
+
<EditField field={{ key: 'k', label: 'K', type: 'select', options: opts(3), searchable: true }} value="" onChange={() => {}} />,
|
|
229
|
+
)
|
|
230
|
+
expect(document.querySelector('[data-slot="searchable-select"]')).toBeTruthy()
|
|
231
|
+
unmount()
|
|
232
|
+
render(<EditField field={{ key: 'k', label: 'K', type: 'select', options: opts(40), searchable: false }} value="" onChange={() => {}} />)
|
|
233
|
+
expect(document.querySelector('[data-slot="searchable-select"]')).toBeNull()
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('DynamicForm: el campo select largo usa el combobox', () => {
|
|
237
|
+
render(
|
|
238
|
+
<DynamicForm
|
|
239
|
+
fields={[{ key: 'regimen', label: 'Régimen fiscal', type: 'select', options: REGIMENES }]}
|
|
240
|
+
onSubmit={() => {}}
|
|
241
|
+
/>,
|
|
242
|
+
)
|
|
243
|
+
const scope = within(document.body)
|
|
244
|
+
expect(scope.getByRole('combobox', { name: 'Régimen fiscal' })).toBeTruthy()
|
|
245
|
+
expect(document.querySelector('[data-slot="searchable-select"]')).toBeTruthy()
|
|
246
|
+
})
|
|
247
|
+
})
|
|
@@ -67,6 +67,8 @@ import { DynamicIcon } from './dynamic-icon'
|
|
|
67
67
|
import { DynamicLineItems } from './dynamic-line-items'
|
|
68
68
|
import { DynamicRelations } from './dynamic-relations'
|
|
69
69
|
import { DynamicSelectField } from './dynamic-select-field'
|
|
70
|
+
import { SearchableSelectField } from './searchable-select-field'
|
|
71
|
+
import { isSearchableSelect } from './searchable-select'
|
|
70
72
|
import { DynamicDateField } from './dynamic-date-field'
|
|
71
73
|
import { UploadField } from './upload-field'
|
|
72
74
|
import { isLineItemsField, resolveWidget, resolveDependsValue, getDependsOn, getFieldRef, applyOptionWhen } from './dynamic-form-schema'
|
|
@@ -1884,6 +1886,20 @@ export function renderField(
|
|
|
1884
1886
|
const stale = !!value && !options.some((o) => String(o.value) === String(value))
|
|
1885
1887
|
const selectInvalid = invalid || stale
|
|
1886
1888
|
const selectCls = selectInvalid ? 'border-destructive ring-1 ring-destructive/30 focus-visible:ring-destructive' : ''
|
|
1889
|
+
if (isSearchableSelect(field, options.length)) {
|
|
1890
|
+
return (
|
|
1891
|
+
<SearchableSelectField
|
|
1892
|
+
id={field.key}
|
|
1893
|
+
ariaLabel={field.label || field.key}
|
|
1894
|
+
options={options}
|
|
1895
|
+
value={stale ? '' : value}
|
|
1896
|
+
onChange={onChange}
|
|
1897
|
+
placeholder={field.placeholder || undefined}
|
|
1898
|
+
required={!!field.required}
|
|
1899
|
+
invalid={selectInvalid}
|
|
1900
|
+
/>
|
|
1901
|
+
)
|
|
1902
|
+
}
|
|
1887
1903
|
return (
|
|
1888
1904
|
<Select value={stale ? '' : value || ''} onValueChange={onChange}>
|
|
1889
1905
|
<SelectTrigger className={'w-full' + (selectCls ? ` ${selectCls}` : '')} aria-invalid={selectInvalid || undefined} data-stale-option={stale || undefined}><SelectValue placeholder={field.placeholder || 'Seleccionar...'} /></SelectTrigger>
|
|
@@ -7,6 +7,7 @@ import { useTranslation } from 'react-i18next'
|
|
|
7
7
|
import { Badge } from '@asteby/metacore-ui'
|
|
8
8
|
import { RecordPicker, useLatestSearch } from '../record-picker'
|
|
9
9
|
import { useRecordPickerDialog } from '../record-picker-dialog'
|
|
10
|
+
import type { FieldDef } from '../dialogs/dynamic-record'
|
|
10
11
|
import { useCan } from '../permissions-context'
|
|
11
12
|
import { useFormatter } from './format'
|
|
12
13
|
|
|
@@ -46,6 +47,14 @@ export interface CustomerPickerProps {
|
|
|
46
47
|
canEdit?: boolean
|
|
47
48
|
/** Valores iniciales del alta rápida (p. ej. el texto buscado como nombre). */
|
|
48
49
|
createDefaults?: Record<string, unknown>
|
|
50
|
+
/**
|
|
51
|
+
* Campos extra del modal de alta/edicion (se fusionan al esquema servido). Con
|
|
52
|
+
* `widget: '<nombre>'` registrado via `registerFieldWidget`, el widget puede llenar
|
|
53
|
+
* el resto del formulario con `setValues` (p. ej. precarga desde un documento).
|
|
54
|
+
*/
|
|
55
|
+
ensureFields?: FieldDef[]
|
|
56
|
+
/** Abre la lista al enfocar el cuadro de busqueda (ver `RecordPicker.openOnFocus`). */
|
|
57
|
+
openOnFocus?: boolean
|
|
49
58
|
currency?: string
|
|
50
59
|
disabled?: boolean
|
|
51
60
|
placeholder?: string
|
|
@@ -72,6 +81,8 @@ export function CustomerPicker({
|
|
|
72
81
|
canCreate,
|
|
73
82
|
canEdit,
|
|
74
83
|
createDefaults,
|
|
84
|
+
ensureFields,
|
|
85
|
+
openOnFocus,
|
|
75
86
|
currency,
|
|
76
87
|
disabled,
|
|
77
88
|
placeholder,
|
|
@@ -103,6 +114,7 @@ export function CustomerPicker({
|
|
|
103
114
|
endpoint,
|
|
104
115
|
prefillField: 'name',
|
|
105
116
|
createDefaults,
|
|
117
|
+
ensureFields,
|
|
106
118
|
onSaved: (rec, kind) => {
|
|
107
119
|
const next = toResult(rec)
|
|
108
120
|
// Al editar se conservan saldo/crédito/alertas que el registro no trae.
|
|
@@ -139,6 +151,7 @@ export function CustomerPicker({
|
|
|
139
151
|
<>
|
|
140
152
|
<RecordPicker<CustomerResult>
|
|
141
153
|
trigger="input"
|
|
154
|
+
openOnFocus={openOnFocus}
|
|
142
155
|
slot="customer-picker"
|
|
143
156
|
search={searchFn}
|
|
144
157
|
minChars={2}
|
|
@@ -24,6 +24,7 @@ export function CreateRecordDialog({
|
|
|
24
24
|
schema,
|
|
25
25
|
defaults,
|
|
26
26
|
lockedFields,
|
|
27
|
+
ensureFields,
|
|
27
28
|
onCreate,
|
|
28
29
|
onUpdate,
|
|
29
30
|
onSaved,
|
|
@@ -40,6 +41,7 @@ export function CreateRecordDialog({
|
|
|
40
41
|
schema={schema}
|
|
41
42
|
defaults={defaults}
|
|
42
43
|
lockedFields={lockedFields}
|
|
44
|
+
ensureFields={ensureFields}
|
|
43
45
|
onCreate={onCreate}
|
|
44
46
|
onUpdate={onUpdate}
|
|
45
47
|
onSaved={onSaved}
|