gradio-pr-bot commited on
Commit
5cf5496
·
verified ·
1 Parent(s): ffdc102

Upload folder using huggingface_hub

Browse files
6.1.0/dropdown/Index.svelte CHANGED
@@ -36,6 +36,22 @@
36
  {#if gradio.props.multiselect}
37
  <Multiselect {gradio} />
38
  {:else}
39
- <Dropdown {gradio} />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  {/if}
41
  </Block>
 
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.1.0/dropdown/package.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "@gradio/dropdown",
3
- "version": "0.10.6",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "author": "",
 
1
  {
2
  "name": "@gradio/dropdown",
3
+ "version": "0.10.7",
4
  "description": "Gradio UI packages",
5
  "type": "module",
6
  "author": "",
6.1.0/dropdown/shared/Dropdown.svelte CHANGED
@@ -3,16 +3,44 @@
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
 
@@ -20,47 +48,36 @@
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)) {
@@ -69,10 +86,10 @@
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
@@ -83,29 +100,28 @@
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,
@@ -115,18 +131,18 @@
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;
@@ -135,19 +151,17 @@
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>
@@ -159,21 +173,21 @@
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">
@@ -184,7 +198,7 @@
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]}
 
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
 
 
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)) {
 
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
 
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,
 
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;
 
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>
 
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">
 
198
  </div>
199
  <DropdownOptions
200
  {show_options}
201
+ {choices}
202
  {filtered_indices}
203
  {disabled}
204
  selected_indices={selected_index === null ? [] : [selected_index]}