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

Custom widget access to form Data #4192

Open
4 tasks done
zzph opened this issue May 11, 2024 · 6 comments
Open
4 tasks done

Custom widget access to form Data #4192

zzph opened this issue May 11, 2024 · 6 comments

Comments

@zzph
Copy link

zzph commented May 11, 2024

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

Inside my custom widget, it will not let me access the current formData. So I pass it into formContext but that breaks the form (nothing can be inputted).

Expected Behavior

Inside my custom widget, I can access all the current values of the form

Steps To Reproduce

  1. Using react and rjsf (both on latest)
  2. Using a custom widget (as implemented per docs)
  3. I expect the formData to appear as a prop, but it does not, so I try passing it on formContext instead (as per issue here
  4. Using that formContext freezes the entire form and nothing can be inputted.

I simplified my code as per below (CodeSandbox here):

import { useState } from 'react'
import Form from '@rjsf/core';
import validator from '@rjsf/validator-ajv8';

const TestCmp = (props) => {
  const { formContext } = props;
  // Use formData as needed
  // ...
  return <div> TEST {JSON.stringify(formContext)} </div>
};

const uiSchema = {
  name: {
    'ui:widget': 'TestCmp'
  }
};
const schema = {
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "number"
    }
  }
}

const widgets = {
  TestCmp
};

export default function Create() {
  const [formDataPublic, setFormDataPublic ] = useState({})
  return <Form
        schema={schema}
        validator={validator}
        uiSchema={uiSchema}
        widgets={widgets}
        onChange={({formData})=> setFormDataPublic(formData)}
        formContext={{formDataPublic}}
      />
}

Environment

- OS: MacOS
- Node: 20.11
- npm: 10.2.4

Anything else?

No response

@zzph zzph added bug needs triage Initial label given, to be assigned correct labels and assigned labels May 11, 2024
@zzph
Copy link
Author

zzph commented May 14, 2024

Any ideas on this? I'm blocked from using this awesome library because of it

@heath-freenome heath-freenome added question awaiting response and removed bug needs triage Initial label given, to be assigned correct labels and assigned labels May 17, 2024
@heath-freenome
Copy link
Member

The widget has access to the formData at its current level (in your example the widget will get the value for name) via the value prop. Is there a reason why you need access to all of the formData within a single widget?

@zzph
Copy link
Author

zzph commented May 18, 2024 via email

@heath-freenome
Copy link
Member

@zzph I'm wondering if the "freezing" of the form you are seeing is a situation where your the state change causes a rerender infinite loop. Theoretically, you should be able to put anything into the formContext. Have you debugged why you things are freezing?

@zzph
Copy link
Author

zzph commented May 28, 2024

Yes I've debugged, and it does seem like you're saying- it re-renders the entire component everytime 'formContext' changes. So this is an issue.

Can you suggest a work around?

@heath-freenome
Copy link
Member

heath-freenome commented May 29, 2024

Maybe you can process the formData on change in a way that you transform what you put into the formContext is only what you need to provide the auto-suggest? Meaning if your component looks at the value of age to auto-suggest the value of name (from your example), perhaps precompute the suggestion OUTSIDE of the component and pass in the suggestion rather than the formData in the formContext?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants