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
|