-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add new formik input for file size, update page media module #791
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
Changes from all commits
7b9e09f
ca45888
03dc271
dc97dd0
1ad4d94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 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 MuiFormikFilesizeField from "../formik-inputs/mui-formik-file-size-field"; | ||
| import { BYTES_PER_MB } from "../../../utils/constants"; | ||
|
|
||
| const renderWithFormik = (props, initialValues = { max_file_size: 0 }) => | ||
| render( | ||
| <Formik initialValues={initialValues} onSubmit={props.onSubmit}> | ||
| <Form> | ||
| <MuiFormikFilesizeField name="max_file_size" {...props} /> | ||
| <button type="submit">submit</button> | ||
| </Form> | ||
| </Formik> | ||
| ); | ||
|
|
||
| describe("MuiFormikFilesizeField", () => { | ||
| describe("display and store", () => { | ||
| it("converts MB input to bytes", async () => { | ||
| const onSubmit = jest.fn(); | ||
| renderWithFormik({ | ||
| label: "Max File Size", | ||
| onSubmit | ||
| }); | ||
|
|
||
| const field = screen.getByLabelText("Max File Size"); | ||
| const submitButton = screen.getByText("submit"); | ||
|
|
||
| await act(async () => { | ||
| await userEvent.clear(field); // field initializes with 0 | ||
| await userEvent.type(field, "10"); | ||
| await userEvent.click(submitButton); | ||
| }); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| max_file_size: 10 * BYTES_PER_MB | ||
| }), | ||
| expect.anything() | ||
| ); | ||
| }); | ||
|
|
||
| it("displays bytes as MB", async () => { | ||
| const onSubmit = jest.fn(); | ||
| renderWithFormik( | ||
| { | ||
| label: "Max File Size", | ||
| onSubmit | ||
| }, | ||
| { max_file_size: 15_728_640 } // 15 * 1_048_576 | ||
| ); | ||
|
|
||
| const field = screen.getByLabelText("Max File Size"); | ||
| expect(field).toHaveValue(15); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import React from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import { InputAdornment } from "@mui/material"; | ||
| import { useField } from "formik"; | ||
| import MuiFormikTextField from "./mui-formik-textfield"; | ||
| import { BYTES_PER_MB } from "../../../utils/constants"; | ||
|
|
||
| const BLOCKED_KEYS = ["e", "E", "+", "-", ".", ","]; | ||
|
|
||
| const MuiFormikFilesizeField = ({ name, label, ...props }) => { | ||
| const [field, meta, helpers] = useField(name); | ||
|
|
||
| const displayValue = | ||
| field.value != null ? Math.floor(field.value / BYTES_PER_MB) : 0; | ||
|
|
||
| const emptyValue = meta.initialValue === null ? null : 0; | ||
|
|
||
| const handleChange = (e) => { | ||
| const mbValue = e.target.value; | ||
|
|
||
| if (mbValue === "") { | ||
| helpers.setValue(emptyValue); | ||
| return; | ||
| } | ||
|
|
||
| const bytes = Number(mbValue) * BYTES_PER_MB; | ||
| helpers.setValue(bytes); | ||
| }; | ||
|
|
||
| return ( | ||
| <MuiFormikTextField | ||
| name={name} | ||
| label={label} | ||
| type="number" | ||
| value={displayValue} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this? formik will pick up the value with the name
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is required to display a simpler value to the user (MB) instead of the value sent and received from the API (bytes) |
||
| onChange={handleChange} | ||
| slotProps={{ | ||
| input: { | ||
| endAdornment: <InputAdornment position="end">MB</InputAdornment> | ||
| } | ||
| }} | ||
| onKeyDown={(e) => { | ||
| if (BLOCKED_KEYS.includes(e.key)) { | ||
| e.nativeEvent.preventDefault(); | ||
| e.nativeEvent.stopImmediatePropagation(); | ||
| } | ||
| }} | ||
| inputProps={{ | ||
| min: 0, | ||
| inputMode: "numeric", | ||
| step: 1 | ||
| }} | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| MuiFormikFilesizeField.propTypes = { | ||
| name: PropTypes.string.isRequired, | ||
| label: PropTypes.string | ||
| }; | ||
|
|
||
| export default MuiFormikFilesizeField; | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -94,7 +94,8 @@ | |||||||||
| "url": "Wrong URL format", | ||||||||||
| "date": "Wrong date format.", | ||||||||||
| "after": "'{field1}' must be after '{field2}'.", | ||||||||||
| "boolean": "Must be a boolean." | ||||||||||
| "boolean": "Must be a boolean.", | ||||||||||
| "mib_aligned": "Must be a MiB aligned value" | ||||||||||
|
Comment on lines
+97
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align MB/MiB wording with the UI label. Line 98 says “MiB” while the UI label says “MB” (Line 3976). This mixed terminology can confuse users—please pick one term across label + validation. 📝 Suggested wording fix (if you keep “MB” in the label)- "mib_aligned": "Must be a MiB aligned value"
+ "mib_aligned": "Must be an MB-aligned value."📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| }, | ||||||||||
| "landing": { | ||||||||||
| "os_summit_admin": "Show Admin", | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.