gradio-pr-bot commited on
Commit
da179b5
·
verified ·
1 Parent(s): 3e70ca3

Upload folder using huggingface_hub

Browse files
6.2.0/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.2.0/dropdown/Index.svelte ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 } 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
+ on_change={() => gradio.dispatch("change")}
50
+ on_input={() => gradio.dispatch("input")}
51
+ on_select={(data) => gradio.dispatch("select", data)}
52
+ on_focus={() => gradio.dispatch("focus")}
53
+ on_blur={() => gradio.dispatch("blur")}
54
+ on_key_up={(data) => gradio.dispatch("key_up", data)}
55
+ />
56
+ {/if}
57
+ </Block>
6.2.0/dropdown/package.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dropdown",
3
+ "version": "0.10.7",
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.2.0/dropdown/shared/Dropdown.svelte ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import DropdownOptions from "./DropdownOptions.svelte";
3
+ import { BlockTitle } from "@gradio/atoms";
4
+ import { DropdownArrow } from "@gradio/icons";
5
+ import { handle_filter, handle_shared_keys } from "./utils";
6
+ import type { SelectData, KeyUpData } from "@gradio/utils";
7
+ import { tick } from "svelte";
8
+
9
+ const is_browser = typeof window !== "undefined";
10
+
11
+ let {
12
+ label = "Dropdown",
13
+ info = undefined,
14
+ value = $bindable<string | number | null>(),
15
+ choices = [],
16
+ interactive = true,
17
+ show_label = true,
18
+ container = true,
19
+ allow_custom_value = false,
20
+ filterable = true,
21
+ on_change,
22
+ on_input,
23
+ on_select,
24
+ on_focus,
25
+ on_blur,
26
+ on_key_up
27
+ }: {
28
+ label?: string;
29
+ info?: string;
30
+ value?: string | number | null;
31
+ choices?: [string, string | number][];
32
+ interactive?: boolean;
33
+ show_label?: boolean;
34
+ container?: boolean;
35
+ allow_custom_value?: boolean;
36
+ filterable?: boolean;
37
+ on_change?: (value: string | number | null) => void;
38
+ on_input?: () => void;
39
+ on_select?: (data: SelectData) => void;
40
+ on_focus?: () => void;
41
+ on_blur?: () => void;
42
+ on_key_up?: (data: KeyUpData) => void;
43
+ } = $props();
44
+
45
+ let filter_input: HTMLElement;
46
+
47
+ let show_options = $derived.by(() => {
48
+ return is_browser && filter_input === document.activeElement;
49
+ });
50
+ let choices_names: string[] = $derived.by(() => {
51
+ return choices.map((c) => c[0]);
52
+ });
53
+ let choices_values: (string | number)[] = $derived.by(() => {
54
+ return choices.map((c) => c[1]);
55
+ });
56
+ let [input_text, selected_index] = $derived.by(() => {
57
+ if (
58
+ value === undefined ||
59
+ value === null ||
60
+ (Array.isArray(value) && value.length === 0)
61
+ ) {
62
+ return ["", null];
63
+ } else if (choices_values.includes(value as string | number)) {
64
+ return [
65
+ choices_names[choices_values.indexOf(value as string | number)],
66
+ choices_values.indexOf(value as string | number)
67
+ ];
68
+ } else if (allow_custom_value) {
69
+ return [value as string, null];
70
+ } else {
71
+ return ["", null];
72
+ }
73
+ });
74
+ let initialized = $state(false);
75
+ let disabled = $derived(!interactive);
76
+
77
+ // All of these are indices with respect to the choices array
78
+ let filtered_indices = $state(choices.map((_, i) => i));
79
+ let active_index: number | null = $state(null);
80
+
81
+ function handle_option_selected(e: any): void {
82
+ selected_index = parseInt(e.detail.target.dataset.index);
83
+ if (isNaN(selected_index)) {
84
+ // This is the case when the user clicks on the scrollbar
85
+ selected_index = null;
86
+ return;
87
+ }
88
+
89
+ let [_input_text, _value] = choices[selected_index];
90
+ input_text = _input_text;
91
+ value = _value;
92
+ on_select?.({
93
+ index: selected_index,
94
+ value: choices_values[selected_index],
95
+ selected: true
96
+ });
97
+ show_options = false;
98
+ active_index = null;
99
+ filter_input.blur();
100
+ }
101
+
102
+ function handle_focus(e: FocusEvent): void {
103
+ filtered_indices = choices.map((_, i) => i);
104
+ show_options = true;
105
+ on_focus?.();
106
+ }
107
+
108
+ function handle_blur(): void {
109
+ if (!allow_custom_value) {
110
+ input_text =
111
+ choices_names[choices_values.indexOf(value as string | number)];
112
+ } else {
113
+ value = input_text;
114
+ }
115
+ show_options = false;
116
+ active_index = null;
117
+ filtered_indices = choices.map((_, i) => i);
118
+ on_blur?.();
119
+ on_input?.();
120
+ }
121
+
122
+ async function handle_key_down(e: KeyboardEvent): Promise<void> {
123
+ await tick();
124
+ filtered_indices = handle_filter(choices, input_text);
125
+ active_index = filtered_indices.length > 0 ? filtered_indices[0] : null;
126
+ [show_options, active_index] = handle_shared_keys(
127
+ e,
128
+ active_index,
129
+ filtered_indices
130
+ );
131
+ if (e.key === "Enter") {
132
+ if (active_index !== null) {
133
+ selected_index = active_index;
134
+ value = choices_values[active_index];
135
+ show_options = false;
136
+ filter_input.blur();
137
+ active_index = null;
138
+ } else if (choices_names.includes(input_text)) {
139
+ selected_index = choices_names.indexOf(input_text);
140
+ value = choices_values[selected_index];
141
+ show_options = false;
142
+ active_index = null;
143
+ filter_input.blur();
144
+ } else if (allow_custom_value) {
145
+ value = input_text;
146
+ selected_index = null;
147
+ show_options = false;
148
+ active_index = null;
149
+ filter_input.blur();
150
+ }
151
+ }
152
+ }
153
+
154
+ let old_value = $state(value);
155
+ $effect(() => {
156
+ if (old_value !== value) {
157
+ old_value = value;
158
+ on_change?.(value);
159
+ }
160
+ });
161
+ </script>
162
+
163
+ <div class:container>
164
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
165
+
166
+ <div class="wrap">
167
+ <div class="wrap-inner" class:show_options>
168
+ <div class="secondary-wrap">
169
+ <input
170
+ role="listbox"
171
+ aria-controls="dropdown-options"
172
+ aria-expanded={show_options}
173
+ aria-label={label}
174
+ class="border-none"
175
+ class:subdued={!choices_names.includes(input_text) &&
176
+ !allow_custom_value}
177
+ autocomplete="off"
178
+ {disabled}
179
+ bind:value={input_text}
180
+ bind:this={filter_input}
181
+ on:keydown={handle_key_down}
182
+ on:keyup={(e) => {
183
+ on_key_up?.({
184
+ key: e.key,
185
+ input_value: input_text
186
+ });
187
+ }}
188
+ on:blur={handle_blur}
189
+ on:focus={handle_focus}
190
+ readonly={!filterable}
191
+ />
192
+ {#if !disabled}
193
+ <div class="icon-wrap">
194
+ <DropdownArrow />
195
+ </div>
196
+ {/if}
197
+ </div>
198
+ </div>
199
+ <DropdownOptions
200
+ {show_options}
201
+ {choices}
202
+ {filtered_indices}
203
+ {disabled}
204
+ selected_indices={selected_index === null ? [] : [selected_index]}
205
+ {active_index}
206
+ on:change={handle_option_selected}
207
+ on:load={() => (initialized = true)}
208
+ />
209
+ </div>
210
+ </div>
211
+
212
+ <style>
213
+ .icon-wrap {
214
+ position: absolute;
215
+ top: 50%;
216
+ transform: translateY(-50%);
217
+ right: var(--size-5);
218
+ color: var(--body-text-color);
219
+ width: var(--size-5);
220
+ pointer-events: none;
221
+ }
222
+ .container {
223
+ height: 100%;
224
+ }
225
+ .container .wrap {
226
+ box-shadow: var(--input-shadow);
227
+ border: var(--input-border-width) solid var(--border-color-primary);
228
+ }
229
+
230
+ .wrap {
231
+ position: relative;
232
+ border-radius: var(--input-radius);
233
+ background: var(--input-background-fill);
234
+ }
235
+
236
+ .wrap:focus-within {
237
+ box-shadow: var(--input-shadow-focus);
238
+ border-color: var(--input-border-color-focus);
239
+ background: var(--input-background-fill-focus);
240
+ }
241
+
242
+ .wrap-inner {
243
+ display: flex;
244
+ position: relative;
245
+ flex-wrap: wrap;
246
+ align-items: center;
247
+ gap: var(--checkbox-label-gap);
248
+ padding: var(--checkbox-label-padding);
249
+ height: 100%;
250
+ }
251
+ .secondary-wrap {
252
+ display: flex;
253
+ flex: 1 1 0%;
254
+ align-items: center;
255
+ border: none;
256
+ min-width: min-content;
257
+ height: 100%;
258
+ }
259
+
260
+ input {
261
+ margin: var(--spacing-sm);
262
+ outline: none;
263
+ border: none;
264
+ background: inherit;
265
+ width: var(--size-full);
266
+ color: var(--body-text-color);
267
+ font-size: var(--input-text-size);
268
+ height: 100%;
269
+ }
270
+
271
+ input:disabled {
272
+ -webkit-text-fill-color: var(--body-text-color);
273
+ -webkit-opacity: 1;
274
+ opacity: 1;
275
+ cursor: not-allowed;
276
+ }
277
+
278
+ .subdued {
279
+ color: var(--body-text-color-subdued);
280
+ }
281
+
282
+ input[readonly] {
283
+ cursor: pointer;
284
+ }
285
+ </style>
6.2.0/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.2.0/dropdown/shared/Multiselect.svelte ADDED
@@ -0,0 +1,390 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { _ } from "svelte-i18n";
3
+ import { BlockTitle } 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
+
18
+ let choices_names: string[] = $derived.by(() => {
19
+ return gradio.props.choices.map((c) => c[0]);
20
+ });
21
+ let choices_values: (string | number)[] = $derived.by(() => {
22
+ return gradio.props.choices.map((c) => c[1]);
23
+ });
24
+
25
+ let disabled = $derived(!gradio.shared.interactive);
26
+
27
+ let show_options = $state(false);
28
+
29
+ // All of these are indices with respect to the choices array
30
+ let [filtered_indices, active_index] = $derived.by(() => {
31
+ const filtered = handle_filter(gradio.props.choices, input_text);
32
+ return [
33
+ filtered,
34
+ filtered.length > 0 && !gradio.props.allow_custom_value
35
+ ? filtered[0]
36
+ : null
37
+ ];
38
+ });
39
+
40
+ function set_selected_indices(): Item[] {
41
+ if (gradio.props.value === undefined) {
42
+ return [];
43
+ } else if (Array.isArray(gradio.props.value)) {
44
+ return gradio.props.value
45
+ .map((v) => {
46
+ const index = choices_values.indexOf(v);
47
+ if (index !== -1) {
48
+ return index;
49
+ }
50
+ if (gradio.props.allow_custom_value) {
51
+ return v;
52
+ }
53
+ // Instead of returning null, skip this iteration
54
+ return undefined;
55
+ })
56
+ .filter((val): val is string | number => val !== undefined);
57
+ }
58
+ return [];
59
+ }
60
+
61
+ let selected_indices: (number | string)[] = $derived.by(set_selected_indices);
62
+
63
+ function handle_blur(): void {
64
+ if (!gradio.props.allow_custom_value) {
65
+ input_text = "";
66
+ }
67
+
68
+ if (gradio.props.allow_custom_value && input_text !== "") {
69
+ add_selected_choice(input_text);
70
+ input_text = "";
71
+ }
72
+ gradio.dispatch("blur");
73
+ show_options = false;
74
+ active_index = null;
75
+ }
76
+
77
+ function remove_selected_choice(option_index: number | string) {
78
+ selected_indices = selected_indices.filter((v) => v !== option_index);
79
+ gradio.props.value = selected_indices.map((index) =>
80
+ typeof index === "number" ? choices_values[index] : index
81
+ );
82
+ gradio.dispatch("input");
83
+ gradio.dispatch("select", {
84
+ index: typeof option_index === "number" ? option_index : -1,
85
+ value:
86
+ typeof option_index === "number"
87
+ ? choices_values[option_index]
88
+ : option_index,
89
+ selected: false
90
+ });
91
+ }
92
+
93
+ function add_selected_choice(option_index: number | string) {
94
+ if (
95
+ gradio.props.max_choices == null ||
96
+ selected_indices.length < gradio.props.max_choices
97
+ ) {
98
+ selected_indices.push(option_index);
99
+ gradio.dispatch("select", {
100
+ index: typeof option_index === "number" ? option_index : -1,
101
+ value:
102
+ typeof option_index === "number"
103
+ ? choices_values[option_index]
104
+ : option_index,
105
+ selected: true
106
+ });
107
+ }
108
+ if (selected_indices.length === gradio.props.max_choices) {
109
+ show_options = false;
110
+ active_index = null;
111
+ filter_input.blur();
112
+ }
113
+ gradio.props.value = selected_indices.map((index) =>
114
+ typeof index === "number" ? choices_values[index] : index
115
+ );
116
+ }
117
+
118
+ function handle_option_selected(e: any): void {
119
+ const option_index = parseInt(e.detail.target.dataset.index);
120
+ add_or_remove_index(option_index);
121
+ }
122
+
123
+ function add_or_remove_index(option_index: number): void {
124
+ if (selected_indices.includes(option_index)) {
125
+ remove_selected_choice(option_index);
126
+ } else {
127
+ add_selected_choice(option_index);
128
+ }
129
+ input_text = "";
130
+ active_index = null;
131
+ gradio.dispatch("input");
132
+ }
133
+
134
+ function remove_all(e: any): void {
135
+ selected_indices = [];
136
+ input_text = "";
137
+ gradio.props.value = [];
138
+ e.preventDefault();
139
+ }
140
+
141
+ function handle_focus(e: FocusEvent): void {
142
+ filtered_indices = gradio.props.choices.map((_, i) => i);
143
+ if (
144
+ gradio.props.max_choices === null ||
145
+ selected_indices.length < gradio.props.max_choices
146
+ ) {
147
+ show_options = true;
148
+ }
149
+ gradio.dispatch("focus");
150
+ show_options = true;
151
+ }
152
+
153
+ function handle_key_down(e: KeyboardEvent): void {
154
+ [show_options, active_index] = handle_shared_keys(
155
+ e,
156
+ active_index,
157
+ filtered_indices
158
+ );
159
+ if (e.key === "Enter") {
160
+ if (active_index !== null) {
161
+ add_or_remove_index(active_index);
162
+ } else {
163
+ if (gradio.props.allow_custom_value) {
164
+ add_selected_choice(input_text);
165
+ input_text = "";
166
+ }
167
+ }
168
+ }
169
+ if (e.key === "Backspace" && input_text === "") {
170
+ selected_indices = [...selected_indices.slice(0, -1)];
171
+ }
172
+ if (selected_indices.length === gradio.props.max_choices) {
173
+ show_options = false;
174
+ active_index = null;
175
+ }
176
+ }
177
+
178
+ let old_value = $state(gradio.props.value);
179
+
180
+ $effect(() => {
181
+ if (old_value !== gradio.props.value) {
182
+ old_value = gradio.props.value;
183
+ gradio.dispatch("change");
184
+ }
185
+ });
186
+ </script>
187
+
188
+ <label class:container={gradio.shared.container}>
189
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
190
+ >{label}</BlockTitle
191
+ >
192
+
193
+ <div class="wrap">
194
+ <div class="wrap-inner" class:show_options>
195
+ {#each selected_indices as s}
196
+ <div class="token">
197
+ <span>
198
+ {#if typeof s === "number"}
199
+ {choices_names[s]}
200
+ {:else}
201
+ {s}
202
+ {/if}
203
+ </span>
204
+ {#if !disabled}
205
+ <div
206
+ class="token-remove"
207
+ on:click|preventDefault={() => remove_selected_choice(s)}
208
+ on:keydown={(event) => {
209
+ if (event.key === "Enter") {
210
+ remove_selected_choice(s);
211
+ }
212
+ }}
213
+ role="button"
214
+ tabindex="0"
215
+ title={gradio.i18n("common.remove") + " " + s}
216
+ >
217
+ <Remove />
218
+ </div>
219
+ {/if}
220
+ </div>
221
+ {/each}
222
+ <div class="secondary-wrap">
223
+ <input
224
+ class="border-none"
225
+ class:subdued={(!choices_names.includes(input_text) &&
226
+ !gradio.props.allow_custom_value) ||
227
+ selected_indices.length === gradio.props.max_choices}
228
+ {disabled}
229
+ autocomplete="off"
230
+ bind:value={input_text}
231
+ bind:this={filter_input}
232
+ on:keydown={handle_key_down}
233
+ on:keyup={(e) => {
234
+ gradio.dispatch("key_up", {
235
+ key: e.key,
236
+ input_value: input_text
237
+ });
238
+ }}
239
+ on:blur={handle_blur}
240
+ on:focus={handle_focus}
241
+ readonly={!gradio.props.filterable}
242
+ />
243
+
244
+ {#if !disabled}
245
+ {#if selected_indices.length > 0}
246
+ <div
247
+ role="button"
248
+ tabindex="0"
249
+ class="token-remove remove-all"
250
+ title={gradio.i18n("common.clear")}
251
+ on:click={remove_all}
252
+ on:keydown={(event) => {
253
+ if (event.key === "Enter") {
254
+ remove_all(event);
255
+ }
256
+ }}
257
+ >
258
+ <Remove />
259
+ </div>
260
+ {/if}
261
+ <span class="icon-wrap"> <DropdownArrow /></span>
262
+ {/if}
263
+ </div>
264
+ </div>
265
+ <DropdownOptions
266
+ {show_options}
267
+ choices={gradio.props.choices}
268
+ {filtered_indices}
269
+ {disabled}
270
+ {selected_indices}
271
+ {active_index}
272
+ remember_scroll={true}
273
+ on:change={handle_option_selected}
274
+ />
275
+ </div>
276
+ </label>
277
+
278
+ <style>
279
+ .icon-wrap {
280
+ color: var(--body-text-color);
281
+ margin-right: var(--size-2);
282
+ width: var(--size-5);
283
+ }
284
+ label:not(.container),
285
+ label:not(.container) .wrap,
286
+ label:not(.container) .wrap-inner,
287
+ label:not(.container) .secondary-wrap,
288
+ label:not(.container) .token,
289
+ label:not(.container) input {
290
+ height: 100%;
291
+ }
292
+ .container .wrap {
293
+ box-shadow: var(--input-shadow);
294
+ border: var(--input-border-width) solid var(--border-color-primary);
295
+ }
296
+
297
+ .wrap {
298
+ position: relative;
299
+ border-radius: var(--input-radius);
300
+ background: var(--input-background-fill);
301
+ }
302
+
303
+ .wrap:focus-within {
304
+ box-shadow: var(--input-shadow-focus);
305
+ border-color: var(--input-border-color-focus);
306
+ }
307
+
308
+ .wrap-inner {
309
+ display: flex;
310
+ position: relative;
311
+ flex-wrap: wrap;
312
+ align-items: center;
313
+ gap: var(--checkbox-label-gap);
314
+ padding: var(--checkbox-label-padding);
315
+ }
316
+
317
+ .token {
318
+ display: flex;
319
+ align-items: center;
320
+ transition: var(--button-transition);
321
+ cursor: pointer;
322
+ box-shadow: var(--checkbox-label-shadow);
323
+ border: var(--checkbox-label-border-width) solid
324
+ var(--checkbox-label-border-color);
325
+ border-radius: var(--button-small-radius);
326
+ background: var(--checkbox-label-background-fill);
327
+ padding: var(--checkbox-label-padding);
328
+ color: var(--checkbox-label-text-color);
329
+ font-weight: var(--checkbox-label-text-weight);
330
+ font-size: var(--checkbox-label-text-size);
331
+ line-height: var(--line-md);
332
+ word-break: break-word;
333
+ }
334
+
335
+ .token > * + * {
336
+ margin-left: var(--size-2);
337
+ }
338
+
339
+ .token-remove {
340
+ fill: var(--body-text-color);
341
+ display: flex;
342
+ justify-content: center;
343
+ align-items: center;
344
+ cursor: pointer;
345
+ border: var(--checkbox-border-width) solid var(--border-color-primary);
346
+ border-radius: var(--radius-full);
347
+ background: var(--background-fill-primary);
348
+ padding: var(--size-0-5);
349
+ width: 16px;
350
+ height: 16px;
351
+ flex-shrink: 0;
352
+ }
353
+
354
+ .secondary-wrap {
355
+ display: flex;
356
+ flex: 1 1 0%;
357
+ align-items: center;
358
+ border: none;
359
+ min-width: min-content;
360
+ }
361
+
362
+ input {
363
+ margin: var(--spacing-sm);
364
+ outline: none;
365
+ border: none;
366
+ background: inherit;
367
+ width: var(--size-full);
368
+ color: var(--body-text-color);
369
+ font-size: var(--input-text-size);
370
+ }
371
+
372
+ input:disabled {
373
+ -webkit-text-fill-color: var(--body-text-color);
374
+ -webkit-opacity: 1;
375
+ opacity: 1;
376
+ cursor: not-allowed;
377
+ }
378
+
379
+ .remove-all {
380
+ margin-left: var(--size-1);
381
+ width: 20px;
382
+ height: 20px;
383
+ }
384
+ .subdued {
385
+ color: var(--body-text-color-subdued);
386
+ }
387
+ input[readonly] {
388
+ cursor: pointer;
389
+ }
390
+ </style>
6.2.0/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.2.0/dropdown/types.ts ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { KeyUpData, SelectData } 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
+ }
14
+
15
+ export interface DropdownEvents {
16
+ change: never;
17
+ input: never;
18
+ select: SelectData;
19
+ focus: never;
20
+ blur: never;
21
+ key_up: KeyUpData;
22
+ }