Skip to content

NumberField - invalid value stored in formData when working with decimal numbers that use , for decimal point #4755

@BTMPL

Description

@BTMPL

Prerequisites

What theme are you using?

antd

Version

5.x

Current Behavior

The issue can be reproduced in the RJSF playground, https://rjsf-team.github.io/react-jsonschema-form/, by changing to the "number" examples.

When the user enters a numerical value followed by a dot, the value from formData is removed completly.

Looking at the NumberField.tsx code (https://github.com/rjsf-team/react-jsonschema-form/blob/main/packages/core/src/components/fields/NumberField.tsx) there's some special handling for such cases:

/**
 * The NumberField class has some special handling for dealing with trailing
 * decimal points and/or zeroes. This logic is designed to allow trailing values
 * to be visible in the input element, but not be represented in the
 * corresponding form data.
 *
 * The algorithm is as follows:
 *
 * 1. When the input value changes the value is cached in the component state
 *
 * 2. The value is then normalized, removing trailing decimal points and zeros,
 *    then passed to the "onChange" callback
 *
 * 3. When the component is rendered, the formData value is checked against the
 *    value cached in the state. If it matches the cached value, the cached
 *    value is passed to the input instead of the formData value
 */

but from my understanding of point 2 the normalized value should be stored in in the formData object, which is not the case.

Expected Behavior

Proper value is saved in the formData state.

Steps To Reproduce

  1. Navigate to the RJSF e=playground and change to the "Numbers" example
  2. Type "123" in the "Number" field
  3. formData properly reflects the change
  4. Type "123." in the "Number" field
  5. formData no longer holds the value form the field

Environment

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions