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

formData definition in types should support undefined as a valid value #3305

Closed
4 tasks done
dagda1 opened this issue Dec 21, 2022 · 5 comments · Fixed by #3395
Closed
4 tasks done

formData definition in types should support undefined as a valid value #3305

dagda1 opened this issue Dec 21, 2022 · 5 comments · Fixed by #3395

Comments

@dagda1
Copy link
Contributor

dagda1 commented Dec 21, 2022

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

In versions before 5.0.0-beta.*, if the<Form /> was given a schema like this:

{
  properties: {
    repository: {
      type: "object"
    }
  }
}

The form component would call getStateFromProps which would, in turn, call computeDefaults and return:

{
  repository: {}
}

This would allow getDefaultFormState to initialise the form correctly.

The code in computeDefaults in v5.0.0-beta.* has an isEmpty check, meaning non-empty objects will not get initialised correctly, and formData will be {}.

The knock-on effect of that is that this code in ObjectField will return undefined for the formData:

        return {
          content: (
            <SchemaField
              formData={get(formData, name)}  // undefined

Any ui:field will receive undefined for the initial formData

Expected Behavior

formData should at least initialise to an empty object.

Steps To Reproduce

I created a failing test here.

Environment

- OS:osx
- Node:16
- npm:8

Anything else?

No response

@dagda1 dagda1 added bug needs triage Initial label given, to be assigned correct labels and assigned labels Dec 21, 2022
@heath-freenome heath-freenome removed the needs triage Initial label given, to be assigned correct labels and assigned label Jan 6, 2023
@heath-freenome
Copy link
Member

@dagda1 Were you depending on that behavior? Many people had been complaining that this behavior was, in fact, a bug and so it was fixed in #3287. If we provided an opt-in flag that would let you choose to initialize top-level objects as an empty object would that help?

Also, there is also a big issue (#2980) that indicates our automatically saving defaulting values in the formData is an actual bug itself, and that we should not do so ever.

@benjdlambert
Copy link
Contributor

@heath-freenome me and @dagda1 discussed this a little bit, and we're happy that the behaviour is fine. However we noticed that the types for FieldProps could maybe do with updating? Just wanted to bring this to your attention before you cut a v5 release as I think it would be a breaking change. Seen as it now uses get and doesn't default, should formData this be optional?

interface FieldProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> extends GenericObjectType, Pick<React.HTMLAttributes<HTMLElement>, Exclude<keyof React.HTMLAttributes<HTMLElement>, "onBlur" | "onFocus" | "onChange">> {
    /** The data for this field */
    formData: T;

@heath-freenome
Copy link
Member

Thoughts @nickgros @epicfaace ?

@heath-freenome
Copy link
Member

@heath-freenome me and @dagda1 discussed this a little bit, and we're happy that the behaviour is fine. However we noticed that the types for FieldProps could maybe do with updating? Just wanted to bring this to your attention before you cut a v5 release as I think it would be a breaking change. Seen as it now uses get and doesn't default, should formData this be optional?

interface FieldProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> extends GenericObjectType, Pick<React.HTMLAttributes<HTMLElement>, Exclude<keyof React.HTMLAttributes<HTMLElement>, "onBlur" | "onFocus" | "onChange">> {
    /** The data for this field */
    formData: T;

Good catch. I will be pushing a PR against this issue to fix formData to support undefined everywhere

@heath-freenome heath-freenome changed the title ui:field not initialising formData when no default given formData definition in types should support undefined as a valid value Jan 24, 2023
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Jan 24, 2023
Fixes rjsf-team#3305 by ensuring that `formData` can be optional everywhere

There were some types which were requiring `formData` to be required even though it really can be optional everywhere
- In `@rjsf/utils`, updated the `formData` prop in all types where it was required to make it optional
  - Also updated all function arguments where it was required to make `formData` optional
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated places where `formData` was required as a function parameter to make it optional instead
- In `@rjsf/core`, updated places where `formData` was required as a function parameter to make it optional instead
- In `utility-functions.md`, updated the documentation of optional parameters without default values to ensure they included `| undefined` in the type of the parameter
- In `5.x upgrade guide.md`, updated the note about improved typescript typing in `@rjsf/utils` to indicate that it may require updating typing in the user's implementations
- Updated the `CHANGELOG.md` accordingly
@dagda1
Copy link
Contributor Author

dagda1 commented Jan 24, 2023

Good catch. I will be pushing a PR against this issue to fix formData to support undefined everywhere

Great! Thank you

heath-freenome added a commit that referenced this issue Jan 24, 2023
Fixes #3305 by ensuring that `formData` can be optional everywhere

There were some types which were requiring `formData` to be required even though it really can be optional everywhere
- In `@rjsf/utils`, updated the `formData` prop in all types where it was required to make it optional
  - Also updated all function arguments where it was required to make `formData` optional
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated places where `formData` was required as a function parameter to make it optional instead
- In `@rjsf/core`, updated places where `formData` was required as a function parameter to make it optional instead
- In `utility-functions.md`, updated the documentation of optional parameters without default values to ensure they included `| undefined` in the type of the parameter
- In `5.x upgrade guide.md`, updated the note about improved typescript typing in `@rjsf/utils` to indicate that it may require updating typing in the user's implementations
- Updated the `CHANGELOG.md` accordingly
shijistar pushed a commit to shijistar/react-jsonschema-form that referenced this issue Jun 8, 2023
Fixes rjsf-team#3305 by ensuring that `formData` can be optional everywhere

There were some types which were requiring `formData` to be required even though it really can be optional everywhere
- In `@rjsf/utils`, updated the `formData` prop in all types where it was required to make it optional
  - Also updated all function arguments where it was required to make `formData` optional
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated places where `formData` was required as a function parameter to make it optional instead
- In `@rjsf/core`, updated places where `formData` was required as a function parameter to make it optional instead
- In `utility-functions.md`, updated the documentation of optional parameters without default values to ensure they included `| undefined` in the type of the parameter
- In `5.x upgrade guide.md`, updated the note about improved typescript typing in `@rjsf/utils` to indicate that it may require updating typing in the user's implementations
- Updated the `CHANGELOG.md` accordingly
shijistar pushed a commit to shijistar/react-jsonschema-form that referenced this issue Jun 8, 2023
Fixes rjsf-team#3305 by ensuring that `formData` can be optional everywhere

There were some types which were requiring `formData` to be required even though it really can be optional everywhere
- In `@rjsf/utils`, updated the `formData` prop in all types where it was required to make it optional
  - Also updated all function arguments where it was required to make `formData` optional
- In `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8`, updated places where `formData` was required as a function parameter to make it optional instead
- In `@rjsf/core`, updated places where `formData` was required as a function parameter to make it optional instead
- In `utility-functions.md`, updated the documentation of optional parameters without default values to ensure they included `| undefined` in the type of the parameter
- In `5.x upgrade guide.md`, updated the note about improved typescript typing in `@rjsf/utils` to indicate that it may require updating typing in the user's implementations
- Updated the `CHANGELOG.md` accordingly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants