gradio-pr-bot commited on
Commit
401f9de
·
verified ·
1 Parent(s): aecee4b

Upload folder using huggingface_hub

Browse files
6.3.1/dropdown/Example.svelte ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string | string[] | null;
3
+ export let type: "gallery" | "table";
4
+ export let selected = false;
5
+ export let choices: [string, string | number][];
6
+
7
+ let value_array = value ? (Array.isArray(value) ? value : [value]) : [];
8
+ let names = value_array
9
+ .map(
10
+ (val) =>
11
+ (
12
+ choices.find((pair) => pair[1] === val) as
13
+ | [string, string | number]
14
+ | undefined
15
+ )?.[0]
16
+ )
17
+ .filter((name) => name !== undefined);
18
+ let names_string = names.join(", ");
19
+ </script>
20
+
21
+ <div
22
+ class:table={type === "table"}
23
+ class:gallery={type === "gallery"}
24
+ class:selected
25
+ >
26
+ {names_string}
27
+ </div>
28
+
29
+ <style>
30
+ .gallery {
31
+ padding: var(--size-1) var(--size-2);
32
+ }
33
+ </style>
6.3.1/dropdown/Index.svelte ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BaseDropdown } from "./shared/Dropdown.svelte";
3
+ export { default as BaseDropdownOptions } from "./shared/DropdownOptions.svelte";
4
+ export { default as BaseMultiselect } from "./shared/Multiselect.svelte";
5
+ export { default as BaseExample } from "./Example.svelte";
6
+ </script>
7
+
8
+ <script lang="ts">
9
+ import { Gradio } from "@gradio/utils";
10
+ import Multiselect from "./shared/Multiselect.svelte";
11
+ import Dropdown from "./shared/Dropdown.svelte";
12
+ import { Block, IconButtonWrapper } from "@gradio/atoms";
13
+ import { StatusTracker } from "@gradio/statustracker";
14
+ import type { DropdownProps, DropdownEvents } from "./types.ts";
15
+
16
+ let props = $props();
17
+ const gradio = new Gradio<DropdownEvents, DropdownProps>(props);
18
+ </script>
19
+
20
+ <Block
21
+ visible={gradio.shared.visible}
22
+ elem_id={gradio.shared.elem_id}
23
+ elem_classes={gradio.shared.elem_classes}
24
+ padding={gradio.shared.container}
25
+ allow_overflow={false}
26
+ scale={gradio.shared.scale}
27
+ min_width={gradio.shared.min_width}
28
+ >
29
+ <StatusTracker
30
+ autoscroll={gradio.shared.autoscroll}
31
+ i18n={gradio.i18n}
32
+ {...gradio.shared.loading_status}
33
+ on_clear_status={() => gradio.dispatch("clear_status", loading_status)}
34
+ />
35
+
36
+ {#if gradio.props.multiselect}
37
+ <Multiselect {gradio} />
38
+ {:else}
39
+ <Dropdown
40
+ label={gradio.shared.label}
41
+ info={gradio.props.info}
42
+ bind:value={gradio.props.value}
43
+ choices={gradio.props.choices}
44
+ interactive={gradio.shared.interactive}
45
+ show_label={gradio.shared.show_label}
46
+ container={gradio.shared.container}
47
+ allow_custom_value={gradio.props.allow_custom_value}
48
+ filterable={gradio.props.filterable}
49
+ buttons={gradio.props.buttons}
50
+ on_custom_button_click={(id) => {
51
+ gradio.dispatch("custom_button_click", { id });
52
+ }}
53
+ on_change={() => gradio.dispatch("change")}
54
+ on_input={() => gradio.dispatch("input")}
55
+ on_select={(data) => gradio.dispatch("select", data)}
56
+ on_focus={() => gradio.dispatch("focus")}
57
+ on_blur={() => gradio.dispatch("blur")}
58
+ on_key_up={(data) => gradio.dispatch("key_up", data)}
59
+ />
60
+ {/if}
61
+ </Block>
6.3.1/dropdown/package.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dropdown",
3
+ "version": "0.11.1",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "exports": {
11
+ ".": {
12
+ "gradio": "./Index.svelte",
13
+ "svelte": "./dist/Index.svelte",
14
+ "types": "./dist/Index.svelte.d.ts"
15
+ },
16
+ "./example": {
17
+ "gradio": "./Example.svelte",
18
+ "svelte": "./dist/Example.svelte",
19
+ "types": "./dist/Example.svelte.d.ts"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "dependencies": {
24
+ "@gradio/atoms": "workspace:^",
25
+ "@gradio/icons": "workspace:^",
26
+ "@gradio/statustracker": "workspace:^",
27
+ "@gradio/utils": "workspace:^"
28
+ },
29
+ "devDependencies": {
30
+ "@gradio/preview": "workspace:^"
31
+ },
32
+ "peerDependencies": {
33
+ "svelte": "^5.43.4"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/gradio-app/gradio.git",
38
+ "directory": "js/dropdown"
39
+ }
40
+ }
6.3.1/dropdown/shared/Dropdown.svelte ADDED
@@ -0,0 +1,308 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import DropdownOptions from "./DropdownOptions.svelte";
3
+ import { BlockTitle, IconButtonWrapper } from "@gradio/atoms";
4
+ import { DropdownArrow } from "@gradio/icons";
5
+ import { handle_filter, handle_shared_keys } from "./utils";
6
+ import {
7
+ type SelectData,
8
+ type KeyUpData,
9
+ type CustomButton as CustomButtonType
10
+ } from "@gradio/utils";
11
+ import { tick } from "svelte";
12
+
13
+ const is_browser = typeof window !== "undefined";
14
+
15
+ let {
16
+ label = "Dropdown",
17
+ info = undefined,
18
+ value = $bindable<string | number | null>(),
19
+ choices = [],
20
+ interactive = true,
21
+ show_label = true,
22
+ container = true,
23
+ allow_custom_value = false,
24
+ filterable = true,
25
+ buttons = null,
26
+ on_custom_button_click = null,
27
+ on_change,
28
+ on_input,
29
+ on_select,
30
+ on_focus,
31
+ on_blur,
32
+ on_key_up
33
+ }: {
34
+ label?: string;
35
+ info?: string;
36
+ value?: string | number | null;
37
+ choices?: [string, string | number][];
38
+ interactive?: boolean;
39
+ show_label?: boolean;
40
+ container?: boolean;
41
+ allow_custom_value?: boolean;
42
+ filterable?: boolean;
43
+ buttons?: (string | CustomButtonType)[] | null;
44
+ on_custom_button_click?: ((id: number) => void) | null;
45
+ on_change?: (value: string | number | null) => void;
46
+ on_input?: () => void;
47
+ on_select?: (data: SelectData) => void;
48
+ on_focus?: () => void;
49
+ on_blur?: () => void;
50
+ on_key_up?: (data: KeyUpData) => void;
51
+ } = $props();
52
+
53
+ let filter_input: HTMLElement;
54
+
55
+ let show_options = $derived.by(() => {
56
+ return is_browser && filter_input === document.activeElement;
57
+ });
58
+ let choices_names: string[] = $derived.by(() => {
59
+ return choices.map((c) => c[0]);
60
+ });
61
+ let choices_values: (string | number)[] = $derived.by(() => {
62
+ return choices.map((c) => c[1]);
63
+ });
64
+ let [input_text, selected_index] = $derived.by(() => {
65
+ if (
66
+ value === undefined ||
67
+ value === null ||
68
+ (Array.isArray(value) && value.length === 0)
69
+ ) {
70
+ return ["", null];
71
+ } else if (choices_values.includes(value as string | number)) {
72
+ return [
73
+ choices_names[choices_values.indexOf(value as string | number)],
74
+ choices_values.indexOf(value as string | number)
75
+ ];
76
+ } else if (allow_custom_value) {
77
+ return [value as string, null];
78
+ } else {
79
+ return ["", null];
80
+ }
81
+ });
82
+ // Use last_typed_value to track when the user has typed
83
+ // on_blur we only want to update value if the user has typed
84
+ let last_typed_value = input_text;
85
+ let initialized = $state(false);
86
+ let disabled = $derived(!interactive);
87
+
88
+ // All of these are indices with respect to the choices array
89
+ let filtered_indices = $state(choices.map((_, i) => i));
90
+ let active_index: number | null = $state(null);
91
+
92
+ function handle_option_selected(e: any): void {
93
+ selected_index = parseInt(e.detail.target.dataset.index);
94
+ if (isNaN(selected_index)) {
95
+ // This is the case when the user clicks on the scrollbar
96
+ selected_index = null;
97
+ return;
98
+ }
99
+
100
+ let [_input_text, _value] = choices[selected_index];
101
+ input_text = _input_text;
102
+ last_typed_value = input_text;
103
+ value = _value;
104
+ on_select?.({
105
+ index: selected_index,
106
+ value: choices_values[selected_index],
107
+ selected: true
108
+ });
109
+ show_options = false;
110
+ active_index = null;
111
+ on_input?.();
112
+ filter_input.blur();
113
+ }
114
+
115
+ function handle_focus(e: FocusEvent): void {
116
+ filtered_indices = choices.map((_, i) => i);
117
+ show_options = true;
118
+ on_focus?.();
119
+ }
120
+
121
+ function handle_blur(): void {
122
+ if (!allow_custom_value) {
123
+ input_text =
124
+ choices_names[choices_values.indexOf(value as string | number)];
125
+ } else {
126
+ if (choices_names.includes(input_text)) {
127
+ selected_index = choices_names.indexOf(input_text);
128
+ value = choices_values[selected_index];
129
+ } else if (input_text !== last_typed_value) {
130
+ value = input_text;
131
+ selected_index = null;
132
+ }
133
+ }
134
+ show_options = false;
135
+ active_index = null;
136
+ filtered_indices = choices.map((_, i) => i);
137
+ on_blur?.();
138
+ on_input?.();
139
+ }
140
+
141
+ async function handle_key_down(e: KeyboardEvent): Promise<void> {
142
+ await tick();
143
+ filtered_indices = handle_filter(choices, input_text);
144
+ active_index = filtered_indices.length > 0 ? filtered_indices[0] : null;
145
+ [show_options, active_index] = handle_shared_keys(
146
+ e,
147
+ active_index,
148
+ filtered_indices
149
+ );
150
+ if (e.key === "Enter") {
151
+ last_typed_value = input_text;
152
+ if (active_index !== null) {
153
+ selected_index = active_index;
154
+ value = choices_values[active_index];
155
+ show_options = false;
156
+ filter_input.blur();
157
+ active_index = null;
158
+ } else if (choices_names.includes(input_text)) {
159
+ selected_index = choices_names.indexOf(input_text);
160
+ value = choices_values[selected_index];
161
+ show_options = false;
162
+ active_index = null;
163
+ filter_input.blur();
164
+ } else if (allow_custom_value) {
165
+ value = input_text;
166
+ selected_index = null;
167
+ show_options = false;
168
+ active_index = null;
169
+ filter_input.blur();
170
+ }
171
+ }
172
+ }
173
+
174
+ let old_value = $state(value);
175
+ $effect(() => {
176
+ if (old_value !== value) {
177
+ old_value = value;
178
+ on_change?.(value);
179
+ }
180
+ });
181
+ </script>
182
+
183
+ <div class:container>
184
+ {#if show_label && buttons && buttons.length > 0}
185
+ <IconButtonWrapper {buttons} {on_custom_button_click} />
186
+ {/if}
187
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
188
+
189
+ <div class="wrap">
190
+ <div class="wrap-inner" class:show_options>
191
+ <div class="secondary-wrap">
192
+ <input
193
+ role="listbox"
194
+ aria-controls="dropdown-options"
195
+ aria-expanded={show_options}
196
+ aria-label={label}
197
+ class="border-none"
198
+ class:subdued={!choices_names.includes(input_text) &&
199
+ !allow_custom_value}
200
+ autocomplete="off"
201
+ {disabled}
202
+ bind:value={input_text}
203
+ bind:this={filter_input}
204
+ on:keydown={handle_key_down}
205
+ on:keyup={(e) => {
206
+ on_key_up?.({
207
+ key: e.key,
208
+ input_value: input_text
209
+ });
210
+ }}
211
+ on:blur={handle_blur}
212
+ on:focus={handle_focus}
213
+ readonly={!filterable}
214
+ />
215
+ {#if !disabled}
216
+ <div class="icon-wrap">
217
+ <DropdownArrow />
218
+ </div>
219
+ {/if}
220
+ </div>
221
+ </div>
222
+ <DropdownOptions
223
+ {show_options}
224
+ {choices}
225
+ {filtered_indices}
226
+ {disabled}
227
+ selected_indices={selected_index === null ? [] : [selected_index]}
228
+ {active_index}
229
+ on:change={handle_option_selected}
230
+ on:load={() => (initialized = true)}
231
+ />
232
+ </div>
233
+ </div>
234
+
235
+ <style>
236
+ .icon-wrap {
237
+ position: absolute;
238
+ top: 50%;
239
+ transform: translateY(-50%);
240
+ right: var(--size-5);
241
+ color: var(--body-text-color);
242
+ width: var(--size-5);
243
+ pointer-events: none;
244
+ }
245
+ .container {
246
+ height: 100%;
247
+ }
248
+ .container .wrap {
249
+ box-shadow: var(--input-shadow);
250
+ border: var(--input-border-width) solid var(--border-color-primary);
251
+ }
252
+
253
+ .wrap {
254
+ position: relative;
255
+ border-radius: var(--input-radius);
256
+ background: var(--input-background-fill);
257
+ }
258
+
259
+ .wrap:focus-within {
260
+ box-shadow: var(--input-shadow-focus);
261
+ border-color: var(--input-border-color-focus);
262
+ background: var(--input-background-fill-focus);
263
+ }
264
+
265
+ .wrap-inner {
266
+ display: flex;
267
+ position: relative;
268
+ flex-wrap: wrap;
269
+ align-items: center;
270
+ gap: var(--checkbox-label-gap);
271
+ padding: var(--checkbox-label-padding);
272
+ height: 100%;
273
+ }
274
+ .secondary-wrap {
275
+ display: flex;
276
+ flex: 1 1 0%;
277
+ align-items: center;
278
+ border: none;
279
+ min-width: min-content;
280
+ height: 100%;
281
+ }
282
+
283
+ input {
284
+ margin: var(--spacing-sm);
285
+ outline: none;
286
+ border: none;
287
+ background: inherit;
288
+ width: var(--size-full);
289
+ color: var(--body-text-color);
290
+ font-size: var(--input-text-size);
291
+ height: 100%;
292
+ }
293
+
294
+ input:disabled {
295
+ -webkit-text-fill-color: var(--body-text-color);
296
+ -webkit-opacity: 1;
297
+ opacity: 1;
298
+ cursor: not-allowed;
299
+ }
300
+
301
+ .subdued {
302
+ color: var(--body-text-color-subdued);
303
+ }
304
+
305
+ input[readonly] {
306
+ cursor: pointer;
307
+ }
308
+ </style>
6.3.1/dropdown/shared/DropdownOptions.svelte ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { fly } from "svelte/transition";
3
+ import { createEventDispatcher } from "svelte";
4
+ export let choices: [string, string | number][];
5
+ export let filtered_indices: number[];
6
+ export let show_options = false;
7
+ export let disabled = false;
8
+ export let selected_indices: (string | number)[] = [];
9
+ export let active_index: number | null = null;
10
+ export let remember_scroll = false;
11
+ export let offset_from_top = 0;
12
+ export let from_top = false;
13
+
14
+ let distance_from_top: number;
15
+ let distance_from_bottom: number;
16
+ let input_height: number;
17
+ let input_width: number;
18
+ let refElement: HTMLDivElement;
19
+ let listElement: HTMLUListElement;
20
+ let top: string | null, bottom: string | null, max_height: number;
21
+ let innerHeight: number;
22
+ let list_scroll_y = 0;
23
+
24
+ function calculate_window_distance(): void {
25
+ const { top: ref_top, bottom: ref_bottom } =
26
+ refElement.getBoundingClientRect();
27
+ if (from_top) {
28
+ distance_from_top = offset_from_top;
29
+ } else {
30
+ distance_from_top = ref_top;
31
+ }
32
+ distance_from_bottom = innerHeight - ref_bottom;
33
+ }
34
+
35
+ let scroll_timeout: NodeJS.Timeout | null = null;
36
+ function scroll_listener(): void {
37
+ if (!show_options) return;
38
+ if (scroll_timeout !== null) {
39
+ clearTimeout(scroll_timeout);
40
+ }
41
+
42
+ scroll_timeout = setTimeout(() => {
43
+ calculate_window_distance();
44
+ scroll_timeout = null;
45
+ }, 10);
46
+ }
47
+
48
+ function restore_last_scroll(): void {
49
+ listElement?.scrollTo?.(0, list_scroll_y);
50
+ }
51
+
52
+ $: {
53
+ if (show_options && refElement) {
54
+ if (remember_scroll) {
55
+ restore_last_scroll();
56
+ } else {
57
+ if (listElement && selected_indices.length > 0) {
58
+ let elements = listElement.querySelectorAll("li");
59
+ for (const element of Array.from(elements)) {
60
+ if (
61
+ element.getAttribute("data-index") ===
62
+ selected_indices[0].toString()
63
+ ) {
64
+ listElement?.scrollTo?.(0, (element as HTMLLIElement).offsetTop);
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ }
70
+ calculate_window_distance();
71
+ const rect = refElement.parentElement?.getBoundingClientRect();
72
+ input_height = rect?.height || 0;
73
+ input_width = rect?.width || 0;
74
+ }
75
+ if (distance_from_bottom > distance_from_top || from_top) {
76
+ top = `${distance_from_top}px`;
77
+ max_height = distance_from_bottom;
78
+ bottom = null;
79
+ } else {
80
+ bottom = `${distance_from_bottom + input_height}px`;
81
+ max_height = distance_from_top - input_height;
82
+ top = null;
83
+ }
84
+ }
85
+
86
+ const dispatch = createEventDispatcher();
87
+ </script>
88
+
89
+ <svelte:window on:scroll={scroll_listener} bind:innerHeight />
90
+
91
+ <div class="reference" bind:this={refElement} />
92
+ {#if show_options && !disabled}
93
+ <ul
94
+ class="options"
95
+ transition:fly={{ duration: 200, y: 5 }}
96
+ on:mousedown|preventDefault={(e) => dispatch("change", e)}
97
+ on:scroll={(e) => (list_scroll_y = e.currentTarget.scrollTop)}
98
+ style:top
99
+ style:bottom
100
+ style:max-height={`calc(${max_height}px - var(--window-padding))`}
101
+ style:width={input_width + "px"}
102
+ bind:this={listElement}
103
+ role="listbox"
104
+ >
105
+ {#each filtered_indices as index}
106
+ <li
107
+ class="item"
108
+ class:selected={selected_indices.includes(index)}
109
+ class:active={index === active_index}
110
+ class:bg-gray-100={index === active_index}
111
+ class:dark:bg-gray-600={index === active_index}
112
+ style:width={input_width + "px"}
113
+ data-index={index}
114
+ aria-label={choices[index][0]}
115
+ data-testid="dropdown-option"
116
+ role="option"
117
+ aria-selected={selected_indices.includes(index)}
118
+ >
119
+ <span class:hide={!selected_indices.includes(index)} class="inner-item">
120
+
121
+ </span>
122
+ {choices[index][0]}
123
+ </li>
124
+ {/each}
125
+ </ul>
126
+ {/if}
127
+
128
+ <style>
129
+ .options {
130
+ --window-padding: var(--size-8);
131
+ position: fixed;
132
+ z-index: var(--layer-top);
133
+ margin-left: 0;
134
+ box-shadow: var(--shadow-drop-lg);
135
+ border-radius: var(--container-radius);
136
+ background: var(--background-fill-primary);
137
+ min-width: fit-content;
138
+ max-width: inherit;
139
+ overflow: auto;
140
+ color: var(--body-text-color);
141
+ list-style: none;
142
+ }
143
+
144
+ .item {
145
+ display: flex;
146
+ cursor: pointer;
147
+ padding: var(--size-2);
148
+ word-break: break-word;
149
+ }
150
+
151
+ .item:hover,
152
+ .active {
153
+ background: var(--background-fill-secondary);
154
+ }
155
+
156
+ .inner-item {
157
+ padding-right: var(--size-1);
158
+ }
159
+
160
+ .hide {
161
+ visibility: hidden;
162
+ }
163
+ </style>
6.3.1/dropdown/shared/Multiselect.svelte ADDED
@@ -0,0 +1,398 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { _ } from "svelte-i18n";
3
+ import { BlockTitle, IconButtonWrapper } from "@gradio/atoms";
4
+ import { Remove, DropdownArrow } from "@gradio/icons";
5
+ import type { Gradio } from "@gradio/utils";
6
+ import DropdownOptions from "./DropdownOptions.svelte";
7
+ import { handle_filter, handle_shared_keys } from "./utils";
8
+ import type { DropdownEvents, DropdownProps, Item } from "../types.ts";
9
+
10
+ const props = $props();
11
+
12
+ const gradio: Gradio<DropdownEvents, DropdownProps> = props.gradio;
13
+
14
+ let filter_input: HTMLElement;
15
+ let input_text = $state("");
16
+ let label = $derived(gradio.shared.label || "Multiselect");
17
+ let buttons = $derived(gradio.props.buttons);
18
+
19
+ let choices_names: string[] = $derived.by(() => {
20
+ return gradio.props.choices.map((c) => c[0]);
21
+ });
22
+ let choices_values: (string | number)[] = $derived.by(() => {
23
+ return gradio.props.choices.map((c) => c[1]);
24
+ });
25
+
26
+ let disabled = $derived(!gradio.shared.interactive);
27
+
28
+ let show_options = $state(false);
29
+
30
+ // All of these are indices with respect to the choices array
31
+ let [filtered_indices, active_index] = $derived.by(() => {
32
+ const filtered = handle_filter(gradio.props.choices, input_text);
33
+ return [
34
+ filtered,
35
+ filtered.length > 0 && !gradio.props.allow_custom_value
36
+ ? filtered[0]
37
+ : null
38
+ ];
39
+ });
40
+
41
+ function set_selected_indices(): Item[] {
42
+ if (gradio.props.value === undefined) {
43
+ return [];
44
+ } else if (Array.isArray(gradio.props.value)) {
45
+ return gradio.props.value
46
+ .map((v) => {
47
+ const index = choices_values.indexOf(v);
48
+ if (index !== -1) {
49
+ return index;
50
+ }
51
+ if (gradio.props.allow_custom_value) {
52
+ return v;
53
+ }
54
+ // Instead of returning null, skip this iteration
55
+ return undefined;
56
+ })
57
+ .filter((val): val is string | number => val !== undefined);
58
+ }
59
+ return [];
60
+ }
61
+
62
+ let selected_indices: (number | string)[] = $derived.by(set_selected_indices);
63
+
64
+ function handle_blur(): void {
65
+ if (!gradio.props.allow_custom_value) {
66
+ input_text = "";
67
+ }
68
+
69
+ if (gradio.props.allow_custom_value && input_text !== "") {
70
+ add_selected_choice(input_text);
71
+ input_text = "";
72
+ }
73
+ gradio.dispatch("blur");
74
+ show_options = false;
75
+ active_index = null;
76
+ }
77
+
78
+ function remove_selected_choice(option_index: number | string) {
79
+ selected_indices = selected_indices.filter((v) => v !== option_index);
80
+ gradio.props.value = selected_indices.map((index) =>
81
+ typeof index === "number" ? choices_values[index] : index
82
+ );
83
+ gradio.dispatch("input");
84
+ gradio.dispatch("select", {
85
+ index: typeof option_index === "number" ? option_index : -1,
86
+ value:
87
+ typeof option_index === "number"
88
+ ? choices_values[option_index]
89
+ : option_index,
90
+ selected: false
91
+ });
92
+ }
93
+
94
+ function add_selected_choice(option_index: number | string) {
95
+ if (
96
+ gradio.props.max_choices == null ||
97
+ selected_indices.length < gradio.props.max_choices
98
+ ) {
99
+ selected_indices.push(option_index);
100
+ gradio.dispatch("select", {
101
+ index: typeof option_index === "number" ? option_index : -1,
102
+ value:
103
+ typeof option_index === "number"
104
+ ? choices_values[option_index]
105
+ : option_index,
106
+ selected: true
107
+ });
108
+ }
109
+ if (selected_indices.length === gradio.props.max_choices) {
110
+ show_options = false;
111
+ active_index = null;
112
+ filter_input.blur();
113
+ }
114
+ gradio.props.value = selected_indices.map((index) =>
115
+ typeof index === "number" ? choices_values[index] : index
116
+ );
117
+ }
118
+
119
+ function handle_option_selected(e: any): void {
120
+ const option_index = parseInt(e.detail.target.dataset.index);
121
+ add_or_remove_index(option_index);
122
+ }
123
+
124
+ function add_or_remove_index(option_index: number): void {
125
+ if (selected_indices.includes(option_index)) {
126
+ remove_selected_choice(option_index);
127
+ } else {
128
+ add_selected_choice(option_index);
129
+ }
130
+ input_text = "";
131
+ active_index = null;
132
+ gradio.dispatch("input");
133
+ }
134
+
135
+ function remove_all(e: any): void {
136
+ selected_indices = [];
137
+ input_text = "";
138
+ gradio.props.value = [];
139
+ e.preventDefault();
140
+ }
141
+
142
+ function handle_focus(e: FocusEvent): void {
143
+ filtered_indices = gradio.props.choices.map((_, i) => i);
144
+ if (
145
+ gradio.props.max_choices === null ||
146
+ selected_indices.length < gradio.props.max_choices
147
+ ) {
148
+ show_options = true;
149
+ }
150
+ gradio.dispatch("focus");
151
+ show_options = true;
152
+ }
153
+
154
+ function handle_key_down(e: KeyboardEvent): void {
155
+ [show_options, active_index] = handle_shared_keys(
156
+ e,
157
+ active_index,
158
+ filtered_indices
159
+ );
160
+ if (e.key === "Enter") {
161
+ if (active_index !== null) {
162
+ add_or_remove_index(active_index);
163
+ } else {
164
+ if (gradio.props.allow_custom_value) {
165
+ add_selected_choice(input_text);
166
+ input_text = "";
167
+ }
168
+ }
169
+ }
170
+ if (e.key === "Backspace" && input_text === "") {
171
+ selected_indices = [...selected_indices.slice(0, -1)];
172
+ }
173
+ if (selected_indices.length === gradio.props.max_choices) {
174
+ show_options = false;
175
+ active_index = null;
176
+ }
177
+ }
178
+
179
+ let old_value = $state(gradio.props.value);
180
+
181
+ $effect(() => {
182
+ if (old_value !== gradio.props.value) {
183
+ old_value = gradio.props.value;
184
+ gradio.dispatch("change");
185
+ }
186
+ });
187
+
188
+ function on_custom_button_click(id: number) {
189
+ gradio.dispatch("custom_button_click", { id });
190
+ }
191
+ </script>
192
+
193
+ <label class:container={gradio.shared.container}>
194
+ {#if gradio.shared.show_label && buttons && buttons.length > 0}
195
+ <IconButtonWrapper {buttons} {on_custom_button_click} />
196
+ {/if}
197
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
198
+ >{label}</BlockTitle
199
+ >
200
+
201
+ <div class="wrap">
202
+ <div class="wrap-inner" class:show_options>
203
+ {#each selected_indices as s}
204
+ <div class="token">
205
+ <span>
206
+ {#if typeof s === "number"}
207
+ {choices_names[s]}
208
+ {:else}
209
+ {s}
210
+ {/if}
211
+ </span>
212
+ {#if !disabled}
213
+ <div
214
+ class="token-remove"
215
+ on:click|preventDefault={() => remove_selected_choice(s)}
216
+ on:keydown={(event) => {
217
+ if (event.key === "Enter") {
218
+ remove_selected_choice(s);
219
+ }
220
+ }}
221
+ role="button"
222
+ tabindex="0"
223
+ title={gradio.i18n("common.remove") + " " + s}
224
+ >
225
+ <Remove />
226
+ </div>
227
+ {/if}
228
+ </div>
229
+ {/each}
230
+ <div class="secondary-wrap">
231
+ <input
232
+ class="border-none"
233
+ class:subdued={(!choices_names.includes(input_text) &&
234
+ !gradio.props.allow_custom_value) ||
235
+ selected_indices.length === gradio.props.max_choices}
236
+ {disabled}
237
+ autocomplete="off"
238
+ bind:value={input_text}
239
+ bind:this={filter_input}
240
+ on:keydown={handle_key_down}
241
+ on:keyup={(e) => {
242
+ gradio.dispatch("key_up", {
243
+ key: e.key,
244
+ input_value: input_text
245
+ });
246
+ }}
247
+ on:blur={handle_blur}
248
+ on:focus={handle_focus}
249
+ readonly={!gradio.props.filterable}
250
+ />
251
+
252
+ {#if !disabled}
253
+ {#if selected_indices.length > 0}
254
+ <div
255
+ role="button"
256
+ tabindex="0"
257
+ class="token-remove remove-all"
258
+ title={gradio.i18n("common.clear")}
259
+ on:click={remove_all}
260
+ on:keydown={(event) => {
261
+ if (event.key === "Enter") {
262
+ remove_all(event);
263
+ }
264
+ }}
265
+ >
266
+ <Remove />
267
+ </div>
268
+ {/if}
269
+ <span class="icon-wrap"> <DropdownArrow /></span>
270
+ {/if}
271
+ </div>
272
+ </div>
273
+ <DropdownOptions
274
+ {show_options}
275
+ choices={gradio.props.choices}
276
+ {filtered_indices}
277
+ {disabled}
278
+ {selected_indices}
279
+ {active_index}
280
+ remember_scroll={true}
281
+ on:change={handle_option_selected}
282
+ />
283
+ </div>
284
+ </label>
285
+
286
+ <style>
287
+ .icon-wrap {
288
+ color: var(--body-text-color);
289
+ margin-right: var(--size-2);
290
+ width: var(--size-5);
291
+ }
292
+ label:not(.container),
293
+ label:not(.container) .wrap,
294
+ label:not(.container) .wrap-inner,
295
+ label:not(.container) .secondary-wrap,
296
+ label:not(.container) .token,
297
+ label:not(.container) input {
298
+ height: 100%;
299
+ }
300
+ .container .wrap {
301
+ box-shadow: var(--input-shadow);
302
+ border: var(--input-border-width) solid var(--border-color-primary);
303
+ }
304
+
305
+ .wrap {
306
+ position: relative;
307
+ border-radius: var(--input-radius);
308
+ background: var(--input-background-fill);
309
+ }
310
+
311
+ .wrap:focus-within {
312
+ box-shadow: var(--input-shadow-focus);
313
+ border-color: var(--input-border-color-focus);
314
+ }
315
+
316
+ .wrap-inner {
317
+ display: flex;
318
+ position: relative;
319
+ flex-wrap: wrap;
320
+ align-items: center;
321
+ gap: var(--checkbox-label-gap);
322
+ padding: var(--checkbox-label-padding);
323
+ }
324
+
325
+ .token {
326
+ display: flex;
327
+ align-items: center;
328
+ transition: var(--button-transition);
329
+ cursor: pointer;
330
+ box-shadow: var(--checkbox-label-shadow);
331
+ border: var(--checkbox-label-border-width) solid
332
+ var(--checkbox-label-border-color);
333
+ border-radius: var(--button-small-radius);
334
+ background: var(--checkbox-label-background-fill);
335
+ padding: var(--checkbox-label-padding);
336
+ color: var(--checkbox-label-text-color);
337
+ font-weight: var(--checkbox-label-text-weight);
338
+ font-size: var(--checkbox-label-text-size);
339
+ line-height: var(--line-md);
340
+ word-break: break-word;
341
+ }
342
+
343
+ .token > * + * {
344
+ margin-left: var(--size-2);
345
+ }
346
+
347
+ .token-remove {
348
+ fill: var(--body-text-color);
349
+ display: flex;
350
+ justify-content: center;
351
+ align-items: center;
352
+ cursor: pointer;
353
+ border: var(--checkbox-border-width) solid var(--border-color-primary);
354
+ border-radius: var(--radius-full);
355
+ background: var(--background-fill-primary);
356
+ padding: var(--size-0-5);
357
+ width: 16px;
358
+ height: 16px;
359
+ flex-shrink: 0;
360
+ }
361
+
362
+ .secondary-wrap {
363
+ display: flex;
364
+ flex: 1 1 0%;
365
+ align-items: center;
366
+ border: none;
367
+ min-width: min-content;
368
+ }
369
+
370
+ input {
371
+ margin: var(--spacing-sm);
372
+ outline: none;
373
+ border: none;
374
+ background: inherit;
375
+ width: var(--size-full);
376
+ color: var(--body-text-color);
377
+ font-size: var(--input-text-size);
378
+ }
379
+
380
+ input:disabled {
381
+ -webkit-text-fill-color: var(--body-text-color);
382
+ -webkit-opacity: 1;
383
+ opacity: 1;
384
+ cursor: not-allowed;
385
+ }
386
+
387
+ .remove-all {
388
+ margin-left: var(--size-1);
389
+ width: 20px;
390
+ height: 20px;
391
+ }
392
+ .subdued {
393
+ color: var(--body-text-color-subdued);
394
+ }
395
+ input[readonly] {
396
+ cursor: pointer;
397
+ }
398
+ </style>
6.3.1/dropdown/shared/utils.ts ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function positive_mod(n: number, m: number): number {
2
+ return ((n % m) + m) % m;
3
+ }
4
+
5
+ export function handle_filter(
6
+ choices: [string, string | number][],
7
+ input_text: string
8
+ ): number[] {
9
+ return choices.reduce((filtered_indices, o, index) => {
10
+ if (
11
+ input_text ? o[0].toLowerCase().includes(input_text.toLowerCase()) : true
12
+ ) {
13
+ filtered_indices.push(index);
14
+ }
15
+ return filtered_indices;
16
+ }, [] as number[]);
17
+ }
18
+
19
+ export function handle_change(
20
+ dispatch: any,
21
+ value: string | number | (string | number)[] | undefined,
22
+ value_is_output: boolean
23
+ ): void {
24
+ dispatch("change", value);
25
+ if (!value_is_output) {
26
+ dispatch("input");
27
+ }
28
+ }
29
+
30
+ export function handle_shared_keys(
31
+ e: KeyboardEvent,
32
+ active_index: number | null,
33
+ filtered_indices: number[]
34
+ ): [boolean, number | null] {
35
+ if (e.key === "Escape") {
36
+ return [false, active_index];
37
+ }
38
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
39
+ if (filtered_indices.length > 0) {
40
+ if (active_index === null) {
41
+ active_index =
42
+ e.key === "ArrowDown"
43
+ ? filtered_indices[0]
44
+ : filtered_indices[filtered_indices.length - 1];
45
+ } else {
46
+ const index_in_filtered = filtered_indices.indexOf(active_index);
47
+ const increment = e.key === "ArrowUp" ? -1 : 1;
48
+ active_index =
49
+ filtered_indices[
50
+ positive_mod(index_in_filtered + increment, filtered_indices.length)
51
+ ];
52
+ }
53
+ }
54
+ }
55
+ return [true, active_index];
56
+ }
6.3.1/dropdown/types.ts ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { KeyUpData, SelectData, CustomButton } from "@gradio/utils";
2
+
3
+ export type Item = string | number;
4
+
5
+ export interface DropdownProps {
6
+ multiselect: boolean;
7
+ max_choices: number | null;
8
+ choices: [string, Item][];
9
+ allow_custom_value: boolean;
10
+ value: Item | Item[];
11
+ info: string;
12
+ filterable: boolean;
13
+ buttons: (string | CustomButton)[] | null;
14
+ }
15
+
16
+ export interface DropdownEvents {
17
+ change: never;
18
+ input: never;
19
+ select: SelectData;
20
+ focus: never;
21
+ blur: never;
22
+ key_up: KeyUpData;
23
+ custom_button_click: { id: number };
24
+ }