1 | import React, { Component } from 'react'
|
2 | import { List, Switch } from '@sishuguojixuefu/antd-mobile-rn'
|
3 | import { SwitchPropsType } from '../utils/PropTypes'
|
4 | import getFieldDecorator from '../utils/getFieldDecorator'
|
5 | import Label from './helper/Label'
|
6 | import ErrorTip from './helper/ErrorTip'
|
7 |
|
8 | interface Props {
|
9 | label: string
|
10 | required?: boolean
|
11 | onChange?: (checked: boolean) => void
|
12 | checked?: boolean
|
13 | }
|
14 |
|
15 | class SwitchItem extends Component<Props, any> {
|
16 | static defaultProps = {
|
17 | required: false,
|
18 | }
|
19 |
|
20 | constructor(props) {
|
21 | super(props)
|
22 | this.state = {
|
23 | checked: false,
|
24 | }
|
25 | }
|
26 |
|
27 | private _onChange = (checked: boolean) => {
|
28 | const { onChange } = this.props
|
29 | this.setState({ checked }, () => {
|
30 | onChange && onChange(checked)
|
31 | })
|
32 | }
|
33 |
|
34 | render() {
|
35 | const { label, required } = this.props
|
36 | const { checked } = this.state
|
37 | return (
|
38 | <List.Item
|
39 | {...this.props}
|
40 | extra={<Switch checked={checked} onChange={this._onChange} />}
|
41 | last
|
42 | style={{ paddingLeft: 0 }}
|
43 | >
|
44 | <Label label={label} required={required} />
|
45 | </List.Item>
|
46 | )
|
47 | }
|
48 | }
|
49 |
|
50 | export default class Input extends Component<SwitchPropsType, any> {
|
51 | private fieldDecorator: any
|
52 | static defaultProps = {
|
53 | required: false,
|
54 | last: false,
|
55 | }
|
56 |
|
57 | componentWillMount() {
|
58 | const { form, id, initialValue, rules } = this.props
|
59 | this.fieldDecorator = getFieldDecorator(form, id, Boolean(initialValue), rules, { valuePropName: 'checked' })
|
60 | }
|
61 |
|
62 | private _onChange = (checked: boolean) => {
|
63 | const { onChange } = this.props
|
64 | if (checked === false || checked === true) {
|
65 | onChange && onChange(checked)
|
66 | }
|
67 | }
|
68 |
|
69 | render() {
|
70 | const { label, required, form, id, last } = this.props
|
71 | return (
|
72 | <ErrorTip error={form.getFieldError(id)} last={last}>
|
73 | {this.fieldDecorator(<SwitchItem label={label} required={required} onChange={this._onChange} />)}
|
74 | </ErrorTip>
|
75 | )
|
76 | }
|
77 | }
|