FaridSafi / react-native-gifted-form

📝 « One React-Native form component to rule them all »
MIT License
1.44k stars 214 forks source link

Adds value and onValue props to WidgetMixin so we can store state in Redux store #23

Closed danielweinmann closed 8 years ago

danielweinmann commented 8 years ago
class Form extends Component {
  render() {
    return (
      <GiftedForm formName="yourForm">
        <GiftedForm.TextInputWidget
          name='fullName'
          value={this.props.yourReduxReducer.fullName}
          onValue={yourReduxActionThatWillReceiveValue}
        />
      </GiftedForm>
    )
  }  
}

export default connect(state => ({
  yourReduxReducer: state.yourReduxReducer,
}))(Form)

What do you think, @FaridSafi? I believe it is 100% compatible with old usage and ads capability to use it with Redux/Flux.

danielweinmann commented 8 years ago

The way it is implemented, we will have to have 1 redux action per input widget. I did this because it is what I need in my project.

But it would be easy to implement 1 redux action for the whole form using onValidation prop on GiftedForm instead of onValue ;)

FaridSafi commented 8 years ago

Hi, I don't have so much experience with Redux/Flux so I trust you with that. We may have a conflict for the "value" prop, as it's also used in OptionWidget and HiddenWidget (and maybe another?)

What about a different name?

danielweinmann commented 8 years ago

Let me take a closer look ;)

danielweinmann commented 8 years ago

@FaridSafi, from what I could test and understand:

  1. HiddenWidget is functional and now it can receive new value prop over time. Before my changes it would get stuck with initial value prop.
  2. OptionWidget too, but I couldn't think of a scenario where an OptionWidget would change its value over time. Thus, there should be no changes in behaviour in this case.
  3. CellWidget will not work as of now if we pass value prop.
  4. DayPickerWidget, SelectWidget, ModalWidget will not change their behaviour but will not use value prop properly for now. In my project I don't have any need for them so far, so I suggest we leave it for the future. I think it wont break anything.

I removed onValue prop from WidgetMixin and added onValueChange to ContainerMixin, so we can manage all changes in the same place.

I added instructions and example to the README and asked for PR's :) what do you say?

FaridSafi commented 8 years ago

Hello, amazing work @danielweinmann ! I have few questions before publishing

Also I think I will remove soon DayPickerWidget from the library since a crossplatform datepicker is coming in react-native core

danielweinmann commented 8 years ago

Thanks, @FaridSafi!

But for the last few days I've been thinking that everything would be simpler if we didn't try to manage state at all, and GiftedForm was a presentational-only set of widgets. They would receive their values, error messages, etc, from props, inform changes in the values through form's onValueChange and that's it.

The whole state management, validation, etc, logic would be outside of the presentational layer. And then we could choose between using react's default state management, together with validator.js or not, or Redux Form (http://erikras.github.io/redux-form/#/?_k=mznz7a), or anything else.

It would be a big departure from what we currently have, and I cannot invest the time to do it now, but from an architecture point of view it seems like the way to do it.

What do you think of this line of thinking?

FaridSafi commented 8 years ago

I added support for GroupWidget and published 0.0.5

The whole state management, validation, etc, logic would be outside of the presentational layer. And then we could choose between using react's default state management, together with validator.js or not, or Redux Form (http://erikras.github.io/redux-form/#/?_k=mznz7a), or anything else.

I agree with you, for the next big version I want also to make this component compatible with React for Web

Thank you for your time!

danielweinmann commented 8 years ago

That's great! Thank you for the package and for maintaining it :)

I'll keep contributing whenever I can!