Skip to content

Commit

Permalink
refactor: reducing duplication in select/multiselect
Browse files Browse the repository at this point in the history
  • Loading branch information
theotheo authored and danielo515 committed Oct 21, 2023
1 parent 195ade1 commit b1087e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 75 deletions.
82 changes: 10 additions & 72 deletions src/views/FormBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -230,80 +230,18 @@
bind:options={field.input.options}
bind:folder={field.input.folder}
notifyChange={onChange}
is_multi={false}
/>
{:else if field.input.type === "multiselect"}
{@const source_id = `source_${index}`}
<div class="flex column gap1">
<label for={source_id}>Source</label>
<select
bind:value={field.input.source}
id={source_id}
>
<option value="fixed">Static</option>
<option value="notes">Notes</option>
</select>
</div>
{#if field.input.source === "fixed"}
{@const options_id = `options_btn_${index}`}
<FormRow label="Options" id={options_id}>
<button
type="button"
on:click={() => {
field.input.multi_select_options =
field.input
.multi_select_options || [];
field.input.multi_select_options = [
...field.input
.multi_select_options,
"",
];
onChange();
}}>Add more options</button
>
{#each field.input.multi_select_options || [] as option, idx}
{@const value_id = `${options_id}_option_${idx}`}
<div class="flex row gap1">
<FormRow
label="Value"
id={value_id}
>
<input
type="text"
placeholder="Value"
bind:value={option}
id={value_id}
/></FormRow
>

<FormRow
label="Delete"
id={"button" + value_id}
hideLabel={true}
>
<button
id={"button" + value_id}
use:setIcon={"trash"}
type="button"
on:click={() => {
field.input.options =
field.input.options?.filter(
(_, i) =>
i !== idx
);
onChange();
}}
/></FormRow
>
</div>
{/each}
</FormRow>
{:else if field.input.source === "notes"}
<InputFolder
{index}
bind:folder={field.input.folder}
notifyChange={onChange}
/>
{/if}
<InputBuilderSelect
{index}
bind:source={field.input.source}
bind:options={field.input.multi_select_options}
bind:folder={field.input.folder}
notifyChange={onChange}
is_multi={true}
/>

{:else if field.input.type === "slider"}
{@const min_id = `min_${index}`}
{@const max_id = `max_${index}`}
Expand Down
22 changes: 19 additions & 3 deletions src/views/components/InputBuilderSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
export let folder: string | undefined;
export let options: EditableInput["options"] = [];
export let notifyChange: () => void;
export let is_multi: boolean;
$: id = `builder_select_${index}`;
$: options_id = `builder_select_options_btn_${index}`;
</script>
Expand All @@ -29,15 +30,29 @@
<button
type="button"
on:click={() => {
options?.push({ value: "", label: "" });
if (is_multi) {
options?.push("");
} else {
options?.push({ value: "", label: "" });
}
options = options;
notifyChange();
}}>Add more options</button
>
{#each options || [] as option, idx}
{@const value_id = `${options_id}_option_${idx}`}
{@const label_id = `${options_id}_option_label_${idx}`}
<div class="modal-form flex row gap1">
{#if is_multi }
<FormRow label="Value" id={value_id}>
<input
type="text"
placeholder="Value"
bind:value={option}
id={value_id}
/></FormRow
>
{:else}
{@const label_id = `${options_id}_option_label_${idx}`}
<FormRow label="Label" id={label_id}>
<input
type="text"
Expand All @@ -53,7 +68,8 @@
id={value_id}
/></FormRow
>


{/if}
<FormRow
label="Delete"
id={"button" + value_id}
Expand Down

0 comments on commit b1087e9

Please sign in to comment.