Allow input and display of floats for Number fields (#7340)
### Description - We added a decimal field for a Number Field type in the settings - We updated the Number Field type create a form with decimals input - We are not implementing the dropdown present on the Figma because it seems not related ### Demo <https://www.loom.com/share/18a8d4b712a14f6d8b66806764f8467f?sid=3fc79b46-ae32-46e3-8635-d0eee02e53b2> Fixes #6987 --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: Marie Stoppa <marie.stoppa@essec.edu>
This commit is contained in:
committed by
GitHub
parent
e3ed574420
commit
97eff774bd
75
packages/twenty-front/src/utils/cast-as-number-or-null.ts
Normal file
75
packages/twenty-front/src/utils/cast-as-number-or-null.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { isNull, isNumber, isString } from '@sniptt/guards';
|
||||
|
||||
import { logError } from './logError';
|
||||
|
||||
const DEBUG_MODE = false;
|
||||
|
||||
export const canBeCastAsNumberOrNull = (
|
||||
probableNumberOrNull: string | undefined | number | null,
|
||||
): probableNumberOrNull is number | null => {
|
||||
if (probableNumberOrNull === undefined) {
|
||||
if (DEBUG_MODE) logError('probableNumberOrNull === undefined');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isNumber(probableNumberOrNull)) {
|
||||
if (DEBUG_MODE) logError('typeof probableNumberOrNull === "number"');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isNull(probableNumberOrNull)) {
|
||||
if (DEBUG_MODE) logError('probableNumberOrNull === null');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (probableNumberOrNull === '') {
|
||||
if (DEBUG_MODE) logError('probableNumberOrNull === ""');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isString(probableNumberOrNull)) {
|
||||
const stringAsNumber = +probableNumberOrNull;
|
||||
|
||||
if (isNaN(stringAsNumber)) {
|
||||
if (DEBUG_MODE) logError('isNaN(stringAsNumber)');
|
||||
|
||||
return false;
|
||||
}
|
||||
if (isNumber(stringAsNumber)) {
|
||||
if (DEBUG_MODE) logError('isNumber(stringAsNumber)');
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export const castAsNumberOrNull = (
|
||||
probableNumberOrNull: string | undefined | number | null,
|
||||
): number | null => {
|
||||
if (canBeCastAsNumberOrNull(probableNumberOrNull) === false) {
|
||||
throw new Error('Cannot cast to number or null');
|
||||
}
|
||||
|
||||
if (isNull(probableNumberOrNull)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isString(probableNumberOrNull)) {
|
||||
if (probableNumberOrNull === '') {
|
||||
return null;
|
||||
}
|
||||
return +probableNumberOrNull;
|
||||
}
|
||||
|
||||
if (isNumber(probableNumberOrNull)) {
|
||||
return probableNumberOrNull;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
Reference in New Issue
Block a user