Skip to content
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

Fix to UniversalSelect Component #1818

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions client/src/components/Admin/RuleViewContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ const RuleViewContainer = props => {
const [ruleId, setRuleId] = useState(rules[0].id);
const rule = rules.filter(rule => rule.id === ruleId)[0];
const classes = useStyles();

const handleSetRule = e => {
const ruleId = Number(e.target.value);
setRuleId(ruleId);
};

return (
<React.Fragment>
<UniversalSelect
className={classes.select}
options={rules.map(rule => ({
value: rule.id,
value: rule.id.toString(),
label: rule.code
}))}
defaultValue={
rules.length > 0 ? { value: rules[0].id, label: rules[0].code } : null
}
onChange={selectedOption => setRuleId(selectedOption.value)}
value={ruleId}
onChange={handleSetRule}
name={"Rule"}
/>

<RuleView rule={rule} rules={rules} setRuleId={setRuleId} />
Expand All @@ -37,8 +42,8 @@ RuleViewContainer.propTypes = {
rules: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
filter: PropTypes.string.isRequired,
length: PropTypes.number.isRequired,
// filter: PropTypes.string.isRequired,
// length: PropTypes.number.isRequired,
code: PropTypes.string.isRequired
})
)
Expand Down
24 changes: 4 additions & 20 deletions client/src/components/ProjectWizard/RuleStrategy/RuleStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createUseStyles, useTheme } from "react-jss";
import clsx from "clsx";
import AccordionToolTip from "../../ToolTip/AccordionToolTip";
import RuleStrategyLabel from "./RuleStrategyLabel";
// import UniversalSelect from "../../UI/UniversalSelect";
import UniversalSelect from "../../UI/UniversalSelect";
import AffordableEdgeCaseModal from "../AffordableEdgeCaseModal";

const useStyles = createUseStyles(theme => ({
Expand Down Expand Up @@ -284,11 +284,10 @@ const RuleStrategy = ({
setShowDescription={setShowDescription}
/>
<div className={classes.choiceSelectContainer}>
{/* <UniversalSelect
<UniversalSelect
autoFocus={autoFocus}
className={classes.select}
value={value}
defaultValue={"0"}
value={value || "0"}
onChange={onInputChangeIfAllowed}
name={code}
id={code}
Expand All @@ -297,22 +296,7 @@ const RuleStrategy = ({
value: choice.id,
label: choice.name
}))}
/> */}
<select
autoFocus={autoFocus}
className={classes.select}
value={value || ""}
onChange={onInputChangeIfAllowed}
name={code}
id={code}
disabled={!display || !!readOnly}
>
{choices.map(choice => (
<option key={choice.id} value={choice.id}>
{choice.name}
</option>
))}
</select>
/>
</div>
{possibleAndEarnedPointsContainers()}
</div>
Expand Down
22 changes: 8 additions & 14 deletions client/src/components/Projects/FilterDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ const FilterPopup = ({
});
};

const handleChange = (e, propertyName) => {
setCriteria({ ...criteria, [propertyName]: e.target.value });
const handleChange = e => {
setCriteria({ ...criteria, [e.target.name]: e.target.value });

// reset any checked project rows when filter is applied
setCheckedProjectIds([]);
Expand Down Expand Up @@ -125,33 +125,27 @@ const FilterPopup = ({
</div>

<UniversalSelect
defaultValue={projectTypeoptions.find(
option => option.value === criteria.type
)}
value={criteria.type}
options={projectTypeoptions}
onChange={e => handleChange(e, "type")}
onChange={handleChange}
name="type"
className={classes.dropdown}
/>

<h4 className={classes.minorHeading}>Status</h4>
<UniversalSelect
defaultValue={statusOptions.find(
option => option.value === criteria.status
)}
value={criteria.status}
options={statusOptions}
onChange={e => handleChange(e, "status")}
onChange={handleChange}
name="status"
className={classes.dropdown}
/>

<h4 className={classes.minorHeading}>Visibility</h4>
<UniversalSelect
defaultValue={visibilityOptions.find(
option => option.value === criteria.visibility
)}
value={criteria.visibility}
options={visibilityOptions}
onChange={e => handleChange(e, "visibility")}
onChange={handleChange}
name="visibility"
className={classes.dropdown}
/>
Expand Down
4 changes: 1 addition & 3 deletions client/src/components/Projects/ProjectsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,7 @@ const ProjectsPage = ({ contentContainerRef }) => {
maxNumOfVisiblePages={5}
/>
<UniversalSelect
defaultValue={perPageOptions.find(
option => option.value === perPage
)}
value={perPage}
options={perPageOptions}
onChange={e => handlePerPageChange(e.target.value)}
name="perPage"
Expand Down
7 changes: 6 additions & 1 deletion client/src/components/UI/UniversalSelect.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default function UniversalSelect({
autoFocus,
className
}) {
// To make UniversalSelect compatible with a standard react <select> component, the onChange method should be passed
// an object that at least looks like an event object.
const handleSelectChange = selectedOption => {
onChange({
target: {
Expand All @@ -39,12 +41,15 @@ export default function UniversalSelect({
}
});
};

return (
<Select
className={classNames(className)}
autoFocus={autoFocus}
onChange={handleSelectChange}
value={value}
// To make value compatible with a standard react <select> component, value should be passed the value of
// the selected option.
value={options.find(o => o.value == value) || ""}
defaultValue={defaultValue}
name={name}
inputId={id}
Expand Down
Loading