freddyaboulton HF Staff commited on
Commit
86941c6
·
verified ·
1 Parent(s): 899a8e2

Upload folder using huggingface_hub

Browse files
6.0.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.0.0/dropdown/Index.svelte ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 {gradio} />
40
+ {/if}
41
+ </Block>
6.0.0/dropdown/package.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dropdown",
3
+ "version": "0.10.6",
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.0.0/dropdown/shared/Dropdown.svelte ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { Gradio } from "@gradio/utils";
7
+ import type { DropdownEvents, DropdownProps, Item } from "../types.ts";
8
+ import { tick } from "svelte";
9
+
10
+ const is_browser = typeof window !== "undefined";
11
+
12
+ let props = $props();
13
+
14
+ const gradio: Gradio<DropdownEvents, DropdownProps> = props.gradio;
15
+ let label = $derived(gradio.shared.label || "Dropdown");
16
+
17
+ let filter_input: HTMLElement;
18
+
19
+ let show_options = $derived.by(() => {
20
+ return is_browser && filter_input === document.activeElement;
21
+ });
22
+ let choices_names: string[] = $derived.by(() => {
23
+ return gradio.props.choices.map((c) => c[0]);
24
+ });
25
+ let choices_values: (string | number)[] = $derived.by(() => {
26
+ return gradio.props.choices.map((c) => c[1]);
27
+ });
28
+ let [input_text, selected_index] = $derived.by(() => {
29
+ if (
30
+ gradio.props.value === undefined ||
31
+ (Array.isArray(gradio.props.value) && gradio.props.value.length === 0)
32
+ ) {
33
+ return ["", null];
34
+ } else if (choices_values.includes(gradio.props.value as string)) {
35
+ return [
36
+ choices_names[choices_values.indexOf(gradio.props.value as string)],
37
+ choices_values.indexOf(gradio.props.value as string)
38
+ ];
39
+ } else if (gradio.props.allow_custom_value) {
40
+ return [gradio.props.value as string, null];
41
+ } else {
42
+ return ["", null];
43
+ }
44
+ });
45
+ let initialized = $state(false);
46
+ let disabled = $derived(!gradio.shared.interactive);
47
+
48
+ // All of these are indices with respect to the choices array
49
+ let filtered_indices = $state(gradio.props.choices.map((_, i) => i));
50
+ let active_index: number | null = $state(null);
51
+
52
+ // Setting the initial value of the dropdown
53
+ if (gradio.props.value) {
54
+ selected_index = gradio.props.choices
55
+ .map((c) => c[1])
56
+ .indexOf(gradio.props.value as string);
57
+ if (selected_index === -1) {
58
+ selected_index = null;
59
+ } else {
60
+ input_text = gradio.props.choices[selected_index][0];
61
+ }
62
+ }
63
+
64
+ function handle_option_selected(e: any): void {
65
+ selected_index = parseInt(e.detail.target.dataset.index);
66
+ if (isNaN(selected_index)) {
67
+ // This is the case when the user clicks on the scrollbar
68
+ selected_index = null;
69
+ return;
70
+ }
71
+
72
+ let [_input_text, _value] = gradio.props.choices[selected_index];
73
+ input_text = _input_text;
74
+ gradio.props.value = _value;
75
+ gradio.dispatch("select", {
76
+ index: selected_index,
77
+ value: choices_values[selected_index],
78
+ selected: true
79
+ });
80
+ show_options = false;
81
+ active_index = null;
82
+ filter_input.blur();
83
+ }
84
+
85
+ function handle_focus(e: FocusEvent): void {
86
+ filtered_indices = gradio.props.choices.map((_, i) => i);
87
+ show_options = true;
88
+ gradio.dispatch("focus");
89
+ }
90
+
91
+ function handle_blur(): void {
92
+ if (!gradio.props.allow_custom_value) {
93
+ input_text =
94
+ choices_names[choices_values.indexOf(gradio.props.value as string)];
95
+ } else {
96
+ gradio.props.value = input_text;
97
+ }
98
+ show_options = false;
99
+ active_index = null;
100
+ // last_input_text = input_text;
101
+ filtered_indices = gradio.props.choices.map((_, i) => i);
102
+ gradio.dispatch("blur");
103
+ gradio.dispatch("input");
104
+ }
105
+
106
+ async function handle_key_down(e: KeyboardEvent): Promise<void> {
107
+ await tick();
108
+ filtered_indices = handle_filter(gradio.props.choices, input_text);
109
+ active_index = filtered_indices.length > 0 ? filtered_indices[0] : null;
110
+ [show_options, active_index] = handle_shared_keys(
111
+ e,
112
+ active_index,
113
+ filtered_indices
114
+ );
115
+ if (e.key === "Enter") {
116
+ if (active_index !== null) {
117
+ selected_index = active_index;
118
+ gradio.props.value = choices_values[active_index];
119
+ show_options = false;
120
+ filter_input.blur();
121
+ active_index = null;
122
+ } else if (choices_names.includes(input_text)) {
123
+ selected_index = choices_names.indexOf(input_text);
124
+ gradio.props.value = choices_values[selected_index];
125
+ show_options = false;
126
+ active_index = null;
127
+ filter_input.blur();
128
+ } else if (gradio.props.allow_custom_value) {
129
+ gradio.props.value = input_text;
130
+ selected_index = null;
131
+ show_options = false;
132
+ active_index = null;
133
+ filter_input.blur();
134
+ }
135
+ }
136
+ }
137
+
138
+ let old_value = $state(gradio.props.value);
139
+ $effect(() => {
140
+ if (old_value !== gradio.props.value) {
141
+ old_value = gradio.props.value;
142
+ gradio.dispatch("change");
143
+ }
144
+ });
145
+ </script>
146
+
147
+ <div class:container={gradio.shared.container}>
148
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
149
+ >{label}</BlockTitle
150
+ >
151
+
152
+ <div class="wrap">
153
+ <div class="wrap-inner" class:show_options>
154
+ <div class="secondary-wrap">
155
+ <input
156
+ role="listbox"
157
+ aria-controls="dropdown-options"
158
+ aria-expanded={show_options}
159
+ aria-label={label}
160
+ class="border-none"
161
+ class:subdued={!choices_names.includes(input_text) &&
162
+ !gradio.props.allow_custom_value}
163
+ autocomplete="off"
164
+ {disabled}
165
+ bind:value={input_text}
166
+ bind:this={filter_input}
167
+ on:keydown={handle_key_down}
168
+ on:keyup={(e) => {
169
+ gradio.dispatch("key_up", {
170
+ key: e.key,
171
+ input_value: input_text
172
+ });
173
+ }}
174
+ on:blur={handle_blur}
175
+ on:focus={handle_focus}
176
+ readonly={!gradio.props.filterable}
177
+ />
178
+ {#if !disabled}
179
+ <div class="icon-wrap">
180
+ <DropdownArrow />
181
+ </div>
182
+ {/if}
183
+ </div>
184
+ </div>
185
+ <DropdownOptions
186
+ {show_options}
187
+ choices={gradio.props.choices}
188
+ {filtered_indices}
189
+ {disabled}
190
+ selected_indices={selected_index === null ? [] : [selected_index]}
191
+ {active_index}
192
+ on:change={handle_option_selected}
193
+ on:load={() => (initialized = true)}
194
+ />
195
+ </div>
196
+ </div>
197
+
198
+ <style>
199
+ .icon-wrap {
200
+ position: absolute;
201
+ top: 50%;
202
+ transform: translateY(-50%);
203
+ right: var(--size-5);
204
+ color: var(--body-text-color);
205
+ width: var(--size-5);
206
+ pointer-events: none;
207
+ }
208
+ .container {
209
+ height: 100%;
210
+ }
211
+ .container .wrap {
212
+ box-shadow: var(--input-shadow);
213
+ border: var(--input-border-width) solid var(--border-color-primary);
214
+ }
215
+
216
+ .wrap {
217
+ position: relative;
218
+ border-radius: var(--input-radius);
219
+ background: var(--input-background-fill);
220
+ }
221
+
222
+ .wrap:focus-within {
223
+ box-shadow: var(--input-shadow-focus);
224
+ border-color: var(--input-border-color-focus);
225
+ background: var(--input-background-fill-focus);
226
+ }
227
+
228
+ .wrap-inner {
229
+ display: flex;
230
+ position: relative;
231
+ flex-wrap: wrap;
232
+ align-items: center;
233
+ gap: var(--checkbox-label-gap);
234
+ padding: var(--checkbox-label-padding);
235
+ height: 100%;
236
+ }
237
+ .secondary-wrap {
238
+ display: flex;
239
+ flex: 1 1 0%;
240
+ align-items: center;
241
+ border: none;
242
+ min-width: min-content;
243
+ height: 100%;
244
+ }
245
+
246
+ input {
247
+ margin: var(--spacing-sm);
248
+ outline: none;
249
+ border: none;
250
+ background: inherit;
251
+ width: var(--size-full);
252
+ color: var(--body-text-color);
253
+ font-size: var(--input-text-size);
254
+ height: 100%;
255
+ }
256
+
257
+ input:disabled {
258
+ -webkit-text-fill-color: var(--body-text-color);
259
+ -webkit-opacity: 1;
260
+ opacity: 1;
261
+ cursor: not-allowed;
262
+ }
263
+
264
+ .subdued {
265
+ color: var(--body-text-color-subdued);
266
+ }
267
+
268
+ input[readonly] {
269
+ cursor: pointer;
270
+ }
271
+ </style>
6.0.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.0.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.0.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.0.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
+ }