Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make the Select component generic #4971

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions apps/desktop/src/lib/select/Select.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts" module>
export type SelectItem = {
export type SelectItem<T extends string = string> = {
label: string;
value: string;
value: T;
selectable?: boolean;
};
</script>

<script lang="ts">
<script lang="ts" generics="T extends string">
import OptionsGroup from './OptionsGroup.svelte';
import SearchItem from './SearchItem.svelte';
import TextBox from '../shared/TextBox.svelte';
Expand All @@ -21,14 +21,14 @@
disabled?: boolean;
loading?: boolean;
wide?: boolean;
options: SelectItem[];
value?: any;
options: SelectItem<T>[];
value?: T;
placeholder?: string;
maxHeight?: number;
searchable?: boolean;
itemSnippet: Snippet<[{ item: any; highlighted: boolean }]>;
itemSnippet: Snippet<[{ item: SelectItem<T>; highlighted: boolean }]>;
children?: Snippet;
onselect?: (value: string) => void;
onselect?: (value: T) => void;
}

const {
Expand Down Expand Up @@ -92,15 +92,16 @@
else openList();
}

function handleSelect(item: { label: string; value: string }) {
function handleSelect(item: SelectItem<T>) {
const value = item.value;
onselect?.(value);
closeList();
}

function handleEnter() {
if (highlightedIndex !== undefined && filteredOptions[highlightedIndex]) {
handleSelect(filteredOptions[highlightedIndex] as SelectItem);
const option = highlightedIndex !== undefined ? filteredOptions[highlightedIndex] : undefined;
if (option) {
handleSelect(option);
}
}

Expand Down