Skip to content

Commit

Permalink
Apply Safari number fixes to Percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
rjackson committed May 14, 2023
1 parent 40477f7 commit a578484
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/components/fields/Percentage/EditorCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IEditorCellProps } from "@src/components/fields/types";
import EditorCellTextField from "@src/components/Table/TableCell/EditorCellTextField";
import { multiply100WithPrecision, divide100WithPrecision } from "./utils";

export default function Percentage(props: IEditorCellProps<number>) {
export default function Percentage(props: IEditorCellProps<number | string>) {
return (
<EditorCellTextField
{...(props as any)}
Expand All @@ -13,7 +13,16 @@ export default function Percentage(props: IEditorCellProps<number>) {
: props.value
}
onChange={(v) => {
props.onChange(divide100WithPrecision(Number(v)));
// Safari/Firefox gives us an empty string for invalid inputs, which includes inputs like "12." on the way to
// typing "12.34". Number would cast these to 0 and replace the user's input to 0 whilst they're mid-way through
// typing. We want to avoid that.
const parsedValue = v === "" ? v : divide100WithPrecision(Number(v));
props.onChange(parsedValue);
}}
onBlur={() => {
// Cast to number when the user has finished editing
props.onChange(Number(props.value));
props.onDirty();
}}
/>
);
Expand Down
17 changes: 14 additions & 3 deletions src/components/fields/Percentage/SideDrawerField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@ export default function Percentage({
onChange,
onSubmit,
disabled,
}: ISideDrawerFieldProps) {
}: ISideDrawerFieldProps<number | string>) {
const { colors } = (column as any).config;
const theme = useTheme();
return (
<TextField
variant="filled"
fullWidth
margin="none"
onChange={(e) => onChange(Number(e.target.value) / 100)}
onBlur={onSubmit}
onChange={(e) => {
// Safari/Firefox gives us an empty string for invalid inputs, which includes inputs like "12." on the way to
// typing "12.34". Number would cast these to 0 and replace the user's input to 0 whilst they're mid-way through
// typing. We want to avoid that.
const parsedValue =
e.target.value === "" ? e.target.value : Number(e.target.value) / 100;
onChange(parsedValue);
}}
onBlur={() => {
// Cast to number when the user has finished editing
onChange(Number(value));
onSubmit();
}}
value={
typeof value === "number" ? multiply100WithPrecision(value) : value
}
Expand Down

0 comments on commit a578484

Please sign in to comment.