daily-share icon indicating copy to clipboard operation
daily-share copied to clipboard

处理虚拟键盘,输入(4.00) 失效的问题如何解决(2020-1-14)

Open yaogengzhu opened this issue 5 years ago • 0 comments

处理虚拟键盘,输入(4.00) 失效的问题如何解决

背景: 在用户输入数字,处理虚拟键盘,输入4.00 ===> 变成 4 ,可以输入4.33 等 bug产生原因: 因为: (4.0).toString() // "4"

解决方案,在处理键盘数据时,统一处理成字符串,严禁在输入时有数字参与


const strategys = new Map([
                [/^\d$/, (keyCode: string) => {
                    const prev = tmpStairPrice[0].sale_price
                    console.log(prev)
                    if (/^\d{0,4}(\.\d{0,2})?$/.test((prev).toString() + (keyCode))) {
                        const price = prev.toString() + keyCode

                        tmpStairPrice.forEach(((item, index) => {
                            if (index === 0 ) {
                                // 禁止处理成数字
                                item.sale_price = price
                                item.threshold = 0
                            }
                        }))
                        this.setState({
                         // 禁止处理成数字
                            tmpStairPrice: tmpStairPrice
                        })
                    }
                }],
                [/(dot)/, () => {
                    let prev = tmpStairPrice[0].sale_price
                    if (/^\d{1,4}(\.\d{0,2})?$/.test((prev).toString() + '.')) {
                        // setTempGoodsPrice((prev).toString() + '.')
                        prev = String(prev)
                        tmpStairPrice.forEach(((item, index) => {
                            if (index === 0 ) {
                                item.sale_price = (prev).toString() + '.'
                            }
                        }))

                        this.setState({
                              // 禁止处理成数字
                            tmpStairPrice: tmpStairPrice
                        })
                    }
                }],
                [/(delete)/, () => {
                    let prev = tmpStairPrice[0].sale_price
                    if (prev.toString().length > 0) {
                        prev = String(prev)
                        // setTempGoodsPrice(prev.substr(0, prev.length - 1))
                        tmpStairPrice.forEach(((item, index) => {
                            if (index === 0 ) {
                                item.sale_price = prev.substr(0, prev.length - 1)
                            }
                        }))

                        this.setState({
                           // 禁止处理成数字
                            tmpStairPrice: tmpStairPrice
                        })
                    }
                }]
            ])
            strategys.forEach((fn, reg) => {
                if (reg.test(code)) {
                    fn(code)
                }
            })

yaogengzhu avatar Jan 14 '20 04:01 yaogengzhu