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

SelectField needs more methods #2504

Closed
jenil opened this issue Dec 13, 2015 · 3 comments
Closed

SelectField needs more methods #2504

jenil opened this issue Dec 13, 2015 · 3 comments
Labels
new feature New feature or request

Comments

@jenil
Copy link

jenil commented Dec 13, 2015

Can we have getValue and setValue methods for SelectFields?

I understand SelectField and DropDownMenu are undergoing a lot of change recently but these methods will be a good-to-have.

@alitaheri
Copy link
Member

@jgog these methods are imperative and make handling the flow of data really hard, that's why we prefer not to support them (directly) but the fact is, it's very easy to write a wrapper component to expose such methods. and of course, you'll need only one wrapper:

class ImperativeWrapper extends React.Component {

  constructor(props) {
    super(props);
    this.state = {value: props.defaultValue};
  }

  setValue = value => this.setState({value});
  getValue = () => this.state.value;
  clearValue = () => this.setState({value: this.props.defaultValue});

  handle = value => this.setState({value});

  render() {
    const {Component, ...other} = this.props;
    return <Component {...other} value={this.state.value} onChange={this.handle}/>;
  }
}

And you would use it like this:

//...

getAllValues() {
   // The imperative method...
   const selectValue = this.refs['my-select-field'].getValue();
}

render() {
  return (
    <ImperativeWrapper 
      ref="my-select-field"
      defaultValue={/*...*/}
      items={/*...*/}
      // And other SelectField props you want to pass down
      Component={SelectField}/>
}
//...

Although this might need a bit of tweaking to make SelectField work with it (and I apologize, I don't have the time to test it my self), but you get the point. We can provide such wrappers as helper components, but that will have to be decided on.

@oliviertassinari Thoughts?

@alitaheri alitaheri added the new feature New feature or request label Dec 13, 2015
@jenil
Copy link
Author

jenil commented Dec 13, 2015

@subjectix Thanks for the explanation, but I thought a SelectField was suppose to work as the html <select> and the value prop should be a part of the FormData when wrapped in a form.
I didn't understand how the handling of data flow becomes hard in case of a SelectField. And I think the onChange way of interaction with the component is more suitable for the DropDownMenu but for SelectField it should be treated as a form element.

@oliviertassinari
Copy link
Member

Will be fixed by the <StateWrapper /> of #2957.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants