reactjs React-materialize Chip - Javascript Plugin - autocomplete:如何更新状态变量

w1e3prcc  于 2023-06-05  发布在  React
关注(0)|答案(1)|浏览(109)

我想更新一个状态变量数组,但我不知道如何在更改时更新。我想改变数组“国家”,但当我尝试将其与React-materialize芯片链接时,它不起作用。选择一个选项后:

  • 要么我没有办法更新状态变量,我看到了带有X的芯片来删除它
  • 要么我有更新状态的功能,但后来我没有看到带X的芯片来删除它

我怎样才能使所选芯片正常显示,同时仍然更新状态变量?谢谢你
下面是我的代码,也可以在CodeSandbox中使用

import React from "react";
import ReactDOM from "react-dom";
import { Icon, Chip} from 'react-materialize';
import 'materialize-css';

    class App extends React.Component {
      constructor() {
        super();
        this.state = {
          name: "Venom",
          countries: [{ name: 'France' }, { name: 'Germany' }],
        };
      }
      handleAddCountry = (e) => {
        console.log("Chip :")
        console.log(e.target.name)
        this.setState({ countries: this.state.countries.concat([{ name: e.target.value }]) });
      }
      handleAddFrance = (e) => {
        console.log("Button:")
        console.log(e.target.value)
        this.setState({ countries: this.state.countries.concat([{ name: e.target.value }]) });
      }
      render() {
        const { countries} = this.state
        return (
        <div>
              <div>
                <h1>{this.state.name}</h1>
              </div>
         
            <div>
                <Chip
                        onChange={ this.handleAddCountry } 
                        close={false}
                        closeIcon={<Icon className="close">close</Icon>}
                        options={{
                        placeholder: 'saisir un territoire',
                        secondaryPlaceholder: '+territoire',
                        autocompleteOptions: {
                        data: {
                        '   France m&#233;tropolitaine  '   :   null,
                        '   DOM - TOM   '   :   null,
                        '   DOM '   :   null,
                        '   Afghanistan '   :   null,
                        },
                        limit: Infinity,
                        minLength: 1,
                        onAutocomplete: function noRefCheck(){}
                        }
                        }}
                />
            </div>
            <button type="button" onClick={this.handleAddFrance} value="France" className="small">Add France</button>
                <br></br>    
                {countries.map(country => <div>{country.name}</div>)}
          </div>
        );
      }
    }
    const rootElement = document.getElementById("root");
    ReactDOM.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>,
      rootElement

);
c3frrgcw

c3frrgcw1#

最后,这是我找到的解决方法:

<Chip close={false}
                closeIcon={<Icon className="close">close</Icon>}
                options={{
                  onChipAdd: this.addProdTerritory,
                  placeholder: "saisir territoire",
                  secondaryPlaceholder: "+territoire",
                  autocompleteOptions: {
                    data: {
                      " France métropolitaine " : null  ,
                      " Algérie " : null  ,
                      " Allemagne " : null  ,
                      " Andorre " : null  ,

                    },
                    limit: Infinity,
                    minLength: 1,
                    onAutocomplete: function noRefCheck() {}},}}
            />

this.state = {
      countriesProd: [{ name: "France" }]
                   }
    this.addProdTerritory = this.addProdTerritory.bind(this);
addProdTerritory = foo => {
    var ret = foo.text().replace('close','');
    this.setState({
      countriesProd: this.state.countriesProd.concat([{ name: ret }])
    });
  };

  deleteProdTerritory(i) {
    const { countriesProd } = this.state;
    countriesProd.splice(i, 1);
    this.setState({ countriesProd });
  }

在autocomplete字段下面,我显示一个表,其中第1列是筹码,第2列是删除按钮
我在GitHub上算出来的

相关问题