-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce numeric search field in table filters
- Loading branch information
1 parent
0a200a4
commit a0c65b5
Showing
6 changed files
with
154 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useState } from "react"; | ||
|
||
import { Select } from "components/input"; | ||
|
||
type Matcher = { | ||
label: string; | ||
value: string; | ||
}; | ||
|
||
const matchers: Matcher[] = [ | ||
{ label: t("less than"), value: "<" }, | ||
{ label: t("less than or equal to"), value: "<=" }, | ||
{ label: t("equal to"), value: "=" }, | ||
{ label: t("greater than or equal to"), value: ">=" }, | ||
{ label: t("greater than"), value: ">" }, | ||
{ label: t("not equal to"), value: "!=" }, | ||
]; | ||
|
||
const resultingCriteria = (matcher: string, value: string) => { | ||
return matcher && value && value.trim() !== "" ? matcher + value : null; | ||
}; | ||
|
||
export const NumericSearchField = ({ name, criteria, onSearch }) => { | ||
let criteriaMatcher = ""; | ||
let criteriaValue = ""; | ||
if (criteria) { | ||
criteriaMatcher = matchers.find((it) => criteria.startsWith(it.value))?.value ?? "="; | ||
criteriaValue = criteria.includes(criteriaMatcher) ? criteria.split(criteriaMatcher)[1] : criteria; | ||
} | ||
|
||
const [matcher, setMatcher] = useState<string>(criteriaMatcher); | ||
const [value, setValue] = useState<string>(criteriaValue); | ||
|
||
const handleMatcherChange = (selectedMatcher: string) => { | ||
setMatcher(selectedMatcher); | ||
onSearch(resultingCriteria(selectedMatcher, value)); | ||
}; | ||
const handleValueChange = (newValue: string) => { | ||
setValue(newValue); | ||
onSearch(resultingCriteria(matcher, newValue)); | ||
}; | ||
return ( | ||
<div className="row"> | ||
<div className="col-sm-6"> | ||
<Select | ||
name="matcher" | ||
placeholder={t("Matcher")} | ||
defaultValue={matcher} | ||
options={matchers} | ||
onChange={(_name: string | undefined, value: string) => handleMatcherChange(value)} | ||
/> | ||
</div> | ||
|
||
<div className="col"> | ||
<input | ||
className="form-control" | ||
value={value || ""} | ||
type="number" | ||
onChange={(e) => handleValueChange(e.target.value)} | ||
name={name} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
web/spacewalk-web.changes.welder.numeric-search-fields-systems-list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Introduce numeric search field in table filters |