diff --git a/src/components/AdvancedSearchBuilder.tsx b/src/components/AdvancedSearchBuilder.tsx index 578e47be3..ac5431b4b 100644 --- a/src/components/AdvancedSearchBuilder.tsx +++ b/src/components/AdvancedSearchBuilder.tsx @@ -33,6 +33,12 @@ export const fields = [ { value: "audience", label: "audience" }, { value: "author", label: "author" }, { value: "title", label: "title" }, + { + value: "fiction", + label: "fiction", + options: ["fiction", "nonfiction"], + operators: ["eq"], + }, ]; export const operators = [ diff --git a/src/components/AdvancedSearchFilterInput.tsx b/src/components/AdvancedSearchFilterInput.tsx index c053e4750..126731a1f 100644 --- a/src/components/AdvancedSearchFilterInput.tsx +++ b/src/components/AdvancedSearchFilterInput.tsx @@ -25,6 +25,23 @@ export default function AdvancedSearchFilterInput({ const handleKeyChange = (value) => { setFilterKey(value); + const selected = fields.find((field) => field.value === value); + + // Set the value to either the first option, or blank. + if (selected.options && selected.options.length) { + setFilterValue(selected.options[0]); + } else { + setFilterValue(""); + } + + // Update operator if the filter doesn't support it. + if ( + selected.operators && + selected.operators.length && + !selected.operators.find((op) => op === filterOp) + ) { + setFilterOp(selected.operators[0]); + } }; const handleOpChange = () => { @@ -64,6 +81,14 @@ export default function AdvancedSearchFilterInput({ }; const selectedField = fields.find((field) => field.value === filterKey); + const selectedFieldOperators = selectedField.operators + ? operators.filter((op) => selectedField.operators.includes(op.value)) + : operators; + + const selectedFieldOptions = selectedField.options ?? []; + const selectedFieldElementType = selectedFieldOptions.length + ? "select" + : "input"; return (
@@ -89,8 +114,9 @@ export default function AdvancedSearchFilterInput({ onChange={handleOpChange} ref={opSelect} value={filterOp} + className="filter-operator" > - {operators.map(({ value, label }) => ( + {selectedFieldOperators.map(({ value, label }) => ( + ))} +