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

Number field revamp #2566

Merged
merged 17 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion cypress/e2e/content/content.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe("Content Specs", () => {
it("Number Field", () => {
// NOTE: the timestamp is too large for the 'small int' column in the DB
// limit is 4294967295
cy.get("#12-9b96ec-tll2gn input[type=number]")
cy.get("#12-9b96ec-tll2gn input[type=text]")
.focus()
/*
input type='number 'cannot be empty so rather than whitespace, it'd have a value of 0
Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"react-flatpickr": "^3.10.7",
"react-measure": "^2.5.2",
"react-monaco-editor": "^0.47.0",
"react-number-format": "^5.3.1",
"react-redux": "^7.2.4",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
import { FieldTypeDate } from "../../../../../../../shell/components/FieldTypeDate";
import { FieldTypeDateTime } from "../../../../../../../shell/components/FieldTypeDateTime";
import { FieldTypeSort } from "../../../../../../../shell/components/FieldTypeSort";
import { FieldTypeNumber } from "../../../../../../../shell/components/FieldTypeNumber";

import styles from "./Field.less";
import { MemoryRouter } from "react-router";
Expand Down Expand Up @@ -859,16 +860,12 @@ export const Field = ({
case "number":
return (
<FieldShell settings={fieldData} errors={errors}>
<TextField
size="small"
variant="outlined"
type="number"
fullWidth
value={value ? value.toString() : "0"}
<FieldTypeNumber
value={value?.toString() || "0"}
name={name}
required={required}
onChange={(evt) => onChange(evt.target.value, name)}
error={errors && Object.values(errors)?.some((error) => !!error)}
onChange={onChange}
hasError={errors && Object.values(errors)?.some((error) => !!error)}
/>
</FieldShell>
);
Expand Down
94 changes: 94 additions & 0 deletions src/shell/components/FieldTypeNumber.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { useEffect, useRef } from "react";
import { IconButton, Stack, TextField } from "@mui/material";
import RemoveRoundedIcon from "@mui/icons-material/RemoveRounded";
import AddRoundedIcon from "@mui/icons-material/AddRounded";

import { NumberFormatInput } from "./NumberFormatInput";

type FieldTypeNumberProps = {
required: boolean;
name: string;
value: string;
onChange: (value: string, name: string) => void;
hasError: boolean;
};
export const FieldTypeNumber = ({
required,
value,
onChange,
name,
hasError,
}: FieldTypeNumberProps) => {
const numberInputRef = useRef(null);

useEffect(() => {
if (value === "0") {
numberInputRef.current?.setSelectionRange(1, 1);
}
}, [value]);

const modifyNumberValue = (action: "increment" | "decrement") => {
const integerFractionalSplit = value.toString().split(".");

switch (action) {
case "increment":
integerFractionalSplit[0] = (+integerFractionalSplit[0] + 1).toString();
break;

case "decrement":
integerFractionalSplit[0] = (+integerFractionalSplit[0] - 1).toString();
break;

default:
break;
}

onChange(integerFractionalSplit.join("."), name);
};

return (
<TextField
inputRef={numberInputRef}
variant="outlined"
fullWidth
value={value?.toString() || "0"}
name={name}
required={required}
onChange={(evt) => {
onChange(evt.target.value?.replace(/^0+/, "") ?? "0", name);
}}
onKeyDown={(evt) => {
if (
(evt.key === "Backspace" || evt.key === "Delete") &&
value === "0"
) {
evt.preventDefault();
}
}}
error={hasError}
InputProps={{
endAdornment: (
<Stack direction="row" gap={1}>
<IconButton
size="small"
onClick={() => modifyNumberValue("decrement")}
>
<RemoveRoundedIcon fontSize="small" />
</IconButton>
<IconButton
size="small"
onClick={() => modifyNumberValue("increment")}
>
<AddRoundedIcon fontSize="small" />
</IconButton>
</Stack>
),
inputComponent: NumberFormatInput as any,
inputProps: {
thousandSeparator: true,
valueIsNumericString: true,
},
}}
/>
);
};
32 changes: 32 additions & 0 deletions src/shell/components/NumberFormatInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { forwardRef } from "react";
import {
NumericFormatProps,
InputAttributes,
NumericFormat,
} from "react-number-format";

type NumberFormatInputProps = {
onChange: (event: { target: { name: string; value: string } }) => void;
name: string;
};
export const NumberFormatInput = forwardRef<
Copy link
Contributor

@agalin920 agalin920 Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now with this preferred refactor do you still need this ?

Copy link
Contributor Author

@finnar-bin finnar-bin Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agalin920 I purposely left this for now until we get the final approval from @zcolah's vqa. I'll remove this file and the dependency once the new solution is approved

NumericFormatProps<InputAttributes>,
NumberFormatInputProps
>((props, ref) => {
const { onChange, ...other } = props;

return (
<NumericFormat
{...other}
getInputRef={ref}
onValueChange={(values) => {
onChange({
target: {
name: props.name,
value: values.value || "0",
},
});
}}
/>
);
});
Loading