-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fiction list filter (PP-1110) #106
Changes from 4 commits
9599f2c
2aa2eb4
078c364
c4fb0be
0e696bc
1645480
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,14 @@ export default function AdvancedSearchFilterInput({ | |
|
||
const handleKeyChange = (value) => { | ||
setFilterKey(value); | ||
const selected = fields.find((field) => field.value === value); | ||
|
||
// Set the value to either the default option, or blank when changing the filter type | ||
if (selected.options && selected.options.length) { | ||
setFilterValue(selected.options[0]); | ||
} else { | ||
setFilterValue(""); | ||
} | ||
}; | ||
|
||
const handleOpChange = () => { | ||
|
@@ -64,6 +72,17 @@ export default function AdvancedSearchFilterInput({ | |
}; | ||
|
||
const selectedField = fields.find((field) => field.value === filterKey); | ||
const selectedFieldOperators = | ||
selectedField.operators !== undefined | ||
? operators.filter((element) => { | ||
return selectedField.operators.includes(element.value); | ||
}) | ||
: operators; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: This is fine and will work; but we could simplify it. Since there's only one statement in the filter function, we can drop the braces and the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That it much more readable, thanks! |
||
|
||
const selectedFieldOptions = selectedField.options ?? []; | ||
const selectedFieldElementType = selectedFieldOptions.length | ||
? "select" | ||
: "input"; | ||
|
||
return ( | ||
<form className="advanced-search-filter-input"> | ||
|
@@ -89,8 +108,9 @@ export default function AdvancedSearchFilterInput({ | |
onChange={handleOpChange} | ||
ref={opSelect} | ||
value={filterOp} | ||
className="filter-operator" | ||
> | ||
{operators.map(({ value, label }) => ( | ||
{selectedFieldOperators.map(({ value, label }) => ( | ||
<option | ||
aria-selected={value === filterOp} | ||
key={value} | ||
|
@@ -104,14 +124,23 @@ export default function AdvancedSearchFilterInput({ | |
<EditableInput | ||
aria-label="filter value" | ||
description={selectedField.helpText} | ||
elementType="input" | ||
elementType={selectedFieldElementType} | ||
type="text" | ||
optionalText={false} | ||
placeholder={selectedField.placeholder} | ||
ref={valueInput} | ||
value={filterValue} | ||
onChange={handleValueChange} | ||
/> | ||
className="filter-value" | ||
> | ||
{selectedFieldOptions.length === 0 | ||
? null | ||
: selectedFieldOptions.map((value) => ( | ||
<option key={value} value={value}> | ||
{value} | ||
</option> | ||
))} | ||
</EditableInput> | ||
|
||
<Button | ||
className="inverted inline" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There could be a problem here. This PR is introducing the potential for a reduced set of operators, so we'll also need to ensure that we've got a valid operator for the newly current field. Ideally, we'd only change the
filterOp
if the current one is not supported.It would be useful to add a test simulating going from an op on a field with broader set ops to a field that doesn't support that op, then adding the filter, and finally verifying that the key, value, and op are as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Thank you, totally missed this one.