Add ability to properly cast a string, number, null to an integer (#990)
This commit is contained in:
@ -4,12 +4,16 @@ import { EditableField } from '@/ui/editable-field/components/EditableField';
|
||||
import { FieldContext } from '@/ui/editable-field/states/FieldContext';
|
||||
import { InplaceInputText } from '@/ui/inplace-input/components/InplaceInputText';
|
||||
import { RecoilScope } from '@/ui/recoil-scope/components/RecoilScope';
|
||||
import {
|
||||
canBeCastAsIntegerOrNull,
|
||||
castAsIntegerOrNull,
|
||||
} from '~/utils/cast-as-integer-or-null';
|
||||
|
||||
type OwnProps = {
|
||||
icon?: React.ReactNode;
|
||||
placeholder?: string;
|
||||
value: number | null | undefined;
|
||||
onSubmit?: (newValue: number) => void;
|
||||
onSubmit?: (newValue: number | null) => void;
|
||||
};
|
||||
|
||||
export function NumberEditableField({
|
||||
@ -29,26 +33,16 @@ export function NumberEditableField({
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
if (!internalValue) return;
|
||||
|
||||
try {
|
||||
const numberValue = parseInt(internalValue);
|
||||
|
||||
if (isNaN(numberValue)) {
|
||||
throw new Error('Not a number');
|
||||
}
|
||||
|
||||
// TODO: find a way to store this better in DB
|
||||
if (numberValue > 2000000000) {
|
||||
throw new Error('Number too big');
|
||||
}
|
||||
|
||||
onSubmit?.(numberValue);
|
||||
|
||||
setInternalValue(numberValue.toString());
|
||||
} catch {
|
||||
if (!canBeCastAsIntegerOrNull(internalValue)) {
|
||||
handleCancel();
|
||||
return;
|
||||
}
|
||||
|
||||
const valueCastedAsNumberOrNull = castAsIntegerOrNull(internalValue);
|
||||
|
||||
onSubmit?.(valueCastedAsNumberOrNull);
|
||||
|
||||
setInternalValue(valueCastedAsNumberOrNull?.toString());
|
||||
}
|
||||
|
||||
async function handleCancel() {
|
||||
|
||||
Reference in New Issue
Block a user