import './demo4.css';
import 'whatwg-fetch';
import CascaderSelect from '..';
import React from 'react';
import ReactDOM from 'react-dom';
interface PageStates {
  data: Array<any>;
  value: Array<any>;
}
class Demo extends React.Component<{}, PageStates> {
  handleChange: (value: any, data: any, extra: any) => void;
  constructor(props) {
    super(props);

    this.handleChange = (value, data, extra) => {
      console.log(value, data, extra);
    };
    this.state = {
      data: [],
      value: [],
    };
  }

  componentDidMount() {
    fetch('https://os.alipayobjects.com/rmsportal/ODDwqcDFTLAguOvWEolX.json')
      .then(response => response.json())
      .then(data => this.setState({ data, value: [ '2975' ] }))
      .catch(e => console.log(e));
  }

  filter(searchValue, path) {
    return (
      searchValue === '' ||
      path
        .map(({ value, label }) => `${value}${label}`)
        .join('')
        .match(searchValue)
    );
  }

  render() {
    return (
      <div className="search-demo">
        <CascaderSelect
          showSearch
          style={{ width: '240px' }}
          dataSource={this.state.data}
          placeholder="搜索名字"
          onChange={this.handleChange}
        />
        <br />
        <br />
        <CascaderSelect
          showSearch
          filter={this.filter}
          style={{ width: '240px' }}
          dataSource={this.state.data}
          onChange={this.handleChange}
          placeholder="搜索名字或id值"
        />
      </div>
    );
  }
}

ReactDOM.render(<Demo />, document.getElementById('cascader-select-demo-4'));
