Merge pull request 'SEV-408: combobox displayValue option (v0.5.0)' (#6) from sev-408/combobox-displayvalue into main
This commit was merged in pull request #6.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@switchev/web-shared",
|
||||
"version": "0.4.0",
|
||||
"version": "0.5.0",
|
||||
"description": "Shared browser helpers (formatters, markdown, image polling, combobox) for the SwitchEV frontends",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
|
||||
+7
-2
@@ -46,6 +46,10 @@ export function comboboxData(config = {}) {
|
||||
const allowFree = config.allowFree !== false;
|
||||
const fuzzy = config.fuzzy !== false;
|
||||
const limit = config.limit || 50;
|
||||
// displayValue: after selecting, show the option's VALUE (e.g. a state code
|
||||
// "TX") in the input instead of its LABEL ("Texas"), while the dropdown stays
|
||||
// label-searchable. Lets a field be searched by name but display the code.
|
||||
const displayValue = config.displayValue === true;
|
||||
|
||||
return {
|
||||
value: config.value ?? '',
|
||||
@@ -89,7 +93,8 @@ export function comboboxData(config = {}) {
|
||||
return;
|
||||
}
|
||||
const match = this._options.find((o) => o.value === String(v));
|
||||
this.query = match ? match.label : allowFree ? String(v) : '';
|
||||
if (match) this.query = displayValue ? match.value : match.label;
|
||||
else this.query = allowFree ? String(v) : '';
|
||||
},
|
||||
|
||||
get filtered() {
|
||||
@@ -121,7 +126,7 @@ export function comboboxData(config = {}) {
|
||||
|
||||
choose(opt) {
|
||||
this.value = opt.value;
|
||||
this.query = opt.label;
|
||||
this.query = displayValue ? opt.value : opt.label;
|
||||
this.close();
|
||||
},
|
||||
|
||||
|
||||
@@ -64,3 +64,30 @@ test('setOptions feeds reactive option lists (SEV-392)', () => {
|
||||
c.query = 'tes';
|
||||
assert.deepEqual(c.filtered.map((o) => o.value), ['Tesla']);
|
||||
});
|
||||
|
||||
test('displayValue shows the code in the input, searches by label (SEV-408)', () => {
|
||||
const c = comboboxData({
|
||||
options: [{ value: 'CA', label: 'California' }, { value: 'TX', label: 'Texas' }],
|
||||
allowFree: false,
|
||||
displayValue: true,
|
||||
});
|
||||
c.init();
|
||||
// search by name
|
||||
c.query = 'tex';
|
||||
assert.deepEqual(c.filtered.map((o) => o.value), ['TX']);
|
||||
// choosing shows the CODE, not the name
|
||||
c.choose({ value: 'TX', label: 'Texas' });
|
||||
assert.equal(c.value, 'TX');
|
||||
assert.equal(c.query, 'TX');
|
||||
});
|
||||
|
||||
test('displayValue syncs the code from an external value (SEV-408)', () => {
|
||||
const c = comboboxData({
|
||||
options: [{ value: 'OR', label: 'Oregon' }],
|
||||
allowFree: false,
|
||||
displayValue: true,
|
||||
value: 'OR',
|
||||
});
|
||||
c.init();
|
||||
assert.equal(c.query, 'OR'); // shows code, not "Oregon"
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user