forked from OpenStackweb/summit-admin
-
Notifications
You must be signed in to change notification settings - Fork 4
86b883b71 - Add Numeric Validation for Quantities Fields #761
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
Open
niko-exo
wants to merge
8
commits into
master
Choose a base branch
from
feature/add_numeric_validation_for_quantities_fields
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−7
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2cc2d36
chore: add quantity validation helper
niko-exo 972cf65
feat: create mui formik quantity field
niko-exo 34680a3
feat: use mui formik quantity field on forms
niko-exo 302149e
chore: tests
niko-exo 481a8b2
chore: remove unnecessary helper
niko-exo 7eaedac
chore: use positive number validation instead of the other one
niko-exo d1168b7
chore: replace input mode for numeric instead of decimal
niko-exo 913f6be
chore: mark field as required
niko-exo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/components/mui/__tests__/mui-formik-quantity-field.test.js
This file contains hidden or 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,68 @@ | ||
| import React from "react"; | ||
| import { render, screen, act } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { Formik, Form } from "formik"; | ||
| import "@testing-library/jest-dom"; | ||
| import MuiFormikQuantityField from "../formik-inputs/mui-formik-quantity-field"; | ||
|
|
||
| const renderWithFormik = (props, initialValues = { testField: [] }) => | ||
| render( | ||
| <Formik initialValues={initialValues} onSubmit={props.onSubmit}> | ||
| <Form> | ||
| <MuiFormikQuantityField name="testField" {...props} /> | ||
| <button type="submit">submit</button> | ||
| </Form> | ||
| </Formik> | ||
| ); | ||
|
|
||
| describe("MuiFormikQuantityField", () => { | ||
| it("must accept user input", async () => { | ||
| const onSubmit = jest.fn(); | ||
|
|
||
| renderWithFormik({ | ||
| label: "some field", | ||
| onSubmit | ||
| }); | ||
|
|
||
| const field = screen.getByLabelText("some field"); | ||
| expect(field).toBeInTheDocument(); | ||
|
|
||
| const submitButton = screen.getByText("submit"); | ||
| await act(async () => { | ||
| await userEvent.type(field, "12345"); | ||
| await userEvent.click(submitButton); | ||
| }); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| testField: 12345 | ||
| }), | ||
| expect.anything() | ||
| ); | ||
| }); | ||
|
|
||
| it("must filter invalid characters", async () => { | ||
| const onSubmit = jest.fn(); | ||
|
|
||
| renderWithFormik({ | ||
| label: "some field", | ||
| onSubmit | ||
| }); | ||
|
|
||
| const field = screen.getByLabelText("some field"); | ||
| expect(field).toBeInTheDocument(); | ||
|
|
||
| const submitButton = screen.getByText("submit"); | ||
| await act(async () => { | ||
| await userEvent.type(field, "123eEe45"); | ||
| await userEvent.click(submitButton); | ||
| }); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| testField: 12345 | ||
| }), | ||
| expect.anything() | ||
| ); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
src/components/mui/formik-inputs/mui-formik-quantity-field.js
This file contains hidden or 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,30 @@ | ||
| import React from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import MuiFormikTextField from "./mui-formik-textfield"; | ||
|
|
||
| const BLOCKED_KEYS = ["e", "E", "+", "-"]; | ||
|
|
||
| const MuiFormikQuantityField = ({ ...props }) => ( | ||
| <MuiFormikTextField | ||
| type="number" | ||
| onKeyDown={(e) => { | ||
| if (BLOCKED_KEYS.includes(e.key)) { | ||
| e.nativeEvent.preventDefault(); | ||
| e.nativeEvent.stopImmediatePropagation(); | ||
| } | ||
| }} | ||
| inputProps={{ | ||
| min: 0, | ||
| inputMode: "numeric" | ||
| }} | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| /> | ||
| ); | ||
|
|
||
| MuiFormikQuantityField.propTypes = { | ||
| name: PropTypes.string.isRequired, | ||
| label: PropTypes.string | ||
| }; | ||
|
|
||
| export default MuiFormikQuantityField; | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Props spread can silently override
inputProps, losingminandinputMode.If a consumer passes
inputProps={{ max: 100 }}, it will completely replace the component'sinputProps, losingmin: 0andinputMode: "numeric". Consider destructuringinputPropsand merging it.🔧 Suggested fix
🤖 Prompt for AI Agents