@jskit-ai/http-web 0.2.0 → 0.2.1
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/fixtures/select-field/App.vue +56 -0
- package/fixtures/select-field/index.html +12 -0
- package/fixtures/select-field/main.js +24 -0
- package/fixtures/select-field/vite.config.mjs +15 -0
- package/locales/en.json +3 -0
- package/package.json +6 -1
- package/patterns/crud-screen-set/PATTERN.md +46 -0
- package/src/client/components/SelectField.vue +55 -0
- package/src/client/composables/crud/crudLookupFieldRuntime.js +10 -28
- package/src/client/support/selectFieldSupport.js +71 -0
- package/test/crudLookupFieldRuntime.test.js +42 -0
- package/test/exportsContract.test.js +1 -0
- package/test/selectField.browser.test.js +63 -0
- package/test/selectFieldSupport.test.js +68 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { ref } from "vue";
|
|
3
|
+
import SelectField from "../../src/client/components/SelectField.vue";
|
|
4
|
+
|
|
5
|
+
const bath = { value: 7, label: "Bath" };
|
|
6
|
+
const daycare = { value: 53, label: "Daycare - One Day" };
|
|
7
|
+
const loading = ref(true);
|
|
8
|
+
const services = ref([]);
|
|
9
|
+
const extras = ref([bath]);
|
|
10
|
+
const service = ref(53);
|
|
11
|
+
const retired = ref(99);
|
|
12
|
+
const serviceIds = ref([7, 53]);
|
|
13
|
+
|
|
14
|
+
// The test releases the delayed options request deterministically.
|
|
15
|
+
window.releaseOptions = () => {
|
|
16
|
+
services.value = [bath, daycare];
|
|
17
|
+
extras.value = [bath, daycare];
|
|
18
|
+
loading.value = false;
|
|
19
|
+
};
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<template>
|
|
23
|
+
<v-app>
|
|
24
|
+
<v-main class="pa-4">
|
|
25
|
+
<SelectField
|
|
26
|
+
v-model="service"
|
|
27
|
+
label="Service"
|
|
28
|
+
:items="services"
|
|
29
|
+
item-title="label"
|
|
30
|
+
item-value="value"
|
|
31
|
+
:loading="loading"
|
|
32
|
+
/>
|
|
33
|
+
<SelectField
|
|
34
|
+
v-model="retired"
|
|
35
|
+
searchable
|
|
36
|
+
label="Retired service"
|
|
37
|
+
:items="services"
|
|
38
|
+
item-title="label"
|
|
39
|
+
item-value="value"
|
|
40
|
+
:loading="loading"
|
|
41
|
+
/>
|
|
42
|
+
<SelectField
|
|
43
|
+
v-model="serviceIds"
|
|
44
|
+
label="Extras"
|
|
45
|
+
multiple
|
|
46
|
+
chips
|
|
47
|
+
closable-chips
|
|
48
|
+
:items="extras"
|
|
49
|
+
item-title="label"
|
|
50
|
+
item-value="value"
|
|
51
|
+
:loading="loading"
|
|
52
|
+
/>
|
|
53
|
+
<pre data-testid="select-field-models">{{ JSON.stringify({ service, retired, serviceIds }) }}</pre>
|
|
54
|
+
</v-main>
|
|
55
|
+
</v-app>
|
|
56
|
+
</template>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Select field display regression</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="app"></div>
|
|
10
|
+
<script type="module" src="/main.js"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createApp } from "vue";
|
|
2
|
+
import "vuetify/styles";
|
|
3
|
+
import { createVuetify } from "vuetify";
|
|
4
|
+
import * as components from "vuetify/components";
|
|
5
|
+
import * as directives from "vuetify/directives";
|
|
6
|
+
import { aliases as mdiAliases, mdi } from "vuetify/iconsets/mdi-svg";
|
|
7
|
+
import App from "./App.vue";
|
|
8
|
+
|
|
9
|
+
const vuetify = createVuetify({
|
|
10
|
+
components,
|
|
11
|
+
directives,
|
|
12
|
+
theme: {
|
|
13
|
+
defaultTheme: "light"
|
|
14
|
+
},
|
|
15
|
+
icons: {
|
|
16
|
+
defaultSet: "mdi",
|
|
17
|
+
aliases: mdiAliases,
|
|
18
|
+
sets: { mdi }
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
createApp(App)
|
|
23
|
+
.use(vuetify)
|
|
24
|
+
.mount("#app");
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { defineConfig } from "vite";
|
|
4
|
+
import vue from "@vitejs/plugin-vue";
|
|
5
|
+
|
|
6
|
+
const fixtureRoot = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
root: fixtureRoot,
|
|
10
|
+
plugins: [vue()],
|
|
11
|
+
server: {
|
|
12
|
+
host: "127.0.0.1",
|
|
13
|
+
strictPort: true
|
|
14
|
+
}
|
|
15
|
+
});
|
package/locales/en.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/http-web",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"./client/components/CrudListFilterSurface": "./src/client/components/CrudListFilterSurface.vue",
|
|
14
14
|
"./client/components/CrudListScreen": "./src/client/components/CrudListScreen.vue",
|
|
15
15
|
"./client/components/CrudViewScreen": "./src/client/components/CrudViewScreen.vue",
|
|
16
|
+
"./client/components/SelectField": "./src/client/components/SelectField.vue",
|
|
16
17
|
"./client/bulkActions": "./src/client/bulkActions.js",
|
|
17
18
|
"./client/filters": "./src/client/filters.js",
|
|
18
19
|
"./client/rowActions": "./src/client/rowActions.js",
|
|
@@ -117,6 +118,10 @@
|
|
|
117
118
|
{
|
|
118
119
|
"subpath": "./client/components/CrudAddEditScreen",
|
|
119
120
|
"summary": "Exports the shared CRUD add/edit screen."
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"subpath": "./client/components/SelectField",
|
|
124
|
+
"summary": "Exports the select/autocomplete field that never shows a stored id as its title."
|
|
120
125
|
}
|
|
121
126
|
]
|
|
122
127
|
},
|
|
@@ -108,12 +108,58 @@ realtime events, delete availability, and action definitions. Use the screen
|
|
|
108
108
|
slots for domain-specific content. Use a lower-level request composable only
|
|
109
109
|
when the wire contract genuinely is not CRUD.
|
|
110
110
|
|
|
111
|
+
### Lookup and selection fields
|
|
112
|
+
|
|
113
|
+
A form field whose `relation` is a `lookup` takes its options from
|
|
114
|
+
`createCrudLookupFieldRuntime({ formFields })` in
|
|
115
|
+
`@jskit-ai/http-web/client/composables/crudLookupFieldRuntime`.
|
|
116
|
+
`resolveLookupItems(fieldKey, { selectedValue, selectedRecord })` returns
|
|
117
|
+
`{ value, label, record }` options. When the loaded record carries a hydrated
|
|
118
|
+
lookup label for a selected value missing from the current options, that option
|
|
119
|
+
is listed first; a selected value without a label is not listed.
|
|
120
|
+
|
|
121
|
+
Render selection fields whose options load from the server, or whose value is a
|
|
122
|
+
record id, with `SelectField` from
|
|
123
|
+
`@jskit-ai/http-web/client/components/SelectField`, not a raw `v-select`,
|
|
124
|
+
`v-autocomplete` or `v-combobox`. Vuetify titles a value missing from `items`
|
|
125
|
+
with the value itself, so a stored id such as `53` shows until the options
|
|
126
|
+
arrive. `SelectField` hides an unresolved value while `loading` is true, shows
|
|
127
|
+
"Unavailable option" once the options settle, and remembers titles it has seen
|
|
128
|
+
so a narrowing search does not blank a chosen value. Display never changes the
|
|
129
|
+
stored value. `searchable` renders an autocomplete; other props and slots pass
|
|
130
|
+
through to Vuetify.
|
|
131
|
+
|
|
132
|
+
```vue
|
|
133
|
+
<CrudAddEditScreen :screen="screen" v-bind="lookups">
|
|
134
|
+
<template #fields="{ formState, addEdit, resolveLookupItems, resolveLookupLoading, resolveLookupSearch, setLookupSearch }">
|
|
135
|
+
<v-col cols="12" md="6">
|
|
136
|
+
<SelectField
|
|
137
|
+
v-model="formState.serviceId"
|
|
138
|
+
searchable
|
|
139
|
+
no-filter
|
|
140
|
+
label="Service"
|
|
141
|
+
item-title="label"
|
|
142
|
+
item-value="value"
|
|
143
|
+
:items="resolveLookupItems('serviceId', { selectedValue: formState.serviceId, selectedRecord: addEdit.resource.data.value })"
|
|
144
|
+
:loading="resolveLookupLoading('serviceId')"
|
|
145
|
+
:search="resolveLookupSearch('serviceId')"
|
|
146
|
+
@update:search="setLookupSearch('serviceId', $event)"
|
|
147
|
+
/>
|
|
148
|
+
</v-col>
|
|
149
|
+
</template>
|
|
150
|
+
</CrudAddEditScreen>
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Here `lookups` is the page's `createCrudLookupFieldRuntime()` result for the
|
|
154
|
+
same form fields.
|
|
155
|
+
|
|
111
156
|
## Verification
|
|
112
157
|
|
|
113
158
|
- Mount each route with a representative resource response.
|
|
114
159
|
- Verify skeleton, empty, success, local load-error, and retry states.
|
|
115
160
|
- Verify create, edit, validation failure, delete confirmation, and navigation.
|
|
116
161
|
- Verify filters and actions when declared.
|
|
162
|
+
- Delay lookup options and confirm a selection field never shows a stored id.
|
|
117
163
|
- Exercise compact, medium, and expanded widths with Playwright.
|
|
118
164
|
- Confirm keyboard access, 48 CSS-pixel compact targets, no horizontal overflow,
|
|
119
165
|
and toast-based mutation feedback.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script setup>
|
|
2
|
+
import { computed, ref } from "vue";
|
|
3
|
+
import { VAutocomplete, VSelect } from "vuetify/components";
|
|
4
|
+
import { optionTitle, optionValue, selectionFieldState } from "../support/selectFieldSupport.js";
|
|
5
|
+
|
|
6
|
+
// A v-select / v-autocomplete that never shows a stored id as text (see selectFieldSupport.js).
|
|
7
|
+
// Pass `searchable` for VAutocomplete, and `loading` while the options are being fetched.
|
|
8
|
+
defineOptions({ inheritAttrs: false });
|
|
9
|
+
|
|
10
|
+
const props = defineProps({
|
|
11
|
+
searchable: { type: Boolean, default: false },
|
|
12
|
+
modelValue: { type: null, default: null },
|
|
13
|
+
items: { type: Array, default: () => [] },
|
|
14
|
+
itemTitle: { type: [String, Array, Function], default: "title" },
|
|
15
|
+
itemValue: { type: [String, Array, Function], default: "value" },
|
|
16
|
+
multiple: { type: Boolean, default: false },
|
|
17
|
+
returnObject: { type: Boolean, default: false },
|
|
18
|
+
loading: { type: [Boolean, String], default: false }
|
|
19
|
+
});
|
|
20
|
+
const emit = defineEmits(["update:modelValue"]);
|
|
21
|
+
|
|
22
|
+
const field = ref(null);
|
|
23
|
+
const seen = new Map();
|
|
24
|
+
const state = computed(() => selectionFieldState(props, seen));
|
|
25
|
+
const titleOf = (item) => optionTitle(item, props.itemTitle);
|
|
26
|
+
const valueOf = (item) => optionValue(item, props.itemTitle, props.itemValue);
|
|
27
|
+
|
|
28
|
+
function update(value) {
|
|
29
|
+
// A value hidden while its options load is kept when the user edits a multiple selection.
|
|
30
|
+
const hidden = state.value.hiddenValues;
|
|
31
|
+
emit("update:modelValue", props.multiple && hidden.length ? [...hidden, ...[].concat(value ?? [])] : value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
defineExpose({ focus: () => field.value?.focus() });
|
|
35
|
+
</script>
|
|
36
|
+
|
|
37
|
+
<template>
|
|
38
|
+
<component
|
|
39
|
+
:is="searchable ? VAutocomplete : VSelect"
|
|
40
|
+
ref="field"
|
|
41
|
+
v-bind="$attrs"
|
|
42
|
+
:model-value="state.modelValue"
|
|
43
|
+
:items="state.items"
|
|
44
|
+
:item-title="returnObject ? itemTitle : titleOf"
|
|
45
|
+
:item-value="returnObject ? itemValue : valueOf"
|
|
46
|
+
:multiple="multiple"
|
|
47
|
+
:return-object="returnObject"
|
|
48
|
+
:loading="loading"
|
|
49
|
+
@update:model-value="update"
|
|
50
|
+
>
|
|
51
|
+
<template v-for="(_, name) in $slots" #[name]="slotProps">
|
|
52
|
+
<slot :name="name" v-bind="slotProps || {}" />
|
|
53
|
+
</template>
|
|
54
|
+
</component>
|
|
55
|
+
</template>
|
|
@@ -7,10 +7,7 @@ import {
|
|
|
7
7
|
} from "@jskit-ai/resource-crud-core/shared/crudLookup";
|
|
8
8
|
import { normalizeSurfaceId } from "@jskit-ai/kernel/shared/surface/registry";
|
|
9
9
|
import { useList } from "../records/useList.js";
|
|
10
|
-
import {
|
|
11
|
-
resolveLookupItemLabel,
|
|
12
|
-
resolveLookupFieldDisplayValue
|
|
13
|
-
} from "./crudLookupFieldLabelSupport.js";
|
|
10
|
+
import { resolveLookupItemLabel } from "./crudLookupFieldLabelSupport.js";
|
|
14
11
|
import { inferCrudLookupJsonApiTransport } from "./crudJsonApiTransportSupport.js";
|
|
15
12
|
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
16
13
|
import { useJskitText } from "../../i18n.js";
|
|
@@ -48,22 +45,16 @@ function createSelectedLookupItem(selectedValue, selectedRecord = {}, entry = {}
|
|
|
48
45
|
return null;
|
|
49
46
|
}
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
{
|
|
59
|
-
key: entry.fieldKey,
|
|
60
|
-
relation: entry.relation
|
|
61
|
-
}
|
|
62
|
-
);
|
|
63
|
-
const label = displayValue == null || displayValue === "" ? value : displayValue;
|
|
48
|
+
// Without a hydrated label the only title would be the raw id; leave the
|
|
49
|
+
// selected value unlisted so the field shows its loading or unavailable state.
|
|
50
|
+
const label = resolveLookupItemLabel(hydratedLookup, entry.labelKey);
|
|
51
|
+
if (!label) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
64
55
|
return {
|
|
65
56
|
value,
|
|
66
|
-
label
|
|
57
|
+
label,
|
|
67
58
|
record: hydratedLookup
|
|
68
59
|
};
|
|
69
60
|
}
|
|
@@ -144,16 +135,7 @@ function createCrudLookupFieldRuntime({
|
|
|
144
135
|
fieldKey: key,
|
|
145
136
|
lookupContainerKey: relationLookupContainerKey,
|
|
146
137
|
valueKey,
|
|
147
|
-
labelKey
|
|
148
|
-
relation: Object.freeze({
|
|
149
|
-
kind: "lookup",
|
|
150
|
-
namespace,
|
|
151
|
-
...(explicitApiPath ? { apiPath: explicitApiPath } : {}),
|
|
152
|
-
...(relationSurfaceId ? { surfaceId: relationSurfaceId } : {}),
|
|
153
|
-
containerKey: relationLookupContainerKey,
|
|
154
|
-
valueKey,
|
|
155
|
-
...(labelKey ? { labelKey } : {})
|
|
156
|
-
})
|
|
138
|
+
labelKey
|
|
157
139
|
}));
|
|
158
140
|
}
|
|
159
141
|
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useJskitText } from "../i18n.js";
|
|
2
|
+
|
|
3
|
+
// Display rules for SelectField. Vuetify titles a model value missing from `items` with the raw
|
|
4
|
+
// value itself, so a stored id ("53") flashes while options load. Here a missing value shows the last
|
|
5
|
+
// title this field saw for it; otherwise it is hidden while `loading` and reads "Unavailable option" once
|
|
6
|
+
// the options have settled. The stored value is never changed by display alone.
|
|
7
|
+
const { t } = useJskitText();
|
|
8
|
+
const unavailableOption = Symbol("unavailable option");
|
|
9
|
+
|
|
10
|
+
// Mirrors Vuetify's item-title/item-value lookup: a key path, a function, or the primitive item.
|
|
11
|
+
function readOption(item, key, fallback) {
|
|
12
|
+
if (typeof key === "function") {
|
|
13
|
+
const value = key(item, fallback);
|
|
14
|
+
return value === undefined ? fallback : value;
|
|
15
|
+
}
|
|
16
|
+
if (item === null || typeof item !== "object" || key == null) {
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
const value = (Array.isArray(key) ? key : String(key).split(".")).reduce((source, part) => source?.[part], item);
|
|
20
|
+
return value === undefined ? fallback : value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function optionTitle(item, itemTitle) {
|
|
24
|
+
return item?.[unavailableOption] ? item.title : readOption(item, itemTitle, item);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function optionValue(item, itemTitle, itemValue) {
|
|
28
|
+
return item?.[unavailableOption] ? item.value : readOption(item, itemValue, readOption(item, itemTitle, item));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function selectedValues(modelValue, multiple) {
|
|
32
|
+
if (multiple) {
|
|
33
|
+
return modelValue == null ? [] : [].concat(modelValue);
|
|
34
|
+
}
|
|
35
|
+
return modelValue == null || modelValue === "" ? [] : [modelValue];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// `seen` (value → item) is the field's memory of titles, so a search that narrows the options
|
|
39
|
+
// does not blank a value chosen earlier.
|
|
40
|
+
function selectionFieldState(field, seen) {
|
|
41
|
+
const { modelValue, items = [], itemTitle = "title", itemValue = "value", multiple = false, returnObject = false } = field;
|
|
42
|
+
if (returnObject) {
|
|
43
|
+
// The model is the option itself and carries its own title.
|
|
44
|
+
return { modelValue, items, hiddenValues: [] };
|
|
45
|
+
}
|
|
46
|
+
const listed = new Set();
|
|
47
|
+
for (const item of items) {
|
|
48
|
+
const value = optionValue(item, itemTitle, itemValue);
|
|
49
|
+
listed.add(value);
|
|
50
|
+
seen.set(value, item);
|
|
51
|
+
}
|
|
52
|
+
const missing = selectedValues(modelValue, multiple).filter((value) => !listed.has(value));
|
|
53
|
+
const remembered = missing.filter((value) => seen.has(value)).map((value) => seen.get(value));
|
|
54
|
+
const unresolved = missing.filter((value) => !seen.has(value));
|
|
55
|
+
if (field.loading && unresolved.length) {
|
|
56
|
+
return {
|
|
57
|
+
modelValue: multiple ? [].concat(modelValue).filter((value) => !unresolved.includes(value)) : null,
|
|
58
|
+
items: [...items, ...remembered],
|
|
59
|
+
hiddenValues: unresolved
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const title = t("jskit.http.selectField.unavailableOption");
|
|
63
|
+
const unavailable = unresolved.map((value) => ({ [unavailableOption]: true, title, value }));
|
|
64
|
+
return {
|
|
65
|
+
modelValue,
|
|
66
|
+
items: missing.length ? [...items, ...remembered, ...unavailable] : items,
|
|
67
|
+
hiddenValues: []
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { optionTitle, optionValue, selectionFieldState };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
+
import { registerHooks } from "node:module";
|
|
2
3
|
import test from "node:test";
|
|
3
4
|
import {
|
|
4
5
|
resolveLookupItemLabel,
|
|
@@ -6,6 +7,26 @@ import {
|
|
|
6
7
|
resolveRecordTitle
|
|
7
8
|
} from "../src/client/composables/crud/crudLookupFieldLabelSupport.js";
|
|
8
9
|
|
|
10
|
+
// The lookup list is the first page of services, which does not contain the selected service 53.
|
|
11
|
+
const useListUrl = new URL("../src/client/composables/records/useList.js", import.meta.url).href;
|
|
12
|
+
const hooks = registerHooks({
|
|
13
|
+
load(url, context, nextLoad) {
|
|
14
|
+
if (url !== useListUrl) return nextLoad(url, context);
|
|
15
|
+
return {
|
|
16
|
+
format: "module",
|
|
17
|
+
shortCircuit: true,
|
|
18
|
+
source: "export const useList = () => ({ items: [{ id: 7, name: 'Bath' }] });"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const { createCrudLookupFieldRuntime } = await import("../src/client/composables/crud/crudLookupFieldRuntime.js");
|
|
23
|
+
hooks.deregister();
|
|
24
|
+
|
|
25
|
+
const serviceFormFields = [{
|
|
26
|
+
key: "serviceId",
|
|
27
|
+
relation: { kind: "lookup", namespace: "services", valueKey: "id", labelKey: "name" }
|
|
28
|
+
}];
|
|
29
|
+
|
|
9
30
|
test("resolveLookupItemLabel composes name + surname", () => {
|
|
10
31
|
assert.equal(
|
|
11
32
|
resolveLookupItemLabel(
|
|
@@ -262,3 +283,24 @@ test("resolveLookupFieldDisplayValue supports scalar lookup descriptor arguments
|
|
|
262
283
|
"Harbor Vet"
|
|
263
284
|
);
|
|
264
285
|
});
|
|
286
|
+
|
|
287
|
+
test("lookup items never title the selected value with its raw id", () => {
|
|
288
|
+
const { resolveLookupItems } = createCrudLookupFieldRuntime({ formFields: serviceFormFields });
|
|
289
|
+
const listed = [{ value: "7", label: "Bath", record: { id: 7, name: "Bath" } }];
|
|
290
|
+
|
|
291
|
+
assert.deepEqual(resolveLookupItems("serviceId", { selectedValue: 53, selectedRecord: { serviceId: 53 } }), listed);
|
|
292
|
+
assert.deepEqual(
|
|
293
|
+
resolveLookupItems("serviceId", {
|
|
294
|
+
selectedValue: 53,
|
|
295
|
+
selectedRecord: { serviceId: 53, lookups: { serviceId: { id: 53 } } }
|
|
296
|
+
}),
|
|
297
|
+
listed
|
|
298
|
+
);
|
|
299
|
+
assert.deepEqual(
|
|
300
|
+
resolveLookupItems("serviceId", {
|
|
301
|
+
selectedValue: 53,
|
|
302
|
+
selectedRecord: { serviceId: 53, lookups: { serviceId: { id: 53, name: "Daycare - One Day" } } }
|
|
303
|
+
}),
|
|
304
|
+
[{ value: "53", label: "Daycare - One Day", record: { id: 53, name: "Daycare - One Day" } }, ...listed]
|
|
305
|
+
);
|
|
306
|
+
});
|
|
@@ -21,6 +21,7 @@ test("http-web exports are explicit and aligned with production and generated us
|
|
|
21
21
|
"./client/components/CrudListFilterSurface",
|
|
22
22
|
"./client/components/CrudListScreen",
|
|
23
23
|
"./client/components/CrudViewScreen",
|
|
24
|
+
"./client/components/SelectField",
|
|
24
25
|
"./client/bulkActions",
|
|
25
26
|
"./client/filters",
|
|
26
27
|
"./client/rowActions",
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import test from "node:test";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { chromium, expect } from "@playwright/test";
|
|
6
|
+
import { createChromiumLaunchOptions, startViteFixture, stopProcess } from "../../../tooling/testUtils/browserFixture.mjs";
|
|
7
|
+
|
|
8
|
+
const FIELDS = Object.freeze(["Service", "Retired service", "Extras"]);
|
|
9
|
+
|
|
10
|
+
function field(page, label) {
|
|
11
|
+
return page.locator(".v-input").filter({ has: page.getByText(label, { exact: true }) });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function readModels(page) {
|
|
15
|
+
return JSON.parse(await page.getByTestId("select-field-models").textContent());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function assertNoStoredIdShown(page) {
|
|
19
|
+
for (const label of FIELDS) {
|
|
20
|
+
const words = (await field(page, label).innerText()).split(/\s+/u);
|
|
21
|
+
assert.ok(!words.some((word) => /^\d+$/u.test(word)), `${label} shows a stored id: ${words.join(" ")}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
test("SelectField never shows a stored id while options load and titles unmatched values afterwards", {
|
|
26
|
+
skip: process.env.JSKIT_HTTP_WEB_SELECT_FIELD_BROWSER === "1"
|
|
27
|
+
? false
|
|
28
|
+
: "set JSKIT_HTTP_WEB_SELECT_FIELD_BROWSER=1 to run the select field browser regression",
|
|
29
|
+
timeout: 120_000
|
|
30
|
+
}, async () => {
|
|
31
|
+
const runtime = await startViteFixture({ fixtureRoot: fileURLToPath(new URL("../fixtures/select-field/", import.meta.url)) });
|
|
32
|
+
let browser = null;
|
|
33
|
+
try {
|
|
34
|
+
browser = await chromium.launch(createChromiumLaunchOptions());
|
|
35
|
+
const page = await browser.newPage({ baseURL: runtime.baseURL });
|
|
36
|
+
const errors = [];
|
|
37
|
+
page.on("pageerror", (error) => errors.push(error.message));
|
|
38
|
+
await page.goto("/", { waitUntil: "networkidle" });
|
|
39
|
+
|
|
40
|
+
// Options are still loading: unresolved values are hidden and the fields show their loading state.
|
|
41
|
+
await expect(field(page, "Extras")).toContainText("Bath");
|
|
42
|
+
await assertNoStoredIdShown(page);
|
|
43
|
+
for (const label of FIELDS) {
|
|
44
|
+
await expect(field(page, label).locator(".v-field--loading")).toHaveCount(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Removing the visible chip keeps the value hidden while its options load.
|
|
48
|
+
await field(page, "Extras").locator(".v-chip__close").click();
|
|
49
|
+
assert.deepEqual(await readModels(page), { service: 53, retired: 99, serviceIds: [53] });
|
|
50
|
+
|
|
51
|
+
await page.evaluate(() => window.releaseOptions());
|
|
52
|
+
await expect(field(page, "Service")).toContainText("Daycare - One Day");
|
|
53
|
+
await expect(field(page, "Retired service")).toContainText("Unavailable option");
|
|
54
|
+
await expect(field(page, "Extras")).toContainText("Daycare - One Day");
|
|
55
|
+
await expect(page.locator(".v-field--loading")).toHaveCount(0);
|
|
56
|
+
await assertNoStoredIdShown(page);
|
|
57
|
+
assert.deepEqual(await readModels(page), { service: 53, retired: 99, serviceIds: [53] });
|
|
58
|
+
assert.deepEqual(errors, []);
|
|
59
|
+
} finally {
|
|
60
|
+
await browser?.close();
|
|
61
|
+
await stopProcess(runtime);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { optionTitle, optionValue, selectionFieldState } from "../src/client/support/selectFieldSupport.js";
|
|
4
|
+
|
|
5
|
+
const services = [
|
|
6
|
+
{ value: 7, label: "Bath" },
|
|
7
|
+
{ value: 53, label: "Daycare - One Day" }
|
|
8
|
+
];
|
|
9
|
+
const field = { itemTitle: "label", itemValue: "value" };
|
|
10
|
+
const titles = (state) => state.items.map((item) => optionTitle(item, field.itemTitle));
|
|
11
|
+
|
|
12
|
+
test("an unresolved value is hidden while options load, never shown as its id", () => {
|
|
13
|
+
const state = selectionFieldState({ ...field, modelValue: 53, items: [], loading: true }, new Map());
|
|
14
|
+
|
|
15
|
+
assert.equal(state.modelValue, null);
|
|
16
|
+
assert.deepEqual(state.items, []);
|
|
17
|
+
assert.deepEqual(state.hiddenValues, [53]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("a listed value keeps its own option and title", () => {
|
|
21
|
+
const state = selectionFieldState({ ...field, modelValue: 53, items: services, loading: true }, new Map());
|
|
22
|
+
|
|
23
|
+
assert.equal(state.modelValue, 53);
|
|
24
|
+
assert.equal(state.items, services);
|
|
25
|
+
assert.deepEqual(state.hiddenValues, []);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("an unmatched value reads Unavailable option once options settle and keeps its stored value", () => {
|
|
29
|
+
const state = selectionFieldState({ ...field, modelValue: 99, items: services }, new Map());
|
|
30
|
+
|
|
31
|
+
assert.equal(state.modelValue, 99);
|
|
32
|
+
assert.deepEqual(titles(state), ["Bath", "Daycare - One Day", "Unavailable option"]);
|
|
33
|
+
assert.equal(optionValue(state.items[2], field.itemTitle, field.itemValue), 99);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("a remembered title survives a narrowed option list", () => {
|
|
37
|
+
const seen = new Map();
|
|
38
|
+
selectionFieldState({ ...field, modelValue: 53, items: services }, seen);
|
|
39
|
+
const narrowed = selectionFieldState({ ...field, modelValue: 53, items: [services[0]], loading: true }, seen);
|
|
40
|
+
|
|
41
|
+
assert.equal(narrowed.modelValue, 53);
|
|
42
|
+
assert.deepEqual(titles(narrowed), ["Bath", "Daycare - One Day"]);
|
|
43
|
+
assert.deepEqual(narrowed.hiddenValues, []);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("multiple selections hide only their unresolved values while loading", () => {
|
|
47
|
+
const state = selectionFieldState(
|
|
48
|
+
{ ...field, multiple: true, modelValue: [7, 99], items: [services[0]], loading: true },
|
|
49
|
+
new Map()
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
assert.deepEqual(state.modelValue, [7]);
|
|
53
|
+
assert.deepEqual(state.hiddenValues, [99]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("item-title and item-value follow Vuetify key paths, functions, and primitive items", () => {
|
|
57
|
+
assert.equal(optionTitle({ service: { name: "Bath" } }, "service.name"), "Bath");
|
|
58
|
+
assert.equal(optionTitle({ name: "Bath" }, (item) => item.name.toUpperCase()), "BATH");
|
|
59
|
+
assert.equal(optionValue("Bath", "title", "value"), "Bath");
|
|
60
|
+
assert.equal(optionValue({ title: "Bath" }, "title", "value"), "Bath");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("returnObject models pass through unchanged", () => {
|
|
64
|
+
const modelValue = { value: 99, label: "Grooming" };
|
|
65
|
+
const state = selectionFieldState({ ...field, returnObject: true, modelValue, items: services, loading: true }, new Map());
|
|
66
|
+
|
|
67
|
+
assert.deepEqual(state, { modelValue, items: services, hiddenValues: [] });
|
|
68
|
+
});
|