Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) parse NumberInput value to Number #367

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/inputs/number/number.component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import { Layer, NumberInput } from '@carbon/react';
import classNames from 'classnames';
import { isTrue } from '../../../utils/boolean-utils';
Expand All @@ -22,6 +22,11 @@ const NumberField: React.FC<FormFieldInputProps> = ({ field, value, errors, warn
}
};

const handleChange = useCallback((event) => {
const parsedValue = Number(event.target.value);
setFieldValue(isNaN(parsedValue) ? undefined : parsedValue);
}, [setFieldValue]);

const isInline = useMemo(() => {
if (['view', 'embedded-view'].includes(sessionMode) || isTrue(field.readonly)) {
return shouldUseInlineLayout(field.inlineRendering, layoutType, workspaceLayout, sessionMode);
Expand Down Expand Up @@ -49,7 +54,7 @@ const NumberField: React.FC<FormFieldInputProps> = ({ field, value, errors, warn
min={Number(field.questionOptions.min) || undefined}
name={field.id}
value={field.value ?? ''}
onChange={setFieldValue}
onChange={handleChange}
onBlur={onBlur}
allowEmpty={true}
size="lg"
Expand Down
Loading