简体   繁体   English

ReactJS,如何从onChange下拉菜单更改表单类型

[英]ReactJS , How to change form type, from onChange dropdown

I'm learning ReactJS, I have dropdown from semantic-ui , and I want it to add onChange event on it. 我正在学习ReactJS,我从语义UI下拉列表,我希望它在上面添加onChange事件。 when dropdown change, the form below also change. 当下拉列表更改时,下面的表单也会更改。 is it possible? 可能吗? anyone know how to do it? 有人知道怎么做吗?

this is my code. 这是我的代码。

layout 布局

    class EditForm extends React.Component {
    componentWillMount() {
        this.setState({
          options: [
            { key: 1, text: 'Input', value: '1' },
            { key: 2, text: 'Dropdown', value: '2' },
            { key: 3, text: 'Radio Boxes', value: '3' },
            { key: 4, text: 'Checkboxes', value: '4' },
            { key: 5, text: 'Paragraph', value:'5'},
          ],
          selected: ['0'],
        });
      }

      handleChange = (e, { value }) => this.setState({
         value
       })
      render() {
        const { value } = this.state
      return(
          <Segment clearing>
              <Container textAlign='right'>
                  <Dropdown selection
                    onChange={this.handleChange}
                    options={this.state.options}
                    value={value}
                    defaultValue={this.state.selected}
                  />
              </Container>
              <Form.Group widths='equal'>
            <Form.Field>
                <Input
                style={{width:'480px'}}
                />
            </Form.Field>
            <Form.Field >
              <Button animated='vertical' floated='right' >
                <Button.Content hidden>Delete</Button.Content>
                <Button.Content visible>
                  <Icon name='trash outline' />
                </Button.Content>
              </Button>
              <Button animated='vertical' floated='right'>
                <Button.Content hidden>Copy</Button.Content>
                <Button.Content visible>
                  <Icon name='copy' />
                </Button.Content>
              </Button>
            </Form.Field>
            </Segment>
        );
      export default EditForm;

I want it to change depending on selected value. 我希望它根据所选值进行更改。 if dropdown is selected. 如果选择下拉菜单。 than the dropdown form appear. 出现下拉菜单。 and so on. 等等。 is it possible? 可能吗? what is the best way to do it? 最好的方法是什么? Thanks. 谢谢。

You can work with conditional rendering where you check for a specific condition before render your component. 您可以使用条件渲染 ,在渲染组件之前检查特定条件。

Something as: 如:

render() {
  const { value } = this.state

  return (
    <div>
        {value === 'dropdown' && <Component1>...</Component1>}
        {value === 'text' && <Component2></Component2>}
        {value === 'checkboxes' && <Component3></Component3>}
        ...
    </div>
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM