@asteby/metacore-runtime-react 18.17.3 → 18.18.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 +27 -0
- package/dist/action-modal-dispatcher.js +16 -5
- package/dist/dynamic-form-schema.d.ts +41 -1
- package/dist/dynamic-form-schema.d.ts.map +1 -1
- package/dist/dynamic-form-schema.js +61 -0
- package/dist/dynamic-line-items.d.ts +7 -1
- package/dist/dynamic-line-items.d.ts.map +1 -1
- package/dist/dynamic-line-items.js +46 -12
- package/dist/dynamic-select-field.d.ts +17 -1
- package/dist/dynamic-select-field.d.ts.map +1 -1
- package/dist/dynamic-select-field.js +48 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/types.d.ts +48 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/use-options-resolver.d.ts +9 -0
- package/dist/use-options-resolver.d.ts.map +1 -1
- package/dist/use-options-resolver.js +7 -2
- package/package.json +1 -1
- package/src/__tests__/dependent-options.test.tsx +337 -0
- package/src/action-modal-dispatcher.tsx +15 -4
- package/src/dynamic-form-schema.ts +72 -1
- package/src/dynamic-line-items.tsx +86 -13
- package/src/dynamic-select-field.tsx +69 -12
- package/src/index.ts +7 -1
- package/src/types.ts +49 -0
- package/src/use-options-resolver.ts +15 -1
|
@@ -41,6 +41,15 @@ export interface UseOptionsResolverArgs {
|
|
|
41
41
|
* server returns the first page unfiltered.
|
|
42
42
|
*/
|
|
43
43
|
query?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Cascade scope forwarded as `?filter_value=`. Set by a dependent picker
|
|
46
|
+
* from the current value of the field it `dependsOn` (e.g. a product
|
|
47
|
+
* picker scoped to the header's `source_warehouse_id`). When empty/undefined
|
|
48
|
+
* the param is omitted (no scope — the picker lists everything). Changing it
|
|
49
|
+
* re-fetches; an empty string is treated as "not set" so a cleared parent
|
|
50
|
+
* does not query for the empty-string scope.
|
|
51
|
+
*/
|
|
52
|
+
filterValue?: string;
|
|
44
53
|
/**
|
|
45
54
|
* Server-side pagination cap. Defaults to 50 (kernel
|
|
46
55
|
* DefaultOptionsLimit) if omitted.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-options-resolver.d.ts","sourceRoot":"","sources":["../src/use-options-resolver.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,cAAc;IAC3B,8CAA8C;IAC9C,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,2DAA2D;IAC3D,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;IACtB,sBAAsB;IACtB,KAAK,EAAE,MAAM,CAAA;IACb,8DAA8D;IAC9D,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CACvB;AAED,MAAM,WAAW,WAAW;IACxB,oEAAoE;IACpE,IAAI,EAAE,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAA;IACnC,2DAA2D;IAC3D,KAAK,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,sBAAsB;IACnC;;;;OAIG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,wBAAwB;IACrC,OAAO,EAAE,cAAc,EAAE,CAAA;IACzB,IAAI,EAAE,WAAW,GAAG,IAAI,CAAA;IACxB,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;IACnB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,IAAI,CAAA;CACtB;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,sBAAsB,GAAG,wBAAwB,
|
|
1
|
+
{"version":3,"file":"use-options-resolver.d.ts","sourceRoot":"","sources":["../src/use-options-resolver.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,cAAc;IAC3B,8CAA8C;IAC9C,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,2DAA2D;IAC3D,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;IACtB,sBAAsB;IACtB,KAAK,EAAE,MAAM,CAAA;IACb,8DAA8D;IAC9D,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CACvB;AAED,MAAM,WAAW,WAAW;IACxB,oEAAoE;IACpE,IAAI,EAAE,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAA;IACnC,2DAA2D;IAC3D,KAAK,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,sBAAsB;IACnC;;;;OAIG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,wBAAwB;IACrC,OAAO,EAAE,cAAc,EAAE,CAAA;IACzB,IAAI,EAAE,WAAW,GAAG,IAAI,CAAA;IACxB,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAA;IACnB,8DAA8D;IAC9D,OAAO,EAAE,MAAM,IAAI,CAAA;CACtB;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,sBAAsB,GAAG,wBAAwB,CAsHzF;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,GAAG,GAAG,cAAc,CAatD"}
|
|
@@ -23,7 +23,7 @@ import { useApi } from './api-context';
|
|
|
23
23
|
* compose this with TanStack Query in their own layer).
|
|
24
24
|
*/
|
|
25
25
|
export function useOptionsResolver(args) {
|
|
26
|
-
const { modelKey, fieldKey, ref, query, limit, enabled = true, endpoint, } = args;
|
|
26
|
+
const { modelKey, fieldKey, ref, query, limit, enabled = true, endpoint, filterValue, } = args;
|
|
27
27
|
const api = useApi();
|
|
28
28
|
const [options, setOptions] = useState([]);
|
|
29
29
|
const [meta, setMeta] = useState(null);
|
|
@@ -77,6 +77,11 @@ export function useOptionsResolver(args) {
|
|
|
77
77
|
params.q = query;
|
|
78
78
|
if (typeof limit === 'number' && limit > 0)
|
|
79
79
|
params.limit = limit;
|
|
80
|
+
// Cascade scope: a dependent picker passes the value of the field it
|
|
81
|
+
// `dependsOn`. Skip empty strings so a cleared parent omits the param
|
|
82
|
+
// (no scope) rather than querying for the empty-string filter_value.
|
|
83
|
+
if (filterValue)
|
|
84
|
+
params.filter_value = filterValue;
|
|
80
85
|
api.get(url, { params, signal: controller.signal })
|
|
81
86
|
.then((res) => {
|
|
82
87
|
if (controller.signal.aborted)
|
|
@@ -117,7 +122,7 @@ export function useOptionsResolver(args) {
|
|
|
117
122
|
return () => {
|
|
118
123
|
controller.abort();
|
|
119
124
|
};
|
|
120
|
-
}, [api, url, effectiveField, query, limit, enabled, refreshKey]);
|
|
125
|
+
}, [api, url, effectiveField, query, limit, enabled, filterValue, refreshKey]);
|
|
121
126
|
return {
|
|
122
127
|
options,
|
|
123
128
|
meta,
|
package/package.json
CHANGED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
//
|
|
3
|
+
// Dependent (cascading) options contract:
|
|
4
|
+
// - resolveDependsValue (pure): a cell `dependsOn` resolves from the row
|
|
5
|
+
// (sibling) first, then the header form values; empty/unset → ''.
|
|
6
|
+
// - useOptionsResolver: a `filterValue` is forwarded as `&filter_value=` and
|
|
7
|
+
// re-fetches when it changes; an empty value omits the param.
|
|
8
|
+
// - DynamicLineItems → cell with `dependsOn`: the picker is disabled while the
|
|
9
|
+
// header field is empty, and once the header field has a value the options
|
|
10
|
+
// request carries that value as `filter_value` and re-fetches on change.
|
|
11
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
12
|
+
import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
|
|
13
|
+
|
|
14
|
+
// Identity translator so any raw i18n keys surface verbatim.
|
|
15
|
+
vi.mock('react-i18next', () => ({
|
|
16
|
+
useTranslation: () => ({ t: (k: string) => k }),
|
|
17
|
+
}))
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
resolveDependsValue,
|
|
21
|
+
getDependsOn,
|
|
22
|
+
getOptionsConfig,
|
|
23
|
+
resolveOptionsSource,
|
|
24
|
+
} from '../dynamic-form-schema'
|
|
25
|
+
import { useOptionsResolver } from '../use-options-resolver'
|
|
26
|
+
import { DynamicLineItems } from '../dynamic-line-items'
|
|
27
|
+
import { ApiProvider, type ApiClient } from '../api-context'
|
|
28
|
+
import type { ActionFieldDef } from '../types'
|
|
29
|
+
import { useState } from 'react'
|
|
30
|
+
|
|
31
|
+
afterEach(cleanup)
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Pure resolution
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
describe('getDependsOn / resolveDependsValue', () => {
|
|
37
|
+
const field = (over: Partial<ActionFieldDef>): ActionFieldDef =>
|
|
38
|
+
({ key: 'product_id', label: 'Producto', type: 'dynamic_select', ...over })
|
|
39
|
+
|
|
40
|
+
it('reads camelCase dependsOn and snake_case depends_on', () => {
|
|
41
|
+
expect(getDependsOn(field({ dependsOn: 'wh' }))).toBe('wh')
|
|
42
|
+
expect(getDependsOn(field({ depends_on: 'wh' }))).toBe('wh')
|
|
43
|
+
expect(getDependsOn(field({}))).toBeUndefined()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('resolves from header form values', () => {
|
|
47
|
+
const f = field({ depends_on: 'source_warehouse_id' })
|
|
48
|
+
expect(resolveDependsValue(f, { source_warehouse_id: 'W1' })).toBe('W1')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('prefers a sibling row value over the header', () => {
|
|
52
|
+
const f = field({ dependsOn: 'warehouse_id' })
|
|
53
|
+
const out = resolveDependsValue(f, { warehouse_id: 'HEADER' }, { warehouse_id: 'ROW' })
|
|
54
|
+
expect(out).toBe('ROW')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('falls back to header when the row value is blank', () => {
|
|
58
|
+
const f = field({ dependsOn: 'warehouse_id' })
|
|
59
|
+
const out = resolveDependsValue(f, { warehouse_id: 'HEADER' }, { warehouse_id: '' })
|
|
60
|
+
expect(out).toBe('HEADER')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('returns empty string when the dependency is unset', () => {
|
|
64
|
+
const f = field({ dependsOn: 'warehouse_id' })
|
|
65
|
+
expect(resolveDependsValue(f, {})).toBe('')
|
|
66
|
+
expect(resolveDependsValue(field({}), { a: 1 })).toBe('')
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe('getOptionsConfig / resolveOptionsSource', () => {
|
|
71
|
+
const field = (over: Partial<ActionFieldDef>): ActionFieldDef =>
|
|
72
|
+
({ key: 'product_id', label: 'Producto', type: 'dynamic_select', ...over })
|
|
73
|
+
|
|
74
|
+
it('reads camelCase optionsConfig and snake_case options_config', () => {
|
|
75
|
+
expect(getOptionsConfig(field({ optionsConfig: { source: 'stock' } }))?.source).toBe('stock')
|
|
76
|
+
expect(getOptionsConfig(field({ options_config: { source: 'stock' } }))?.source).toBe('stock')
|
|
77
|
+
expect(getOptionsConfig(field({}))).toBeUndefined()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('routes to the source model with field=value when optionsConfig.source is present', () => {
|
|
81
|
+
const f = field({
|
|
82
|
+
ref: 'products.Product',
|
|
83
|
+
options_config: { source: 'stock', filter_by: 'warehouse_id', value: 'product_id', description: 'quantity' },
|
|
84
|
+
})
|
|
85
|
+
const out = resolveOptionsSource(f)
|
|
86
|
+
expect(out.endpoint).toBe('/options/stock')
|
|
87
|
+
expect(out.fieldKey).toBe('product_id')
|
|
88
|
+
// source wins over ref — the ref pointer is not used for routing.
|
|
89
|
+
expect(out.ref).toBeUndefined()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('falls back to the field key when optionsConfig.value is absent', () => {
|
|
93
|
+
const f = field({ key: 'item_id', options_config: { source: 'stock' } })
|
|
94
|
+
const out = resolveOptionsSource(f)
|
|
95
|
+
expect(out.endpoint).toBe('/options/stock')
|
|
96
|
+
expect(out.fieldKey).toBe('item_id')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('keeps ref-based resolution when there is no optionsConfig.source', () => {
|
|
100
|
+
const f = field({ ref: 'products.Product' })
|
|
101
|
+
const out = resolveOptionsSource(f)
|
|
102
|
+
expect(out.endpoint).toBeUndefined()
|
|
103
|
+
expect(out.ref).toBe('products.Product')
|
|
104
|
+
expect(out.fieldKey).toBe('id')
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// useOptionsResolver — filter_value forwarding + re-fetch
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
function Harness({ filterValue }: { filterValue?: string }) {
|
|
112
|
+
const { options } = useOptionsResolver({
|
|
113
|
+
modelKey: '',
|
|
114
|
+
fieldKey: 'id',
|
|
115
|
+
ref: 'products.Product',
|
|
116
|
+
filterValue,
|
|
117
|
+
})
|
|
118
|
+
return <div data-testid="count">{options.length}</div>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
describe('useOptionsResolver filter_value', () => {
|
|
122
|
+
it('forwards a non-empty filter_value and re-fetches on change', async () => {
|
|
123
|
+
const get = vi.fn(async () => ({
|
|
124
|
+
data: { success: true, data: [{ id: '1', label: 'One' }], meta: { type: 'dynamic', count: 1 } },
|
|
125
|
+
}))
|
|
126
|
+
const client = { get } as unknown as ApiClient
|
|
127
|
+
|
|
128
|
+
const { rerender } = render(
|
|
129
|
+
<ApiProvider client={client}>
|
|
130
|
+
<Harness filterValue="W1" />
|
|
131
|
+
</ApiProvider>,
|
|
132
|
+
)
|
|
133
|
+
await waitFor(() => expect(screen.getByTestId('count').textContent).toBe('1'))
|
|
134
|
+
|
|
135
|
+
// First call carried filter_value=W1.
|
|
136
|
+
const firstParams = get.mock.calls[0][1]?.params
|
|
137
|
+
expect(firstParams.filter_value).toBe('W1')
|
|
138
|
+
|
|
139
|
+
// Change the parent value → a fresh request with the new filter_value.
|
|
140
|
+
rerender(
|
|
141
|
+
<ApiProvider client={client}>
|
|
142
|
+
<Harness filterValue="W2" />
|
|
143
|
+
</ApiProvider>,
|
|
144
|
+
)
|
|
145
|
+
await waitFor(() => expect(get.mock.calls.length).toBeGreaterThan(1))
|
|
146
|
+
const lastParams = get.mock.calls[get.mock.calls.length - 1][1]?.params
|
|
147
|
+
expect(lastParams.filter_value).toBe('W2')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('omits filter_value when empty', async () => {
|
|
151
|
+
const get = vi.fn(async () => ({
|
|
152
|
+
data: { success: true, data: [], meta: { type: 'dynamic', count: 0 } },
|
|
153
|
+
}))
|
|
154
|
+
const client = { get } as unknown as ApiClient
|
|
155
|
+
render(
|
|
156
|
+
<ApiProvider client={client}>
|
|
157
|
+
<Harness filterValue="" />
|
|
158
|
+
</ApiProvider>,
|
|
159
|
+
)
|
|
160
|
+
await waitFor(() => expect(get).toHaveBeenCalled())
|
|
161
|
+
expect(get.mock.calls[0][1]?.params.filter_value).toBeUndefined()
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
// DynamicLineItems — a cell that dependsOn a HEADER field
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
const lineItemsField: ActionFieldDef = {
|
|
169
|
+
key: 'items',
|
|
170
|
+
label: 'Renglones',
|
|
171
|
+
type: 'array',
|
|
172
|
+
itemFields: [
|
|
173
|
+
{
|
|
174
|
+
key: 'product_id',
|
|
175
|
+
label: 'Producto',
|
|
176
|
+
type: 'dynamic_select',
|
|
177
|
+
ref: 'products.Product',
|
|
178
|
+
// depends on the HEADER field, not a sibling row cell.
|
|
179
|
+
depends_on: 'source_warehouse_id',
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Drives DynamicLineItems with one pre-existing row + a switchable header value.
|
|
185
|
+
function LineItemsHost({ headerValue }: { headerValue: string }) {
|
|
186
|
+
const [rows, setRows] = useState<any[]>([{ product_id: '' }])
|
|
187
|
+
return (
|
|
188
|
+
<DynamicLineItems
|
|
189
|
+
field={lineItemsField}
|
|
190
|
+
value={rows}
|
|
191
|
+
onChange={setRows}
|
|
192
|
+
formValues={{ source_warehouse_id: headerValue }}
|
|
193
|
+
/>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
describe('DynamicLineItems cascading cell', () => {
|
|
198
|
+
it('disables the picker while the header field is empty', () => {
|
|
199
|
+
const get = vi.fn(async () => ({
|
|
200
|
+
data: { success: true, data: [], meta: { type: 'dynamic', count: 0 } },
|
|
201
|
+
}))
|
|
202
|
+
const client = { get } as unknown as ApiClient
|
|
203
|
+
render(
|
|
204
|
+
<ApiProvider client={client}>
|
|
205
|
+
<LineItemsHost headerValue="" />
|
|
206
|
+
</ApiProvider>,
|
|
207
|
+
)
|
|
208
|
+
// The combobox trigger is disabled and shows the dependency hint.
|
|
209
|
+
const trigger = screen.getByRole('combobox')
|
|
210
|
+
expect((trigger as HTMLButtonElement).disabled).toBe(true)
|
|
211
|
+
expect(trigger.getAttribute('data-depends-blocked')).toBe('')
|
|
212
|
+
// No options request fires while blocked.
|
|
213
|
+
expect(get).not.toHaveBeenCalled()
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('sends filter_value from the header field and re-fetches when it changes', async () => {
|
|
217
|
+
const get = vi.fn(async () => ({
|
|
218
|
+
data: {
|
|
219
|
+
success: true,
|
|
220
|
+
data: [{ id: 'p1', label: 'Tornillo', description: 'disp. 12' }],
|
|
221
|
+
meta: { type: 'dynamic', count: 1 },
|
|
222
|
+
},
|
|
223
|
+
}))
|
|
224
|
+
const client = { get } as unknown as ApiClient
|
|
225
|
+
|
|
226
|
+
const { rerender } = render(
|
|
227
|
+
<ApiProvider client={client}>
|
|
228
|
+
<LineItemsHost headerValue="W1" />
|
|
229
|
+
</ApiProvider>,
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
// Open the popover so the typeahead fetches.
|
|
233
|
+
const trigger = screen.getByRole('combobox')
|
|
234
|
+
expect((trigger as HTMLButtonElement).disabled).toBe(false)
|
|
235
|
+
await act(async () => {
|
|
236
|
+
trigger.click()
|
|
237
|
+
})
|
|
238
|
+
await waitFor(() => expect(get).toHaveBeenCalled())
|
|
239
|
+
const firstParams = get.mock.calls[0][1]?.params
|
|
240
|
+
expect(firstParams.filter_value).toBe('W1')
|
|
241
|
+
|
|
242
|
+
// Switch the header warehouse → the cell picker re-fetches scoped to W2.
|
|
243
|
+
rerender(
|
|
244
|
+
<ApiProvider client={client}>
|
|
245
|
+
<LineItemsHost headerValue="W2" />
|
|
246
|
+
</ApiProvider>,
|
|
247
|
+
)
|
|
248
|
+
await waitFor(() => {
|
|
249
|
+
const last = get.mock.calls[get.mock.calls.length - 1][1]?.params
|
|
250
|
+
expect(last.filter_value).toBe('W2')
|
|
251
|
+
})
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
// optionsConfig.source routing — the picker queries the SOURCE model, not `ref`
|
|
257
|
+
// ---------------------------------------------------------------------------
|
|
258
|
+
const sourceLineItemsField: ActionFieldDef = {
|
|
259
|
+
key: 'items',
|
|
260
|
+
label: 'Renglones',
|
|
261
|
+
type: 'array',
|
|
262
|
+
itemFields: [
|
|
263
|
+
{
|
|
264
|
+
key: 'product_id',
|
|
265
|
+
label: 'Producto',
|
|
266
|
+
type: 'dynamic_select',
|
|
267
|
+
// A `ref` is present but optionsConfig.source MUST win for routing.
|
|
268
|
+
ref: 'products.Product',
|
|
269
|
+
depends_on: 'source_warehouse_id',
|
|
270
|
+
options_config: {
|
|
271
|
+
type: 'dynamic',
|
|
272
|
+
source: 'stock',
|
|
273
|
+
filter_by: 'warehouse_id',
|
|
274
|
+
value: 'product_id',
|
|
275
|
+
label_ref: 'products.Product',
|
|
276
|
+
description: 'quantity',
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
],
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function SourceLineItemsHost({ headerValue }: { headerValue: string }) {
|
|
283
|
+
const [rows, setRows] = useState<any[]>([{ product_id: '' }])
|
|
284
|
+
return (
|
|
285
|
+
<DynamicLineItems
|
|
286
|
+
field={sourceLineItemsField}
|
|
287
|
+
value={rows}
|
|
288
|
+
onChange={setRows}
|
|
289
|
+
formValues={{ source_warehouse_id: headerValue }}
|
|
290
|
+
/>
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
describe('DynamicLineItems cell with optionsConfig.source', () => {
|
|
295
|
+
it('queries /options/<source>?field=<value>&filter_value=<dependsOn> and re-fetches on parent change', async () => {
|
|
296
|
+
const get = vi.fn(async () => ({
|
|
297
|
+
data: {
|
|
298
|
+
success: true,
|
|
299
|
+
data: [{ id: 'p1', label: 'Tornillo', description: 'disp. 12' }],
|
|
300
|
+
meta: { type: 'dynamic', count: 1 },
|
|
301
|
+
},
|
|
302
|
+
}))
|
|
303
|
+
const client = { get } as unknown as ApiClient
|
|
304
|
+
|
|
305
|
+
const { rerender } = render(
|
|
306
|
+
<ApiProvider client={client}>
|
|
307
|
+
<SourceLineItemsHost headerValue="W1" />
|
|
308
|
+
</ApiProvider>,
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
const trigger = screen.getByRole('combobox')
|
|
312
|
+
expect((trigger as HTMLButtonElement).disabled).toBe(false)
|
|
313
|
+
await act(async () => {
|
|
314
|
+
trigger.click()
|
|
315
|
+
})
|
|
316
|
+
await waitFor(() => expect(get).toHaveBeenCalled())
|
|
317
|
+
|
|
318
|
+
// URL hits the SOURCE model, not the `ref`.
|
|
319
|
+
const [firstUrl, firstCfg] = get.mock.calls[0]
|
|
320
|
+
expect(firstUrl).toBe('/options/stock')
|
|
321
|
+
expect(firstCfg?.params.field).toBe('product_id')
|
|
322
|
+
expect(firstCfg?.params.filter_value).toBe('W1')
|
|
323
|
+
|
|
324
|
+
// Parent change → re-fetch, still routed to the source, new filter_value.
|
|
325
|
+
rerender(
|
|
326
|
+
<ApiProvider client={client}>
|
|
327
|
+
<SourceLineItemsHost headerValue="W2" />
|
|
328
|
+
</ApiProvider>,
|
|
329
|
+
)
|
|
330
|
+
await waitFor(() => {
|
|
331
|
+
const [url, cfg] = get.mock.calls[get.mock.calls.length - 1]
|
|
332
|
+
expect(url).toBe('/options/stock')
|
|
333
|
+
expect(cfg?.params.field).toBe('product_id')
|
|
334
|
+
expect(cfg?.params.filter_value).toBe('W2')
|
|
335
|
+
})
|
|
336
|
+
})
|
|
337
|
+
})
|
|
@@ -42,7 +42,7 @@ import { DynamicLineItems } from './dynamic-line-items'
|
|
|
42
42
|
import { DynamicSelectField } from './dynamic-select-field'
|
|
43
43
|
import { DynamicDateField } from './dynamic-date-field'
|
|
44
44
|
import { UploadField } from './upload-field'
|
|
45
|
-
import { isLineItemsField, resolveWidget } from './dynamic-form-schema'
|
|
45
|
+
import { isLineItemsField, resolveWidget, resolveDependsValue, getDependsOn } from './dynamic-form-schema'
|
|
46
46
|
import type { ActionFieldDef } from './types'
|
|
47
47
|
// Canonical registry lives in @asteby/metacore-sdk
|
|
48
48
|
import {
|
|
@@ -292,7 +292,7 @@ function GenericActionModal({ open, onOpenChange, action, model, record, endpoin
|
|
|
292
292
|
{field.label}
|
|
293
293
|
{field.required && <span className="text-red-500 ml-1">*</span>}
|
|
294
294
|
</Label>
|
|
295
|
-
{renderField(field, formData[field.key], (v: any) => updateField(field.key, v))}
|
|
295
|
+
{renderField(field, formData[field.key], (v: any) => updateField(field.key, v), formData)}
|
|
296
296
|
</div>
|
|
297
297
|
)
|
|
298
298
|
})}
|
|
@@ -319,10 +319,16 @@ function renderField(
|
|
|
319
319
|
field: ActionFieldDef,
|
|
320
320
|
value: any,
|
|
321
321
|
onChange: (value: any) => void,
|
|
322
|
+
// Full current form values — lets a line-items grid (and any cascading
|
|
323
|
+
// header picker) resolve a `dependsOn` reference against sibling header
|
|
324
|
+
// fields. Omitted by callers that have no surrounding form (the field is
|
|
325
|
+
// then treated as having no resolvable dependency).
|
|
326
|
+
formValues?: Record<string, any>,
|
|
322
327
|
) {
|
|
323
328
|
// Repeatable line-items group → row grid (value is an array of row objects).
|
|
329
|
+
// The header form values flow in so a cell can depend on a header field.
|
|
324
330
|
if (isLineItemsField(field)) {
|
|
325
|
-
return <DynamicLineItems field={field} value={value} onChange={onChange} />
|
|
331
|
+
return <DynamicLineItems field={field} value={value} onChange={onChange} formValues={formValues} />
|
|
326
332
|
}
|
|
327
333
|
// Resolve the widget the same way DynamicForm does (explicit widget wins,
|
|
328
334
|
// else inferred from type) so action modals and the standalone form stay in
|
|
@@ -330,7 +336,12 @@ function renderField(
|
|
|
330
336
|
// dropped `dynamic_select` to a plain text input.
|
|
331
337
|
const widget = resolveWidget(field)
|
|
332
338
|
if (widget === 'dynamic_select') {
|
|
333
|
-
|
|
339
|
+
// A header-level dynamic_select may itself depend on another header
|
|
340
|
+
// field; resolve its filter_value from the form context.
|
|
341
|
+
const dependsValue = getDependsOn(field)
|
|
342
|
+
? resolveDependsValue(field, formValues)
|
|
343
|
+
: undefined
|
|
344
|
+
return <DynamicSelectField field={field} value={value} onChange={onChange} dependsValue={dependsValue} />
|
|
334
345
|
}
|
|
335
346
|
// File upload → themed picker that POSTs the file to the host upload
|
|
336
347
|
// endpoint and stores the returned url/path. Kept in sync with DynamicForm.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// callers (and unit tests) can use the zod schema without pulling in React or
|
|
3
3
|
// metacore-ui primitives.
|
|
4
4
|
import { z, type ZodTypeAny } from 'zod'
|
|
5
|
-
import type { ActionFieldDef, FieldValidation } from './types'
|
|
5
|
+
import type { ActionFieldDef, FieldValidation, FieldOptionsConfig } from './types'
|
|
6
6
|
import { resolveValidatorToken } from './use-org-config-bridge'
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -252,6 +252,77 @@ export function fieldHasRef(field: ActionFieldDef): boolean {
|
|
|
252
252
|
return getFieldRef(field) !== undefined
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
+
/**
|
|
256
|
+
* Resolves a field's cascade dependency — the key of another form field whose
|
|
257
|
+
* current value scopes this picker's options (`filter_value`). Tolerates the
|
|
258
|
+
* camelCase `dependsOn` (authored SDK shape) and the snake_case `depends_on`
|
|
259
|
+
* the kernel manifest serves. Returns the trimmed field key, or `undefined`
|
|
260
|
+
* when the field declares no dependency.
|
|
261
|
+
*/
|
|
262
|
+
export function getDependsOn(field: ActionFieldDef): string | undefined {
|
|
263
|
+
const dep = field.dependsOn ?? field.depends_on
|
|
264
|
+
if (typeof dep === 'string' && dep.trim() !== '') return dep.trim()
|
|
265
|
+
return undefined
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Resolves the cascade `filter_value` for a field from the surrounding form
|
|
270
|
+
* context. The depended-on key is matched against the current row first (a
|
|
271
|
+
* sibling item-field on the same line) and then the header form values, so a
|
|
272
|
+
* line-items cell can depend on either a sibling cell OR a header field (e.g.
|
|
273
|
+
* `source_warehouse_id`). Returns the stringified value, or `''` when the
|
|
274
|
+
* field has no dependency or the depended-on value is empty/unset.
|
|
275
|
+
*/
|
|
276
|
+
export function resolveDependsValue(
|
|
277
|
+
field: ActionFieldDef,
|
|
278
|
+
formValues?: Record<string, any> | null,
|
|
279
|
+
rowValues?: Record<string, any> | null,
|
|
280
|
+
): string {
|
|
281
|
+
const dep = getDependsOn(field)
|
|
282
|
+
if (!dep) return ''
|
|
283
|
+
const raw =
|
|
284
|
+
(rowValues && rowValues[dep] != null && rowValues[dep] !== '' ? rowValues[dep] : undefined) ??
|
|
285
|
+
(formValues ? formValues[dep] : undefined)
|
|
286
|
+
if (raw == null || raw === '') return ''
|
|
287
|
+
return String(raw)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Reads a field's enriched options-resolution config, tolerating the camelCase
|
|
292
|
+
* `optionsConfig` (authored SDK shape) and the snake_case `options_config` the
|
|
293
|
+
* kernel manifest serves. Returns `undefined` when the field declares none.
|
|
294
|
+
*/
|
|
295
|
+
export function getOptionsConfig(field: ActionFieldDef): FieldOptionsConfig | undefined {
|
|
296
|
+
const cfg = field.optionsConfig ?? field.options_config
|
|
297
|
+
return cfg && typeof cfg === 'object' ? cfg : undefined
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Resolves where a picker should fetch its options from, honouring an
|
|
302
|
+
* `optionsConfig.source` (the dependent/scoped routing the kernel serves) and
|
|
303
|
+
* falling back to the field's `ref` for retrocompat.
|
|
304
|
+
*
|
|
305
|
+
* - With `optionsConfig.source`: query the SOURCE model →
|
|
306
|
+
* `{ endpoint: '/options/<source>', fieldKey: <value ?? field.key> }`.
|
|
307
|
+
* - Without it: keep `ref`-based resolution → `{ ref }` (the hook's canonical
|
|
308
|
+
* path), `fieldKey` defaulting to `'id'`.
|
|
309
|
+
*
|
|
310
|
+
* The returned shape feeds straight into `useOptionsResolver` args.
|
|
311
|
+
*/
|
|
312
|
+
export function resolveOptionsSource(field: ActionFieldDef): {
|
|
313
|
+
endpoint?: string
|
|
314
|
+
ref?: string
|
|
315
|
+
fieldKey: string
|
|
316
|
+
} {
|
|
317
|
+
const cfg = getOptionsConfig(field)
|
|
318
|
+
const source = typeof cfg?.source === 'string' ? cfg.source.trim() : ''
|
|
319
|
+
if (source) {
|
|
320
|
+
const value = typeof cfg?.value === 'string' && cfg.value.trim() !== '' ? cfg.value.trim() : field.key
|
|
321
|
+
return { endpoint: `/options/${source}`, fieldKey: value }
|
|
322
|
+
}
|
|
323
|
+
return { ref: getFieldRef(field), fieldKey: 'id' }
|
|
324
|
+
}
|
|
325
|
+
|
|
255
326
|
/**
|
|
256
327
|
* Normalizes an upload field's config, tolerating both the camelCase authored
|
|
257
328
|
* SDK shape and the snake_case the kernel serves (`max_size`, `storage_path`).
|