king
2024-06-21 eaf119a885719fdc172037510fd2f263ec3d5471
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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(`${window.GLOB.dict['less_limit'] || '不可小于'}${btn.min}!`)
      return
    } else if (btn.max !== '' && val > btn.max) {
      message.warning(`${window.GLOB.dict['max_limit'] || '不可大于'}${btn.max}!`)
      return
    }
 
    this.setState({count: val, orival: val})
 
    clearTimeout(this.timer)
 
    this.timer = setTimeout(() => {
      this.props.onChange(val)
    }, btn.formType === 'count_line' ? 100 : 500)
  }
 
  plus = () => {
    const { btn } = this.props
    const { count } = this.state
    
    let val = count + 1
 
    if (btn.min !== '' && val < btn.min) {
      message.warning(`${window.GLOB.dict['less_limit'] || '不可小于'}${btn.min}!`)
      return
    } else if (btn.max !== '' && val > btn.max) {
      message.warning(`${window.GLOB.dict['max_limit'] || '不可大于'}${btn.max}!`)
      return
    }
 
    this.setState({count: val, orival: val})
 
    clearTimeout(this.timer)
 
    this.timer = setTimeout(() => {
      this.props.onChange(val)
    }, btn.formType === 'count_line' ? 100 : 500)
  }
 
  submit = () => {
    const { btn } = this.props
    const { count, orival } = this.state
 
    if (count === '') {
      message.warning(window.GLOB.dict['not_empty'] || '不可为空!')
      this.setState({count: 0})
      return
    } else if (btn.min !== '' && count < btn.min) {
      message.warning(`${window.GLOB.dict['less_limit'] || '不可小于'}${btn.min}!`)
      return
    } else if (btn.max !== '' && count > btn.max) {
      message.warning(`${window.GLOB.dict['max_limit'] || '不可大于'}${btn.max}!`)
      return
    }
 
    if (orival === count) return
 
    clearTimeout(this.timer)
 
    this.timer = setTimeout(() => {
      this.props.onChange(count)
    }, btn.formType === 'count_line' ? 100 : 500)
  }
 
  onChange = (val) => {
    const { btn } = this.props
 
    this.setState({count: val})
 
    if (btn.formType === 'count_line' && typeof(val) === 'number') {
      clearTimeout(this.timer)
 
      this.timer = setTimeout(() => {
        this.props.onChange(val)
      }, 100)
    }
  }
 
  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={this.onChange} onBlur={this.submit} onPressEnter={this.submit}/></span>
        <span onClick={this.plus}><PlusOutlined /></span>
      </div>
    )
  }
}
 
export default MkCounter