Skip to content

Commit 94f07c4

Browse files
author
Jicheng Lu
committed
refine dropdown
1 parent f13c57b commit 94f07c4

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

src/lib/common/MultiSelect.svelte

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/** @type {string} */
99
export let tag;
1010
11-
/** @type {{key: string, value: string}[]} */
11+
/** @type {import('$commonTypes').LabelValuePair[]} */
1212
export let options = [];
1313
1414
/** @type {boolean} */
@@ -24,7 +24,7 @@
2424
export let selectedText = '';
2525
2626
/** @type {string[]} */
27-
export let selectedKeys;
27+
export let selectedLabels;
2828
2929
/** @type {string} */
3030
export let containerClasses = "";
@@ -51,10 +51,10 @@
5151
/** @type {boolean} */
5252
let showOptionList = false;
5353
54-
/** @type {{key: string, value: string, checked: boolean}[]} */
54+
/** @type {{label: string, value: string, checked: boolean}[]} */
5555
let innerOptions = [];
5656
57-
/** @type {{key: string, value: string, checked: boolean}[]} */
57+
/** @type {{label: string, value: string, checked: boolean}[]} */
5858
let refOptions = [];
5959
6060
/** @type {string} */
@@ -66,15 +66,15 @@
6666
onMount(() => {
6767
innerOptions = options.map(x => {
6868
return {
69-
key: x.key,
69+
label: x.label,
7070
value: x.value,
7171
checked: false
7272
}
7373
});
7474
7575
refOptions = options.map(x => {
7676
return {
77-
key: x.key,
77+
label: x.label,
7878
value: x.value,
7979
checked: false
8080
}
@@ -83,22 +83,22 @@
8383
8484
$: {
8585
innerOptions = innerOptions.map(x => {
86-
x.checked = !!selectedKeys?.includes(x.key);
86+
x.checked = !!selectedLabels?.includes(x.label);
8787
return {...x};
8888
});
8989
refOptions = refOptions.map(x => {
90-
x.checked = !!selectedKeys?.includes(x.key);
90+
x.checked = !!selectedLabels?.includes(x.label);
9191
return {...x};
9292
});
9393
changeDisplayText();
9494
}
9595
9696
$: {
9797
if (options.length > refOptions.length) {
98-
const curKeys = refOptions.map(x => x.key);
99-
const newOptions = options.filter(x => !curKeys.includes(x.key)).map(x => {
98+
const curKeys = refOptions.map(x => x.label);
99+
const newOptions = options.filter(x => !curKeys.includes(x.label)).map(x => {
100100
return {
101-
key: x.key,
101+
label: x.label,
102102
value: x.value,
103103
checked: false
104104
};
@@ -147,14 +147,14 @@
147147
*/
148148
function checkOption(e, option) {
149149
innerOptions = innerOptions.map(x => {
150-
if (x.key == option.key) {
150+
if (x.label == option.label) {
151151
x.checked = e == null ? !x.checked : e.target.checked;
152152
}
153153
return { ...x };
154154
});
155155
156156
refOptions = refOptions.map(x => {
157-
if (x.key == option.key) {
157+
if (x.label == option.label) {
158158
x.checked = e == null ? !x.checked : e.target.checked;
159159
}
160160
return { ...x };
@@ -178,9 +178,9 @@
178178
179179
/** @param {boolean} checked */
180180
function syncChangesToRef(checked) {
181-
const keys = innerOptions.map(x => x.key);
181+
const keys = innerOptions.map(x => x.label);
182182
refOptions = refOptions.map(x => {
183-
if (keys.includes(x.key)) {
183+
if (keys.includes(x.label)) {
184184
return {
185185
...x,
186186
checked: checked
@@ -229,7 +229,7 @@
229229
230230
function sendEvent() {
231231
svelteDispatch("select", {
232-
selecteds: refOptions.filter(x => !!x.checked).map(x => ({ key: x.key, value: x.value }))
232+
selecteds: refOptions.filter(x => !!x.checked).map(x => ({ label: x.label, value: x.value }))
233233
});
234234
}
235235
@@ -343,7 +343,7 @@
343343
/>
344344
</div>
345345
<div class="line-align-center select-name">
346-
{option.value}
346+
{option.label}
347347
</div>
348348
</li>
349349
{/each}

src/lib/helpers/types/commonTypes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
* @property {string} name - The name.
1818
*/
1919

20+
/**
21+
* @typedef {Object} LabelValuePair
22+
* @property {string} label - The label.
23+
* @property {string} value - The value.
24+
*/
25+
2026
/**
2127
* @template T
2228
* @typedef {Object} PagedItems<T>

src/routes/page/agent/+page.svelte

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@
4343
/** @type {any} */
4444
let unsubscriber;
4545
46+
/** @type {import('$commonTypes').LabelValuePair[]} */
4647
const agentTypeOptions = Object.entries(AgentType).map(([k, v]) => (
47-
{ key: v, value: v }
48-
));
48+
{ label: v, value: v }
49+
)).sort((a, b) => a.label.localeCompare(b.label));
4950
50-
/** @type {{ key: string, value: string }[]} */
51+
/** @type {import('$commonTypes').LabelValuePair[]} */
5152
let agentLabelOptions = [];
5253
5354
/** @type {string[]} */
@@ -91,7 +92,7 @@
9192
9293
function getAgentLabelOptions() {
9394
return getAgentLabels().then(res => {
94-
agentLabelOptions = res?.map(x => ({ key: x, value: x })) || [];
95+
agentLabelOptions = res?.map(x => ({ label: x, value: x })) || [];
9596
}).catch(() => {
9697
agentLabelOptions = [];
9798
});
@@ -166,13 +167,13 @@
166167
/** @param {any} e */
167168
function selectAgentTypeOption(e) {
168169
// @ts-ignore
169-
selectedAgentTypes = e.detail.selecteds?.map(x => x.key) || [];
170+
selectedAgentTypes = e.detail.selecteds?.map(x => x.label) || [];
170171
}
171172
172173
/** @param {any} e */
173174
function selectAgentLabelOption(e) {
174175
// @ts-ignore
175-
selectedAgentLabels = e.detail.selecteds?.map(x => x.key) || [];
176+
selectedAgentLabels = e.detail.selecteds?.map(x => x.label) || [];
176177
}
177178
178179
function search() {
@@ -221,15 +222,16 @@
221222
placeholder={'Select labels'}
222223
selectedText={'labels'}
223224
searchMode
224-
selectedKeys={selectedAgentLabels}
225+
selectedLabels={selectedAgentLabels}
225226
options={agentLabelOptions}
226227
on:select={e => selectAgentLabelOption(e)}
227228
/>
228229
<MultiSelect
229230
tag={'agent-type-select'}
230231
placeholder={'Select types'}
231232
selectedText={'types'}
232-
selectedKeys={selectedAgentTypes}
233+
searchMode
234+
selectedLabels={selectedAgentTypes}
233235
options={agentTypeOptions}
234236
on:select={e => selectAgentTypeOption(e)}
235237
/>

src/routes/page/agent/[agentId]/agent-components/agent-utility.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@
215215
}
216216
217217
/**
218-
* @param {any[]} options
218+
* @param {string[]} options
219219
* @param {string} placeholder
220220
*/
221221
function getUtilityOptions(options, placeholder = '') {
222-
let list = options?.map(x => {
222+
let list = options?.sort((a, b) => a.localeCompare(b))?.map(x => {
223223
return {
224224
label: x,
225225
value: x
@@ -234,7 +234,7 @@
234234
}
235235
236236
/** @param {number} uid */
237-
function revertUtility(uid) {
237+
function resetUtility(uid) {
238238
const found = innerUtilities.find((_, index) => index === uid);
239239
if (!found) return;
240240
@@ -338,8 +338,8 @@
338338
class="mdi mdi-refresh clickable"
339339
data-bs-toggle="tooltip"
340340
data-bs-placement="top"
341-
title="Revert"
342-
on:click={() => revertUtility(uid)}
341+
title="Reset"
342+
on:click={() => resetUtility(uid)}
343343
/>
344344
</div>
345345
{/if}

0 commit comments

Comments
 (0)