Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
How can I bind the selected value of a cascading dropdown to a h1 tag in Ant Design?

I am using Ant Design to create a cascading dropdown. I am looking to return the selected value as a tag to display something like "You selected {name}". How can I bind the selected value to the h1 tag? Here is the code so far:

    const options = [
      {
        value: 'Person',
        label: 'person',
        children: [
          {
            value: 'amy',
            label: 'Amy',
          },
          {
           value: 'john',
           label: 'John'
          }
        ],
      }
    ];
    
    function onChange(value, selectedOptions) {
      console.log(value, selectedOptions);
    }
    
    function filter(inputValue, path) {
      return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
    }  
    
    class CascadingDropdown extends Component{
      render(){
            return(
                <div>
                    <p>Please select a person:</p>
                  <div>
                  <Cascader
                      options={options}
                      onChange={onChange}
                      placeholder="Please select"
                      showSearch={{ filter }}
                      onSearch={value => console.log(value)}
                    />
                  </div>
                  <h1>You selected {name}</h1> // here is where I want to print the name
                </div>
            );
        }
    }
    
    export default CascadingDropdown;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use can use state to display the selected value as shown in the following code

const options = [
  {
    value: 'Person',
    label: 'person',
    children: [
      {
        value: 'amy',
        label: 'Amy',
      },
      {
        value: 'john',
        label: 'John',
      },
    ],
  },
];

class CascadingDropdown extends Component {
  onChange(value, selectedOptions) {
    console.log(value, selectedOptions);
    if (value != undefined) {
      this.setState({ name: value[1].toString() });
    }
  }

  filter(inputValue, path) {
    return path.some(
      (option) =>
        option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
    );
  }

  constructor(props) {
    super(props);
    this.state = {
      name: '',
    };
  }
  render() {
    return (
      <div>
        <p>Please select a person:</p>
        <div>
          <Cascader
            options={options}
            onChange={this.onChange.bind(this)}
            placeholder="Please select"
            showSearch={this.filter}
            onSearch={(value) => console.log(value)}
          />
        </div>
        <h1>You selected {this.state.name}</h1> 
      </div>
    );
  }
}

Screenshot:

Screenshot

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda