Cast number inputs before save

This commit is contained in:
Rob Jackson
2023-05-14 13:46:08 +01:00
parent 65686d80e0
commit 40477f7a36
3 changed files with 23 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import { spreadSx } from "@src/utils/ui";
export interface IEditorCellTextFieldProps extends IEditorCellProps<string> {
InputProps?: Partial<InputBaseProps>;
onBlur?: () => void;
}
export default function EditorCellTextField({
@@ -11,6 +12,7 @@ export default function EditorCellTextField({
value,
onDirty,
onChange,
onBlur,
setFocusInsideCell,
InputProps = {},
}: IEditorCellTextFieldProps) {
@@ -19,7 +21,12 @@ export default function EditorCellTextField({
return (
<InputBase
value={value}
onBlur={() => onDirty()}
onBlur={() => {
if (onBlur) {
onBlur();
}
onDirty();
}}
onChange={(e) => onChange(e.target.value)}
fullWidth
autoFocus
@@ -42,6 +49,11 @@ export default function EditorCellTextField({
setTimeout(() => setFocusInsideCell(false));
}
if (e.key === "Enter" && !e.shiftKey) {
// Trigger an onBlur in case we have any final mutations
if (onBlur) {
onBlur();
}
// Removes focus from inside cell, triggering save on unmount
setFocusInsideCell(false);
}

View File

@@ -13,6 +13,11 @@ export default function Number_(props: IEditorCellProps<number | string>) {
const parsedValue = v === "" ? v : Number(v);
props.onChange(parsedValue);
}}
onBlur={() => {
// Cast to number when the user has finished editing
props.onChange(Number(props.value));
props.onDirty();
}}
/>
);
}

View File

@@ -23,7 +23,11 @@ export default function Number_({
e.target.value === "" ? e.target.value : Number(e.target.value);
onChange(parsedValue);
}}
onBlur={onSubmit}
onBlur={() => {
// Cast to number when the user has finished editing
onChange(Number(value));
onSubmit();
}}
value={value}
id={getFieldId(column.key)}
label=""