Skip to content

Commit

Permalink
Merge pull request #4971 from gitbutlerapp/make-select-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
estib-vega authored Sep 24, 2024
2 parents 86460fb + 0f7b9f4 commit 598d60f
Showing 1 changed file with 11 additions and 10 deletions.
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

0 comments on commit 598d60f

Please sign in to comment.