Skip to content

Commit

Permalink
Improve props destructuring and fix invalid value selected when chang…
Browse files Browse the repository at this point in the history
…ing field
  • Loading branch information
wweellddeerr committed Aug 1, 2023
1 parent 633ef80 commit e4793d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
17 changes: 11 additions & 6 deletions web/html/src/components/table/SelectSearchField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ import { Select } from "components/input";

const ALL_OPTION = { value: "ALL", label: t("All") };

export const SelectSearchField = (props) => {
const [searchValue, setSearchValue] = useState(props.criteria || "");
export const SelectSearchField = ({ label, criteria, options, onSearch }) => {
const [searchValue, setSearchValue] = useState(criteria || "");

const handleSearchValueChange = (value) => {
setSearchValue(value);
props.onSearch?.(value === ALL_OPTION.value ? "" : value);
onSearch?.(value === ALL_OPTION.value ? "" : value);
};

const options = [ALL_OPTION].concat(props.options);
const allOptions = [ALL_OPTION].concat(options);

// Avoid invalid value selected when changing field.
if (!allOptions.some((it) => it.value === searchValue)) {
handleSearchValueChange(ALL_OPTION.value);
}

return (
<Select
name="selectSearchField"
placeholder={props.label}
placeholder={label}
defaultValue={searchValue}
options={options}
options={allOptions}
onChange={(_name, value) => handleSearchValueChange(value)}
/>
);
Expand Down
24 changes: 15 additions & 9 deletions web/html/src/components/table/TableFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ import { Select } from "components/input";
import { Form } from "components/input/Form";
import { SelectSearchField } from "components/table/SelectSearchField";

const renderSearchField = (props) => {
const { field } = props;
const selectedOption = props.filterOptions.find((it) => it.value === field);
const renderSearchField = ({ filterOptions, field, criteria, onSearch, placeholder, name }) => {
const selectedOption = filterOptions.find((it) => it.value === field);
if (selectedOption?.filterOptions) {
return <SelectSearchField label={selectedOption.label} options={selectedOption.filterOptions} {...props} />;
return (
<SelectSearchField
label={selectedOption.label}
options={selectedOption.filterOptions}
criteria={criteria}
onSearch={onSearch}
/>
);
}
return (
<div className="form-group">
<input
className="form-control"
value={props.criteria || ""}
placeholder={props.placeholder}
value={criteria || ""}
placeholder={placeholder}
type="text"
onChange={(e) => props.onSearch?.(e.target.value)}
name={props.name}
onChange={(e) => onSearch?.(e.target.value)}
name={name}
/>
</div>
);
};

export const TableFilter = (props) => {
// Dummy model and onChange to reuse the Select component as it requires a Form
let model = {};
const model = {};
const onChange = () => {};

const [selectedFilter, setSelectedFilter] = useState(props.field || "");
Expand Down

0 comments on commit e4793d0

Please sign in to comment.