Skip to content

Commit

Permalink
fix autocomplete and btn search
Browse files Browse the repository at this point in the history
  • Loading branch information
N.Alivernini committed Sep 27, 2023
1 parent 4a6de1f commit f9c2d86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
20 changes: 14 additions & 6 deletions js-packages/search-frontend/src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function Search({
const clickAwayRef = React.useRef<HTMLDivElement | null>(null);
useClickAway([clickAwayRef], () => setOpenedDropdown(null));

const [textBtn, setTextBtn] = React.useState("");
const [textBtn, setTextBtn] = React.useState<string | undefined>();
const inputRef = React.useRef<HTMLInputElement | null>(null);
const [adjustedSelection, setAdjustedSelection] = React.useState<{
selectionStart: number;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);
}}
Expand Down
5 changes: 4 additions & 1 deletion js-packages/search-frontend/src/components/TokenSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type TokenSelectProps = {
optionPosition: number;
} | null>
>;
setTextBtn: React.Dispatch<React.SetStateAction<string>>;
setTextBtn: React.Dispatch<React.SetStateAction<string | undefined>>;
};
export function TokenSelect({
span,
Expand Down Expand Up @@ -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({
Expand Down
10 changes: 9 additions & 1 deletion js-packages/search-frontend/src/components/useSelections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit f9c2d86

Please sign in to comment.