Skip to content

Commit

Permalink
Save empty numeric value as null instead of empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Dec 30, 2024
1 parent 9fee23c commit 41b0bbe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
export let fieldConfig;
/**
* @type {string | number}
* @type {string | number | null}
*/
export let currentValue;
/**
Expand Down Expand Up @@ -92,16 +92,20 @@
const setCurrentValue = () => {
let newValue;
if (valueType === 'int') {
newValue = Number.parseInt(isNumeric ? String(numInputValue) : strInputValue, 10);
} else if (valueType === 'float') {
newValue = Number.parseFloat(isNumeric ? String(numInputValue) : strInputValue);
if (isNumeric) {
if (numInputValue === undefined) {
newValue = NaN;
} else if (valueType === 'int') {
newValue = Number.parseInt(isNumeric ? String(numInputValue) : strInputValue, 10);
} else {
newValue = Number.parseFloat(isNumeric ? String(numInputValue) : strInputValue);
}
} else {
newValue = strInputValue;
}
if (isNumeric && Number.isNaN(newValue)) {
newValue = '';
newValue = null;
}
// Avoid a cycle dependency & infinite loop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
export let fieldConfig;
/**
* @type {string | number}
* @type {string | number | null}
*/
export let currentValue;
Expand All @@ -33,7 +33,7 @@
$: numberFormatter = Intl.NumberFormat(canonicalLocale);
</script>

{#if currentValue !== undefined && currentValue !== ''}
{#if currentValue !== undefined && currentValue !== null && currentValue !== ''}
<p lang={locale} dir="auto">
{#if valueType === 'int' || valueType === 'float'}
{numberFormatter.format(Number(currentValue))}
Expand Down

0 comments on commit 41b0bbe

Please sign in to comment.