Closed qicmsg closed 1 year ago
修改constructor
方法
const {value, max, min} = this.props;
this.state = {
value: value,
controlled: typeof value !== 'undefined',
totalWidth: 0,
touching: false,
ogX: 0,
touchID: false,
//percent: this.props.value ? parseInt(this.props.value / (this.props.max - this.props.min) * 100) : 0
percent: value ? (value - min) / (max - min) * 100 : 0
}
componentWillReceiveProps
方法
componentWillReceiveProps(nextProps) {
const {value, max, min} = nextProps;
if (this.state.controlled) {
if (
nextProps.value <= this.props.max &&
nextProps.value >= this.props.min
) {
//let percent = parseInt(nextProps.value / (this.props.max - this.props.min) * 100);
let percent = (value - min) / (max - min) * 100;
this.setState({value: value, percent})
}
}
}
修改countValue
方法
countValue(percent = this.state.percent, min = this.props.min, max = this.props.max) {
let value = 0
if (percent === 100) {
value = max
} else if (percent === 0) {
value = min
} else {
value = Math.round(min + (max - min) * percent / 100);
}
return value
}
修改handleTouchMove
方法
handleTouchMove(e) {
let {onChanging, max, min, step} = this.props
if (!this.state.touching || this.props.disabled) return
if (e.targetTouches[0].identifier !== this.state.touchId) return
// 阻止默认事件
e.preventDefault()
const pageX = e.targetTouches[0].pageX
const diffX = pageX - this.state.ogX
let percent = parseInt(diffX / this.state.totalWidth * 100) + this.state.ogPercent
percent = percent < 0 ? 0 : percent > 100 ? 100 : percent
// 计算一步的百分比比率
let p = 100 / ((max - min) / step)
// 计算步数
let _step = parseInt((percent + (p / 2)) / p)
percent = _step * p;
const value = this.countValue(percent)
if (value === this.state.value) return
this.setState(
{
percent,
value
},
() => {
Object.defineProperty(e, 'detail', {
enumerable: true,
value: {
detail: e.detail,
value: this.state.value
}
})
if (onChanging) onChanging(e)
}
)
}
官方看到自己修改一下bug吧。
直接提个 pr
相关平台
H5
浏览器版本: chrome 89 使用框架: React
复现步骤
当 slider 的min>0,max>100时,滑动时value值计算有问题。如:
期望结果
当第一次滑动时value=70
实际结果
第一次滑动value=10
环境信息