From f9c2d866e6898d2ec8c62f356a4715f6e34c8c7f Mon Sep 17 00:00:00 2001 From: "N.Alivernini" Date: Wed, 27 Sep 2023 14:35:27 +0200 Subject: [PATCH] fix autocomplete and btn search --- .../search-frontend/src/components/Search.tsx | 20 +++++++++++++------ .../src/components/TokenSelect.tsx | 5 ++++- .../src/components/useSelections.tsx | 10 +++++++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/js-packages/search-frontend/src/components/Search.tsx b/js-packages/search-frontend/src/components/Search.tsx index 9b8bb8cd2..c2eda7054 100644 --- a/js-packages/search-frontend/src/components/Search.tsx +++ b/js-packages/search-frontend/src/components/Search.tsx @@ -69,7 +69,7 @@ export function Search({ const clickAwayRef = React.useRef(null); useClickAway([clickAwayRef], () => setOpenedDropdown(null)); - const [textBtn, setTextBtn] = React.useState(""); + const [textBtn, setTextBtn] = React.useState(); const inputRef = React.useRef(null); const [adjustedSelection, setAdjustedSelection] = React.useState<{ selectionStart: number; @@ -254,7 +254,7 @@ export function Search({ } type="text" placeholder={t("search") || "search..."} - value={btnSearch ? textBtn : selectionsState.text} + value={btnSearch ? textBtn ?? '' : selectionsState.text} onChange={(event) => { if (!btnSearch) { selectionsDispatch({ @@ -461,10 +461,18 @@ export function Search({ cursor: pointer; `} onClick={() => { - selectionsDispatch({ - type: "set-text", - text: textBtn, - }); + if(textBtn === '') { + selectionsDispatch({ + type: "reset-search" + }); + } else { + selectionsDispatch({ + type: "set-text", + text: textBtn, + textOnchange: textBtn, + }); + } + onDetail(null); setOpenedDropdown(null); }} diff --git a/js-packages/search-frontend/src/components/TokenSelect.tsx b/js-packages/search-frontend/src/components/TokenSelect.tsx index 3700fdeed..5d46442a8 100644 --- a/js-packages/search-frontend/src/components/TokenSelect.tsx +++ b/js-packages/search-frontend/src/components/TokenSelect.tsx @@ -24,7 +24,7 @@ type TokenSelectProps = { optionPosition: number; } | null> >; - setTextBtn: React.Dispatch>; + setTextBtn: React.Dispatch>; }; export function TokenSelect({ span, @@ -248,7 +248,10 @@ function getTokenLabel(token: AnalysisToken) { return token.entityName; case "TEXT": return token.value; + case "AUTOCOMPLETE": + return token.value; } + return token.value; } function FactoryTokenType({ diff --git a/js-packages/search-frontend/src/components/useSelections.tsx b/js-packages/search-frontend/src/components/useSelections.tsx index 726f28e23..24c5e3d73 100644 --- a/js-packages/search-frontend/src/components/useSelections.tsx +++ b/js-packages/search-frontend/src/components/useSelections.tsx @@ -65,6 +65,7 @@ const initial: SelectionsState = { export type SelectionsAction = | { type: "set-text"; text?: string; textOnchange?: string } + | { type: "reset-search" } | { type: "set-selection"; replaceText: boolean; @@ -116,12 +117,19 @@ function reducer( text: action.text || state.text || "", textOnChange: action.textOnchange || state.textOnChange || "", selection: shiftSelection( - state.textOnChange || "", + state.textOnChange ?? "", action.textOnchange || state.textOnChange || "", state.selection, ), }; } + case "reset-search" : { + return { + text: '', + textOnChange: '', + selection: [] + } + } case "set-selection": { const { text, selection } = (() => { if (