king
2023-03-11 642b103206a54923a460ee36550f275b22d6f09c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { message, InputNumber } from 'antd'
import { PlusOutlined, MinusOutlined } from '@ant-design/icons'
 
import './index.scss'
 
class MkCounter extends Component {
  static propTpyes = {
    count: PropTypes.number,
    btn: PropTypes.object,
    disabled: PropTypes.any,
    onChange: PropTypes.func
  }
 
  state = {
    count: 0,
    orival: 0
  }
 
  timer = null
 
  UNSAFE_componentWillMount() {
    this.setState({count: this.props.count, orival: this.props.count})
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
    this.setState({count: nextProps.count, orival: nextProps.count})
  }
 
  minus = () => {
    const { btn } = this.props
    const { count } = this.state
    
    let val = count - 1
 
    if (btn.min !== '' && val < btn.min) {
      message.warning(`不可小于${btn.min}!`)
      return
    } else if (btn.max !== '' && val > btn.max) {
      message.warning(`不可大于${btn.max}!`)
      return
    }
 
    this.setState({count: val, orival: val})
 
    clearTimeout(this.timer)
 
    this.timer = setTimeout(() => {
      this.props.onChange(val)
    }, 1000)
  }
 
  plus = () => {
    const { btn } = this.props
    const { count } = this.state
    
    let val = count + 1
 
    if (btn.min !== '' && val < btn.min) {
      message.warning(`不可小于${btn.min}!`)
      return
    } else if (btn.max !== '' && val > btn.max) {
      message.warning(`不可大于${btn.max}!`)
      return
    }
 
    this.setState({count: val, orival: val})
 
    clearTimeout(this.timer)
 
    this.timer = setTimeout(() => {
      this.props.onChange(val)
    }, 1000)
  }
 
  submit = () => {
    const { btn } = this.props
    const { count, orival } = this.state
 
    if (count === '') {
      message.warning(`不可为空!`)
      this.setState({count: 0})
      return
    } else if (btn.min !== '' && count < btn.min) {
      message.warning(`不可小于${btn.min}!`)
      return
    } else if (btn.max !== '' && count > btn.max) {
      message.warning(`不可大于${btn.max}!`)
      return
    }
 
    if (orival === count) return
 
    clearTimeout(this.timer)
 
    this.timer = setTimeout(() => {
      this.props.onChange(count)
    }, 1000)
  }
 
  render() {
    const { btn, disabled } = this.props
    const { count } = this.state
 
    return (
      <div onClick={(e) => e.stopPropagation()} className={'mk-btn-counter ' + (btn.size || '') + (disabled ? ' mk-disabled' : '')} style={btn.style}>
        <span onClick={this.minus}><MinusOutlined /></span>
        <span><InputNumber value={count} onChange={(val) => this.setState({count: val})} onBlur={this.submit} onPressEnter={this.submit}/></span>
        <span onClick={this.plus}><PlusOutlined /></span>
      </div>
    )
  }
}
 
export default MkCounter