@asteby/metacore-runtime-react 53.26.0 → 53.27.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 +12 -0
- package/dist/action-modal-dispatcher.d.ts.map +1 -1
- package/dist/action-modal-dispatcher.js +5 -0
- package/dist/dialogs/dynamic-record.d.ts +10 -0
- package/dist/dialogs/dynamic-record.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.js +6 -0
- package/dist/dynamic-form.d.ts.map +1 -1
- package/dist/dynamic-form.js +7 -0
- 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/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 +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/record-picker.d.ts +5 -0
- package/dist/record-picker.d.ts.map +1 -1
- package/dist/record-picker.js +14 -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 +12 -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__/host-api-client.test.tsx +12 -0
- package/src/__tests__/host-api.test.ts +41 -0
- package/src/__tests__/searchable-select.test.tsx +247 -0
- package/src/action-modal-dispatcher.tsx +16 -0
- package/src/dialogs/dynamic-record.tsx +26 -0
- package/src/dynamic-form.tsx +17 -0
- package/src/dynamic-line-items.tsx +17 -0
- package/src/dynamic-multi-select-field.tsx +5 -4
- package/src/host-api-client.ts +24 -5
- package/src/host-api.ts +28 -3
- package/src/index.ts +1 -1
- package/src/record-picker.tsx +32 -3
- package/src/searchable-select-field.tsx +134 -0
- package/src/searchable-select.ts +87 -0
- package/src/types.ts +12 -0
- package/src/use-host-api-client.ts +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// SearchableSelectField - the combobox-with-search that replaces the flat
|
|
2
|
+
// <Select> when a STATIC option list is long (a tax-regime catalog with ~25
|
|
3
|
+
// entries, a country list...), so the user types instead of scrolling.
|
|
4
|
+
//
|
|
5
|
+
// It is a thin configuration of the shared <RecordPicker> (the same control
|
|
6
|
+
// DynamicSelectField and EntitySelect use), so it inherits ARIA combobox +
|
|
7
|
+
// listbox with aria-activedescendant, arrows/Home/End/Enter/Esc/Tab, type-ahead
|
|
8
|
+
// on the closed trigger, scroll-to-selected on open, a portaled collision-aware
|
|
9
|
+
// popover and light/dark theming. What it adds:
|
|
10
|
+
// - client-side filtering by label, code (value) and `description`, ignoring
|
|
11
|
+
// case and accents (a code, a word or an unaccented word find the same row);
|
|
12
|
+
// - match highlighting, a "no results" state and a windowed list when the
|
|
13
|
+
// catalog is huge;
|
|
14
|
+
// - a clear (x) when the field is not required.
|
|
15
|
+
//
|
|
16
|
+
// Who renders it: DynamicForm / DynamicRecordDialog / ActionModal / line items,
|
|
17
|
+
// via {@link isSearchableSelect}: `field.searchable === true`, or - unless
|
|
18
|
+
// `searchable === false` - more than SEARCHABLE_SELECT_THRESHOLD options.
|
|
19
|
+
import { useMemo, useState } from 'react'
|
|
20
|
+
import { RecordPicker } from './record-picker'
|
|
21
|
+
import { OptionLead } from './record-picker-option'
|
|
22
|
+
import type { ResolvedOption } from './use-options-resolver'
|
|
23
|
+
import { SEARCHABLE_SELECT_MAX_ROWS, filterSearchableOptions, windowOptions } from './searchable-select'
|
|
24
|
+
|
|
25
|
+
export interface SearchableSelectOption {
|
|
26
|
+
value: string | number
|
|
27
|
+
label: string
|
|
28
|
+
description?: string | null
|
|
29
|
+
color?: string
|
|
30
|
+
icon?: string
|
|
31
|
+
image?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SearchableSelectFieldProps {
|
|
35
|
+
options: readonly SearchableSelectOption[]
|
|
36
|
+
/** Current value (string | number); empty / nullish = nothing selected. */
|
|
37
|
+
value: unknown
|
|
38
|
+
onChange: (value: string) => void
|
|
39
|
+
id?: string
|
|
40
|
+
/** Accessible name (the field label). */
|
|
41
|
+
ariaLabel?: string
|
|
42
|
+
placeholder?: string
|
|
43
|
+
/** Required fields cannot be cleared. */
|
|
44
|
+
required?: boolean
|
|
45
|
+
disabled?: boolean
|
|
46
|
+
invalid?: boolean
|
|
47
|
+
/** `cell` = compact variant for table / line-item cells. */
|
|
48
|
+
variant?: 'field' | 'cell'
|
|
49
|
+
/** Overrides the "Buscar..." search placeholder. */
|
|
50
|
+
searchPlaceholder?: string
|
|
51
|
+
/** Overrides the "Sin resultados" copy. */
|
|
52
|
+
emptyText?: string
|
|
53
|
+
maxRows?: number
|
|
54
|
+
triggerClassName?: string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const toResolved = (o: SearchableSelectOption): ResolvedOption => ({
|
|
58
|
+
id: String(o.value),
|
|
59
|
+
value: String(o.value),
|
|
60
|
+
label: o.label,
|
|
61
|
+
name: o.label,
|
|
62
|
+
description: o.description ?? undefined,
|
|
63
|
+
color: o.color,
|
|
64
|
+
icon: o.icon,
|
|
65
|
+
image: o.image,
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
export function SearchableSelectField({
|
|
69
|
+
options,
|
|
70
|
+
value,
|
|
71
|
+
onChange,
|
|
72
|
+
id,
|
|
73
|
+
ariaLabel,
|
|
74
|
+
placeholder = 'Seleccionar...',
|
|
75
|
+
required = false,
|
|
76
|
+
disabled = false,
|
|
77
|
+
invalid = false,
|
|
78
|
+
variant = 'field',
|
|
79
|
+
searchPlaceholder = 'Buscar…',
|
|
80
|
+
emptyText = 'Sin resultados',
|
|
81
|
+
maxRows = SEARCHABLE_SELECT_MAX_ROWS,
|
|
82
|
+
triggerClassName,
|
|
83
|
+
}: SearchableSelectFieldProps) {
|
|
84
|
+
const [query, setQuery] = useState('')
|
|
85
|
+
const all = useMemo(() => options.map(toResolved), [options])
|
|
86
|
+
const valueKey = value != null && value !== '' ? String(value) : ''
|
|
87
|
+
const selected = valueKey ? (all.find((o) => String(o.id) === valueKey) ?? null) : null
|
|
88
|
+
|
|
89
|
+
const { rows, hidden } = useMemo(() => {
|
|
90
|
+
const matched = filterSearchableOptions(all, query)
|
|
91
|
+
return windowOptions(matched, maxRows, (o) => String(o.id) === valueKey)
|
|
92
|
+
}, [all, query, maxRows, valueKey])
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<RecordPicker<ResolvedOption>
|
|
96
|
+
slot="searchable-select"
|
|
97
|
+
id={id}
|
|
98
|
+
ariaLabel={ariaLabel}
|
|
99
|
+
items={rows}
|
|
100
|
+
getKey={(o) => String(o.id)}
|
|
101
|
+
getLabel={(o) => o.label}
|
|
102
|
+
getDescription={(o) => o.description}
|
|
103
|
+
// Plain text options get no lead (OptionLead would add an initials avatar).
|
|
104
|
+
renderLead={(o, where) => (o.image || o.icon || o.color ? <OptionLead option={o} size={where === 'option' ? 24 : 20} /> : null)}
|
|
105
|
+
value={valueKey || null}
|
|
106
|
+
selected={selected}
|
|
107
|
+
onSelect={(o) => onChange(String(o.id))}
|
|
108
|
+
onClear={!required && valueKey ? () => onChange('') : undefined}
|
|
109
|
+
query={query}
|
|
110
|
+
onQueryChange={setQuery}
|
|
111
|
+
onOpenChange={(o) => {
|
|
112
|
+
if (!o) setQuery('')
|
|
113
|
+
}}
|
|
114
|
+
placeholder={placeholder}
|
|
115
|
+
searchPlaceholder={searchPlaceholder}
|
|
116
|
+
emptyText={emptyText}
|
|
117
|
+
highlightQuery
|
|
118
|
+
disabled={disabled}
|
|
119
|
+
invalid={invalid}
|
|
120
|
+
variant={variant}
|
|
121
|
+
triggerClassName={triggerClassName}
|
|
122
|
+
listLabel={ariaLabel}
|
|
123
|
+
beforeList={
|
|
124
|
+
hidden > 0 ? (
|
|
125
|
+
<p className="px-3 py-1.5 text-xs text-muted-foreground" data-slot="searchable-select-more" role="status">
|
|
126
|
+
{`Mostrando ${rows.length} de ${rows.length + hidden} — escribe para filtrar`}
|
|
127
|
+
</p>
|
|
128
|
+
) : null
|
|
129
|
+
}
|
|
130
|
+
/>
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export default SearchableSelectField
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Pure rules of the searchable select (combobox with a search box) used by the
|
|
2
|
+
// STATIC `select` / `multiselect` widgets. Kept free of React so the threshold
|
|
3
|
+
// and the matching are unit-testable and shared with the table filters.
|
|
4
|
+
import { matchesSearch, normalizeSearchText } from '@asteby/metacore-ui/lib'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Option count above which a static select becomes a searchable combobox when
|
|
8
|
+
* the field does not say otherwise. Mirrors the kernel constant
|
|
9
|
+
* `modelbase.SearchableSelectThreshold`. At or below it the flat select stays.
|
|
10
|
+
*/
|
|
11
|
+
export const SEARCHABLE_SELECT_THRESHOLD = 8
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Rows rendered at once in the list. Static lists are filtered client-side, so
|
|
15
|
+
* a long catalog (hundreds of rows) is windowed instead of painting every row:
|
|
16
|
+
* the match set is cut to this many (the selected option is always kept) and
|
|
17
|
+
* the list says how many more there are. Typing narrows it.
|
|
18
|
+
*/
|
|
19
|
+
export const SEARCHABLE_SELECT_MAX_ROWS = 100
|
|
20
|
+
|
|
21
|
+
export interface SearchableSelectConfig {
|
|
22
|
+
/** `true` forces the combobox, `false` the flat select, unset = by option count. */
|
|
23
|
+
searchable?: boolean | null
|
|
24
|
+
/** Per-field override of the threshold (`searchThreshold`). */
|
|
25
|
+
searchThreshold?: number | null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Should a static select with `count` options render as a searchable combobox? */
|
|
29
|
+
export function isSearchableSelect(field: SearchableSelectConfig | undefined | null, count: number): boolean {
|
|
30
|
+
if (field?.searchable === true) return true
|
|
31
|
+
if (field?.searchable === false) return false
|
|
32
|
+
const t = field?.searchThreshold
|
|
33
|
+
const threshold = typeof t === 'number' && Number.isFinite(t) && t >= 0 ? t : SEARCHABLE_SELECT_THRESHOLD
|
|
34
|
+
return count > threshold
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SearchableOptionLike {
|
|
38
|
+
value: unknown
|
|
39
|
+
label: string
|
|
40
|
+
description?: string | null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Text an option is matched against: label, code (value) and description. */
|
|
44
|
+
export function optionHaystack(o: SearchableOptionLike): string {
|
|
45
|
+
return `${o.label ?? ''} ${o.value ?? ''} ${o.description ?? ''}`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Filters options by `query` (case/accent insensitive, every token must hit the
|
|
50
|
+
* label, value or description) and ranks them: label or code starts with the
|
|
51
|
+
* query, then a word of the label starts with it, then any other hit. The
|
|
52
|
+
* original order is kept inside each rank. An empty query returns a copy of the
|
|
53
|
+
* list untouched.
|
|
54
|
+
*/
|
|
55
|
+
export function filterSearchableOptions<T extends SearchableOptionLike>(options: readonly T[], query: string): T[] {
|
|
56
|
+
const q = normalizeSearchText(query)
|
|
57
|
+
if (!q) return options.slice()
|
|
58
|
+
const first = q.split(' ')[0]!
|
|
59
|
+
const ranked: Array<{ o: T; rank: number; i: number }> = []
|
|
60
|
+
options.forEach((o, i) => {
|
|
61
|
+
if (!matchesSearch(optionHaystack(o), q)) return
|
|
62
|
+
const label = normalizeSearchText(o.label)
|
|
63
|
+
const code = normalizeSearchText(o.value)
|
|
64
|
+
let rank = 3
|
|
65
|
+
if (label.startsWith(q) || code.startsWith(q)) rank = 0
|
|
66
|
+
else if (label.split(/[^a-z0-9]+/).some((w) => w.startsWith(first))) rank = 1
|
|
67
|
+
else if (label.includes(q)) rank = 2
|
|
68
|
+
ranked.push({ o, rank, i })
|
|
69
|
+
})
|
|
70
|
+
ranked.sort((a, b) => a.rank - b.rank || a.i - b.i)
|
|
71
|
+
return ranked.map((r) => r.o)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Cuts a match list to `max` rows, always keeping the selected one(s) so the
|
|
76
|
+
* list never loses them. Returns the window and how many rows were hidden.
|
|
77
|
+
*/
|
|
78
|
+
export function windowOptions<T>(
|
|
79
|
+
rows: readonly T[],
|
|
80
|
+
max: number,
|
|
81
|
+
isSelected: (o: T) => boolean,
|
|
82
|
+
): { rows: T[]; hidden: number } {
|
|
83
|
+
if (rows.length <= max) return { rows: rows.slice(), hidden: 0 }
|
|
84
|
+
const head = rows.slice(0, max)
|
|
85
|
+
const missing = rows.slice(max).filter(isSelected)
|
|
86
|
+
return { rows: [...head, ...missing], hidden: rows.length - max - missing.length }
|
|
87
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -723,6 +723,8 @@ export interface OptionDef {
|
|
|
723
723
|
icon?: string
|
|
724
724
|
color?: string
|
|
725
725
|
image?: string
|
|
726
|
+
/** Secondary text; matched by the searchable select search box and shown under the label. */
|
|
727
|
+
description?: string
|
|
726
728
|
when?: OptionWhen
|
|
727
729
|
}
|
|
728
730
|
|
|
@@ -752,6 +754,16 @@ export interface ActionFieldDef {
|
|
|
752
754
|
*/
|
|
753
755
|
nullable?: boolean
|
|
754
756
|
options?: OptionDef[]
|
|
757
|
+
/**
|
|
758
|
+
* Searchable select: `true` always renders the combobox with a search box
|
|
759
|
+
* (filters while typing, accent/case-insensitive, by label, code and option
|
|
760
|
+
* `description`), `false` always the flat select, unset = combobox when the
|
|
761
|
+
* field has more than 8 options (`SEARCHABLE_SELECT_THRESHOLD`). The kernel
|
|
762
|
+
* serves it as `searchable` (FieldDef.Searchable). Static selects only.
|
|
763
|
+
*/
|
|
764
|
+
searchable?: boolean
|
|
765
|
+
/** Per-field override of the option count above which the select becomes searchable. */
|
|
766
|
+
searchThreshold?: number
|
|
755
767
|
defaultValue?: any
|
|
756
768
|
placeholder?: string
|
|
757
769
|
searchEndpoint?: string
|
|
@@ -9,7 +9,7 @@ import { createHostApiClient, type HostApiClient, type HostApiClientOptions } fr
|
|
|
9
9
|
*/
|
|
10
10
|
export function useHostApiClient(options?: HostApiClientOptions): HostApiClient {
|
|
11
11
|
const api = useHostApi()
|
|
12
|
-
const key =
|
|
12
|
+
const key = JSON.stringify([options?.defaultHeaders ?? null, options?.defaultTimeout ?? null])
|
|
13
13
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
14
14
|
return useMemo(() => createHostApiClient(api, options), [api, key])
|
|
15
15
|
}
|