日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

將antd中的Tree組件放在Form表單里面

作者:小五Ivy 更新時(shí)間: 2022-02-24 編程語言

問題:

  • 前一段時(shí)間使用antd的Tree組件的時(shí)候遇到一個(gè)問題:將Tree組件放在Form表單的時(shí)候,想用initialValueTreedefaultExpandedKeys,defaultSelectedKeys和defaultCheckedKeys賦初始值,已找到解決方法。

解決思路:

  • Tree 不是 form control,你不能直接把 Tree 丟給 getFieldDecorator,需要把 Tree 封裝下再丟給 getFieldDecorator
  • 注意4.x版本的antd不支持Form.create()()方法。

代碼展示

1. 封裝Tree
export default  class TreeDemo extends React.Component {
  render() {
    return (
      <Tree
        checkable
        expandedKeys={['STATUS']}
        checkedKeys={this.props.value}
        onCheck={this.props.onChange}
      >
        <TreeNode title="全選" key="STATUS">
          <TreeNode title="未成交" key="A"></TreeNode>
          <TreeNode title="已成交" key="B"></TreeNode>
          <TreeNode title="流拍" key="C"></TreeNode>
          <TreeNode title="未來供應(yīng)" key="D"></TreeNode>
        </TreeNode>
      </Tree>
    );
  }
}

2. Form表單

import React from 'react';
import { toJS } from 'mobx';
import { observer } from "mobx-react";
import { Row, Col, Button, Form, Popconfirm, message, Checkbox, Tree } from 'antd';

const { Item } = Form
const { TreeNode } = Tree;

class Index extends React.Component {
  constructor(props) {
    super(props)
    this.state = {checkList:['A']}
  }

  //確定
  handleSubmit = e => {
    e && e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      console.log(toJS(values));

      if (!err) {
        //驗(yàn)證無誤
        console.log('驗(yàn)證無誤---');
        this.props.state.getParcelList()
      }
    });
  }

  //土地狀態(tài)
  onCheck = checkedKeys => {
    //過濾掉父級的key
    checkedKeys = checkedKeys.filter(item => item !== 'STATUS')
    this.setState({checkList:checkedKeys})
  };

  render() {
    const { getFieldDecorator } = this.props.form
    return (
      <div>
        <Form layout="inline" autoComplete="off" onSubmit={this.handleSubmit} >      
          <Row>
            <Col span={4}>demo</Col>
            <Col span={20}>
              <Item>
                {
                  getFieldDecorator('demo', {
                    initialValue: this.state.checkList,
                  })
                    (
                      <TreeDemo onChange={this.onCheck} />
                    )}
              </Item>
            </Col>
          </Row>        
          <Row type='flex' justify="center">
            <Button type='primary' htmlType="submit">確定</Button>
          </Row>
        </Form>
      </div>
    )
  }
}
export default Form.create({
  onValuesChange(props, changeValues, allValues) {
    // console.log(allValues, 'allValues');

  }
})(Index);

原文鏈接:https://blog.csdn.net/weixin_44471622/article/details/106994055

欄目分類
最近更新