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'))
效果