Skip to content

Commit

Permalink
feat: multi-select fixed can allow unknown values
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Jan 2, 2024
1 parent fa9da3d commit dcefe7c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/FormModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { log_error, log_notice } from "./utils/Log";
import { FieldValue, FormEngine, makeFormEngine } from "./store/formStore";
import { Writable } from "svelte/store";
import { FolderSuggest } from "./suggesters/suggestFolder";
import { allowsUnknownValues } from "./core/InputDefinitionSchema";

export type SubmitFn = (formResult: FormResult) => void;

Expand Down Expand Up @@ -162,8 +163,7 @@ export class FormModal extends Modal {
});
case "multiselect": {
const source = fieldInput.source;
const allowUnknownValues =
source === "dataview" ? fieldInput.allowUnknownValues : false;
const allowUnknownValues = allowsUnknownValues(fieldInput);
const options =
source == "fixed"
? fieldInput.multi_select_options
Expand Down
15 changes: 14 additions & 1 deletion src/core/InputDefinitionSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { trySchemas, ParsingFn, parseC } from "@std";
import { AllFieldTypes } from "./formDefinition";
import { AllFieldTypes, AllSources } from "./formDefinition";
import {
object,
number,
Expand Down Expand Up @@ -88,6 +88,7 @@ const MultiSelectFixedSchema = object({
type: literal("multiselect"),
source: literal("fixed"),
multi_select_options: array(string()),
allowUnknownValues: optional(boolean(), false),
});
const MultiSelectQuerySchema = object({
type: literal("multiselect"),
Expand All @@ -96,6 +97,18 @@ const MultiSelectQuerySchema = object({
allowUnknownValues: optional(boolean(), false),
});

export function canAllowUnknownValues(
type: "multiselect",
source: AllSources,
): source is "dataview" | "fixed" {
return type === "multiselect" && (source === "dataview" || source === "fixed");
}

export function allowsUnknownValues(input: multiselect): boolean {
if (input.source === "notes") return false;
return input.allowUnknownValues;
}

export const MultiselectSchema = union([
MultiSelectNotesSchema,
MultiSelectFixedSchema,
Expand Down
1 change: 1 addition & 0 deletions src/exampleModalDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const exampleModalDefinition: FormDefinition = {
input: {
type: "multiselect",
source: "fixed",
allowUnknownValues: false,
multi_select_options: [
"Android",
"iOS",
Expand Down
5 changes: 3 additions & 2 deletions src/views/components/InputBuilderSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import InputFolder from "./InputFolder.svelte";
import { AllSources } from "src/core/formDefinition";
import InputBuilderDataview from "./inputBuilderDataview.svelte";
import { canAllowUnknownValues } from "src/core/InputDefinitionSchema";
export let index: number;
export let source: AllSources = "fixed";
Expand All @@ -22,7 +23,7 @@
export let is_multi: boolean;
$: id = `builder_select_${index}`;
$: options_id = `builder_select_options_btn_${index}`;
$: canAllowUnknownValues = source === "dataview" && is_multi;
$: showAllowUnknownValuesOption = is_multi && canAllowUnknownValues("multiselect", source);
function moveOption(from: number, direction: "up" | "down") {
const to = direction === "up" ? from - 1 : from + 1;
Expand All @@ -46,7 +47,7 @@
<option value="dataview">Dataview</option>
{/if}
</select>
{#if canAllowUnknownValues}
{#if showAllowUnknownValuesOption}
<label class="unknown-checkbox">
<span>
<input type="checkbox" bind:checked={allowUnknownValues} />
Expand Down

0 comments on commit dcefe7c

Please sign in to comment.