Skip to content

Commit 422a476

Browse files
committed
refac
1 parent c65b29e commit 422a476

3 files changed

Lines changed: 48 additions & 19 deletions

File tree

src/lib/components/common/ConfirmDialog.svelte

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import { flyAndScale } from '$lib/utils/transitions';
1212
import { marked } from 'marked';
1313
import SensitiveInput from './SensitiveInput.svelte';
14+
import NativeSelect from './NativeSelect.svelte';
1415
1516
export let title = '';
1617
export let message = '';
@@ -24,6 +25,7 @@
2425
export let inputPlaceholder = '';
2526
export let inputValue = '';
2627
export let inputType = '';
28+
export let inputOptions: ({ label?: string; value: string } | string)[] = [];
2729
2830
let _inputValue = inputValue;
2931
@@ -147,6 +149,14 @@
147149
required={true}
148150
/>
149151
</div>
152+
{:else if inputType === 'select' && inputOptions.length}
153+
<NativeSelect
154+
className="w-full mt-2 rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-900 outline-hidden"
155+
bind:value={_inputValue}
156+
options={inputOptions}
157+
placeholder={inputPlaceholder ? inputPlaceholder : $i18n.t('Select an option')}
158+
required
159+
/>
150160
{:else}
151161
<textarea
152162
bind:value={_inputValue}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script lang="ts">
2+
import { createEventDispatcher } from 'svelte';
3+
4+
const dispatch = createEventDispatcher();
5+
6+
export let value = '';
7+
export let options: ({ label?: string; value: string } | string)[] = [];
8+
export let placeholder = '';
9+
export let className = '';
10+
export let required = false;
11+
</script>
12+
13+
<select
14+
class={className}
15+
bind:value
16+
{required}
17+
on:change={() => {
18+
dispatch('change');
19+
}}
20+
>
21+
{#if placeholder}
22+
<option value="" disabled>{placeholder}</option>
23+
{/if}
24+
25+
{#each options as option}
26+
{@const optionValue = typeof option === 'object' && option !== null ? option.value : option}
27+
{@const optionLabel =
28+
typeof option === 'object' && option !== null ? (option.label ?? option.value) : option}
29+
<option value={optionValue} selected={optionValue === value}>{optionLabel}</option>
30+
{/each}
31+
</select>

src/lib/components/common/Valves.svelte

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
import Switch from './Switch.svelte';
1010
import SensitiveInput from './SensitiveInput.svelte';
11+
import NativeSelect from './NativeSelect.svelte';
1112
import MapSelector from './Valves/MapSelector.svelte';
1213
1314
export let valvesSpec = null;
@@ -122,29 +123,16 @@
122123
/>
123124
</div>
124125
{:else if valvesSpec.properties[property]?.input?.type === 'select' && valvesSpec.properties[property]?.input?.options}
125-
<select
126-
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100/30 dark:border-gray-850/30"
126+
<NativeSelect
127+
className="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-hidden border border-gray-100/30 dark:border-gray-850/30"
127128
bind:value={valves[property]}
129+
options={valvesSpec.properties[property].input.options}
130+
placeholder={valvesSpec.properties[property]?.description ??
131+
$i18n.t('Select an option')}
128132
on:change={() => {
129133
dispatch('change');
130134
}}
131-
>
132-
<option value="" disabled
133-
>{valvesSpec.properties[property]?.description ??
134-
$i18n.t('Select an option')}</option
135-
>
136-
{#each valvesSpec.properties[property].input.options as option}
137-
{#if typeof option === 'object' && option !== null}
138-
<option value={option.value} selected={option.value === valves[property]}>
139-
{option.label ?? option.value}
140-
</option>
141-
{:else}
142-
<option value={option} selected={option === valves[property]}>
143-
{option}
144-
</option>
145-
{/if}
146-
{/each}
147-
</select>
135+
/>
148136
{:else if valvesSpec.properties[property]?.input?.type === 'color'}
149137
<div class="flex items-center space-x-2">
150138
<div class="relative size-6">

0 commit comments

Comments
 (0)