From 1337b21789379e7657b4c4f6735e3ef0bae926f0 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 9 Oct 2023 09:12:37 +0200 Subject: [PATCH] #685 - Fix when using non-string values in the tag picker --- CHANGELOG.md | 12 ++++++++++ src/panelWebView/components/TagPicker.tsx | 29 ++++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b4da5b1..e2d58bb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Change Log +## [9.4.0] - 2023-xx-xx + +### ✨ New features + +### 🎨 Enhancements + +### ⚡️ Optimizations + +### 🐞 Fixes + +- [#685](https://github.com/estruyf/vscode-front-matter/issues/685): Fix when using non-string values in the tag picker + ## [9.3.0] - 2023-10-06 - [Release notes](https://beta.frontmatter.codes/updates/v9.3.0) ### ✨ New features diff --git a/src/panelWebView/components/TagPicker.tsx b/src/panelWebView/components/TagPicker.tsx index 8eec3679..7da81001 100644 --- a/src/panelWebView/components/TagPicker.tsx +++ b/src/panelWebView/components/TagPicker.tsx @@ -145,7 +145,10 @@ const TagPicker: React.FunctionComponent = ({ if (selectedItem) { let value = selectedItem || ''; - const item = options.find((o) => o?.toLowerCase() === selectedItem?.toLowerCase()); + const item = options.find((o) => { + o = typeof o === 'string' ? o : `${o}`; + return o?.toLowerCase() === value?.toLowerCase(); + }); if (item) { value = item; } @@ -174,8 +177,12 @@ const TagPicker: React.FunctionComponent = ({ * @param inputValue */ const filterList = (option: string, inputValue: string | null) => { + if (typeof option !== 'string') { + return false; + } + return ( - !selected.includes(option) && option.toLowerCase().includes((inputValue || '').toLowerCase()) + option && !selected.includes(option) && option.toLowerCase().includes((inputValue || '').toLowerCase()) ); }; @@ -201,7 +208,10 @@ const TagPicker: React.FunctionComponent = ({ for (let crntValue of values) { crntValue = crntValue.trim(); if (crntValue) { - const item = options.find((o) => o?.toLowerCase() === crntValue?.toLowerCase()); + const item = options.find((o) => { + o = typeof o === 'string' ? o : `${o}`; + return o?.toLowerCase() === crntValue?.toLowerCase(); + }); if (item) { newValues.push(item); } else if (freeform) { @@ -279,6 +289,15 @@ const TagPicker: React.FunctionComponent = ({ ); }, [settings?.aiEnabled, label, type]); + const sortedSelectedTags = useMemo(() => { + return (selected || []).sort((a: string, b: string) => { + const aString = typeof a === 'string' ? a : `${a}`; + const bString = typeof b === 'string' ? b : `${b}`; + + return aString?.toLowerCase() < bString?.toLowerCase() ? -1 : 1; + }); + }, [selected]); + useEffect(() => { setTimeout(() => { triggerFocus(); @@ -400,9 +419,7 @@ const TagPicker: React.FunctionComponent = ({ /> - a?.toLowerCase() < b?.toLowerCase() ? -1 : 1 - )} + values={sortedSelectedTags} onRemove={onRemove} onCreate={onCreate} options={options}