mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-12 10:34:32 +08:00
ddc02268f7
* dropdown * changes * add changeset * refactor * cleanup * dropdown * more refactoring * fixes * simplify, docstring * restore active_index * split into two files * new files * simplify * single select dropdown working * single select dropdown almost working * dropdown * multiselect * multiselect wip * multiselect * multiselect * multiselect * interactive working * dropdown * lint * add changeset * type * typing * fix multiselect static * dropdown * stories and tests * split stories * lint * add changeset * revert * add changeset * fix x * dropdown * lint, test * backend fixes * lint * fix tests * lint * fix final test * clean * review fixes * dropdown * lint * lint * changes * typing * fixes * active index null bug --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
function positive_mod(n: number, m: number): number {
|
|
return ((n % m) + m) % m;
|
|
}
|
|
|
|
export function handle_filter(
|
|
choices: [string, string | number][],
|
|
input_text: string
|
|
): number[] {
|
|
return choices.reduce((filtered_indices, o, index) => {
|
|
if (
|
|
input_text ? o[0].toLowerCase().includes(input_text.toLowerCase()) : true
|
|
) {
|
|
filtered_indices.push(index);
|
|
}
|
|
return filtered_indices;
|
|
}, [] as number[]);
|
|
}
|
|
|
|
export function handle_change(
|
|
dispatch: any,
|
|
value: string | number | (string | number)[] | undefined,
|
|
value_is_output: boolean
|
|
): void {
|
|
dispatch("change", value);
|
|
if (!value_is_output) {
|
|
dispatch("input");
|
|
}
|
|
}
|
|
|
|
export function handle_shared_keys(
|
|
e: KeyboardEvent,
|
|
active_index: number | null,
|
|
filtered_indices: number[]
|
|
): [boolean, number | null] {
|
|
if (e.key === "Escape") {
|
|
return [false, active_index];
|
|
}
|
|
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
if (filtered_indices.length >= 0) {
|
|
if (active_index === null) {
|
|
active_index =
|
|
e.key === "ArrowDown"
|
|
? filtered_indices[0]
|
|
: filtered_indices[filtered_indices.length - 1];
|
|
} else {
|
|
const index_in_filtered = filtered_indices.indexOf(active_index);
|
|
const increment = e.key === "ArrowUp" ? -1 : 1;
|
|
active_index =
|
|
filtered_indices[
|
|
positive_mod(index_in_filtered + increment, filtered_indices.length)
|
|
];
|
|
}
|
|
}
|
|
}
|
|
return [true, active_index];
|
|
}
|