Skip to content

Commit

Permalink
feat: tags input allow any value to be selected, even if it does not …
Browse files Browse the repository at this point in the history
…exist yet
  • Loading branch information
danielo515 committed Jan 2, 2024
1 parent 0e7e24b commit 1834bf5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/FormModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export class FormModal extends Modal {
setting: fieldBase,
errors: fieldStore.errors,
app: this.app,
allowUnknownValues: true,
},
}),
);
Expand Down
19 changes: 15 additions & 4 deletions src/suggesters/MultiSuggest.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { AbstractInputSuggest, App } from 'obsidian'
import { AbstractInputSuggest, App } from "obsidian";

export class MultiSuggest extends AbstractInputSuggest<string> {
content: Set<string>;

constructor(private inputEl: HTMLInputElement, content: Set<string>, private onSelectCb: (value: string) => void, app: App) {
constructor(
private inputEl: HTMLInputElement,
content: Set<string>,
private onSelectCb: (value: string) => void,
app: App,
private allowUnknownValues = false,
) {
super(app, inputEl);
this.content = content;
}

getSuggestions(inputStr: string): string[] {
const lowerCaseInputStr = inputStr.toLocaleLowerCase();
return [...this.content].filter((content) =>
content.toLocaleLowerCase().contains(lowerCaseInputStr)
const candidates =
this.allowUnknownValues && inputStr !== ""
? [...this.content, inputStr]
: Array.from(this.content);
return candidates.filter((content) =>
content.toLocaleLowerCase().contains(lowerCaseInputStr),
);
}

Expand All @@ -20,6 +30,7 @@ export class MultiSuggest extends AbstractInputSuggest<string> {
}

selectSuggestion(content: string, evt: MouseEvent | KeyboardEvent): void {
console.log("selectSuggestion", content);
this.onSelectCb(content);
this.inputEl.value = "";
this.close();
Expand Down
2 changes: 2 additions & 0 deletions src/views/components/MultiSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
export let availableOptions: string[] = [];
export let errors: Readable<string[]>;
export let values: Writable<string[]>;
export let allowUnknownValues: boolean = false;
// We take the setting to make it consistent with the other input components
export let setting: Setting;
export let app: App;
Expand All @@ -27,6 +28,7 @@
values.update((x) => [...x, selected]);
},
app,
allowUnknownValues,
);
}
function removeValue(value: string) {
Expand Down

0 comments on commit 1834bf5

Please sign in to comment.