ant design中treeselect的默认值不能动态更新

ant design中treeselect的默认值不能动态更新

看代码

import React from 'react'
import ReactDom from 'react-dom'
import { useState } from 'react';
import Toast from './toast'
import { TreeSelect,Input,Button, Modal } from 'antd';

const treeData = [
  {
    value: 'parent 1',
    title: 'parent 1',
    children: [
      {
        value: 'parent 1-0',
        title: 'parent 1-0',
        children: [
          {
            value: 'leaf1',
            title: 'leaf1',
          },
          {
            value: 'leaf2',
            title: 'leaf2',
          },
        ],
      },
      {
        value: 'parent 1-1',
        title: 'parent 1-1',
        children: [
          {
            value: 'leaf3',
            title: (
              <b
                style={{
                  color: '#08c',
                }}
              >
                leaf3
              </b>
            ),
          },
        ],
      },
    ],
  },
];
class Test extends React.Component{
  constructor(){
    super()
    this.state={
      jobNumber: "",
      visible:false,
      treeDefault:'leaf1'
    }
  };
  
  handleNumber = (e) =>{
    console.log(e.target.value)
    this.setState({
      jobNumber:e.target.value
    })
  };
  handleChangeValue =(e)=>{
    this.setState({
      visible:true
    })
  };
  handleOk=e=>{
    console.log(e)
    console.log(this.refs.toast)

    this.setState({
      visible:false,
      jobNumber:this.refs.toast.state.jobNumber2
    })
  };
  handleCancle=e=>{
    console.log(e)
    this.setState({
      visible:false
    })
  };
  add=()=>{
    this.setState({
      jobNumber:Number(this.state.jobNumber)+1
    })
  };
  onChange=(value)=>{
    console.log(value)

  };
  changeDefault = ()=>{
    this.setState({
      treeDefault:this.state.treeDefault==='leaf1'?'leaf2':'leaf1'
    })
    console.log(this.state.treeDefault)
  }

  render(){
    return (
      <div>
        <Input placeholder='请输入' style={{width:"150px"}} onChange={this.handleNumber} value={this.state.jobNumber}></Input>
        <Button type='primary' onClick={this.add}>1</Button>
        <Button type='primary' onClick={this.handleChangeValue}> 传值 </Button>
        <Modal 
          title='Basic Modal'
          open={this.state.visible}
          onOk={this.handleOk}
          onCancel={this.handleCancle}
          >
            <Toast jobNumber={this.state.jobNumber} ref="toast"></Toast>
        </Modal>
        <Button onClick={this.changeDefault}>Change Default</Button>
        <TreeSelect
          showSearch
          style={{
            width: '100%',
          }}
          dropdownStyle={{
            maxHeight: 400,
            overflow: 'auto',
          }}
          placeholder="Please select"
          allowClear
          treeDefaultExpandAll
          onChange={this.onChange}
          treeData={treeData}
          defaultValue={this.state.treeDefault}
        />
      </div>
    )
  }
}

const element = <Test></Test>

ReactDom.render(element,document.getElementById('root'))

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值