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

Restricted negative values/sign in weight and height user input #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions src/components/BmiForm/BmiForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const initialValues = {
height: '',
date: ''
}
const restrictKeyList = ['-', '+'];

const BmiForm = ({ change }) => {
const [state, setState] = useState(initialValues);
Expand All @@ -24,6 +25,11 @@ const BmiForm = ({ change }) => {
});
};

const filterInput = (e, restrictKeyList) => {
let isValidInput = restrictKeyList.some((key) => e.key === key);
if (isValidInput) e.preventDefault();
};

const handleSubmit = () => {
change(state);
setState(initialValues);
Expand All @@ -38,11 +44,12 @@ const BmiForm = ({ change }) => {
id="weight"
name="weight"
type="number"
min="1"
max="999"
min="1"
placeholder="50"
value={state.weight}
onChange={handleChange}
onChange={(e) => handleChange(e)}
onKeyDown={(e) => filterInput(e, restrictKeyList)}
/>
</div>

Expand All @@ -57,6 +64,7 @@ const BmiForm = ({ change }) => {
placeholder="176"
value={state.height}
onChange={handleChange}
onKeyDown={(e) => filterInput(e, restrictKeyList)}
/>
</div>
</div>
Expand All @@ -76,7 +84,7 @@ const BmiForm = ({ change }) => {
};

BmiForm.propTypes = {
change: PropTypes.func.isRequired
change: PropTypes.func.isRequired,
};

export default BmiForm;