AbdullahQureshi1080 / superforms-rn

Super forms using formik and yup in react-native with built-in ready to use form components that are customizable
https://www.npmjs.com/package/@superforms/superforms-rn
MIT License
3 stars 0 forks source link

FormDateSelector change by the model bind #16

Closed SpringSan closed 1 year ago

SpringSan commented 1 year ago

Describe the bug When my data from api change but the value which the field show is not change. So i want to know how to change the value the field show except press the date selector. Also i bind the property 'date' to the state, the date selector change the date but it can't show in the field. Please help me , thank you. '' To Reproduce Steps to reproduce the behavior:

  1. Get data from api

Expected behavior The date field value can change by value change.

Screens f146515abb93ae534c68cda1605328e hots

Smartphone (please complete the following information):

SpringSan commented 1 year ago

I see other field have the Property 'value'

AbdullahQureshi1080 commented 1 year ago

@SpringSan That's an interesting problem, while the form state is not meant to be directly manipulated by providing the value, it exists to be handled by form field state change.

I can however try to add additional state updater binding that to the value prop and updating the form state internally.

What you can do is instead of just having the date object as a state variable, have the entire initial form values as the state, and then when data from API changes try to update the initial values and see if that updates your date field.

Note: While trying this, make the enableReinitialize={true} in form wrapper

Let me know if that works.

I will try to test the use case whenever I can spare some free time this weekend.

SpringSan commented 1 year ago

Your suggestion that update the initial values i have tried. It don't works. But if you update the initial values the date selector's value will update, however the field value that show never change. Also i think the function 'setFeildValue' can update the field value.

1671881216156
SpringSan commented 1 year ago

And the function onDateChange can't work

AbdullahQureshi1080 commented 1 year ago

@SpringSan Yes, I get the point. That is only changing the values for submission and not updating the base date picker's value, hence not the updated value in UI. What we could do is that If an existing value exists from the values[name] == name (date picker) as an initial value we can update that from the start or whenever values[name] for date changes. Kind of like an initial setter and onChange trigger.

AbdullahQureshi1080 commented 1 year ago
  useEffect(() => {
    if (values[name] && moment(values[name).isValid()) {
      setFieldValue(name, values[name]);
    }
  }, []);

@SpringSan Something like this, which might not be the best solution at the moment can make things work.

SpringSan commented 1 year ago
  useEffect(() => {
    if (values[name] && moment(values[name).isValid()) {
      setFieldValue(name, values[name]);
    }
  }, []);

@SpringSan Something like this, which might not be the best solution at the moment can make things work.

Hey,I try to add the code to \node_modules\@superforms\superforms-rn\src\Form\FormDateSelector , but it doesn't works. And it seems never go into this code.

AbdullahQureshi1080 commented 1 year ago
  useEffect(() => {
    if (values[name] && moment(values[name).isValid()) {
      setFieldValue(name, values[name]);
    }
  }, []);

@SpringSan Something like this, which might not be the best solution at the moment can make things work.

Hey,I try to add the code to \node_modules\@superforms\superforms-rn\src\Form\FormDateSelector , but it doesn't works. And it seems never go into this code.

Hmm, it should have. I will debug this later in the week. Maybe you can add values [name] in the dependency array.

SpringSan commented 1 year ago

Ok . Thanks a lot