king
2020-12-28 6467e4115c5284a15f393e3dd2ffb07ebd2a96d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Switch } from 'antd'
 
import './index.scss'
 
class CheckCard extends Component {
  static propTpyes = {
    Item: PropTypes.bool,      // 表单
    onChange: PropTypes.func   // 数据切换
  }
 
  state = {
    defaultChecked: this.props.Item.initval === true
  }
 
  UNSAFE_componentWillMount () {
    const { Item, onChange } = this.props
    if (Item.initval === true) {
      onChange && onChange(Item.openVal)
    } else {
      onChange && onChange(Item.closeVal)
    }
  }
 
  onChange = (val) => {
    const { Item, onChange } = this.props
    if (val) {
      onChange && onChange(Item.openVal)
    } else {
      onChange && onChange(Item.closeVal)
    }
  }
 
  render() {
    const { Item } = this.props
    const { defaultChecked } = this.state
 
    return (
      <Switch checkedChildren={Item.openText || ''} unCheckedChildren={Item.closeText || ''} defaultChecked={defaultChecked} onChange={this.onChange}/>
    )
  }
}
 
export default CheckCard