Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当 input 的 type 为 digit 时,无法手动改变 value 的值(无法满足 input 框输入最多两位小数这个需求) #11759

Closed
zhangqiongyue opened this issue May 5, 2022 · 3 comments
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@zhangqiongyue
Copy link

相关平台

微信小程序

小程序基础库: 2.22.1
使用框架: React

复现步骤

微信小程序/字节小程序无法满足需求input框输入最多两位小数这个需求,我的实现是:
// digitValue 无法改变输入框的值

const [digitValue, setDigitValue] = useState();
const handleChange = e => {
let newVal = e.detail.value;
if (e.detail.value.indexOf('.') > -1) {
newVal = ${Math.floor(Number(e.detail.value) * 100) / 100};
}
setDigitValue(newVal);
}

期望结果

input的框的值可以被人为改变

实际结果

input的框的值不能被改变

环境信息

👽 Taro v3.4.2


  Taro CLI 3.4.2 environment info:
    System:
      OS: macOS 12.1
      Shell: 5.8 - /bin/zsh
    Binaries:
      Node: 14.19.0 - /usr/local/opt/node@14/bin/node
      Yarn: 1.22.17 - /usr/local/bin/yarn
      npm: 6.14.16 - /usr/local/opt/node@14/bin/npm
    npmPackages:
      @tarojs/cli: 3.4.1 => 3.4.1
      @tarojs/components: 3.4.1 => 3.4.1
      @tarojs/mini-runner: 3.4.1 => 3.4.1
      @tarojs/plugin-platform-alipay: 3.4.1 => 3.4.1
      @tarojs/plugin-platform-jd: 3.4.1 => 3.4.1
      @tarojs/plugin-platform-qq: 3.4.1 => 3.4.1
      @tarojs/plugin-platform-swan: 3.4.1 => 3.4.1
      @tarojs/plugin-platform-tt: 3.4.1 => 3.4.1
      @tarojs/plugin-platform-weapp: 3.4.1 => 3.4.1
      @tarojs/react: 3.4.1 => 3.4.1
      @tarojs/runtime: 3.4.1 => 3.4.1
      @tarojs/taro: 3.4.1 => 3.4.1
      @tarojs/webpack-runner: 3.4.1 => 3.4.1
      babel-preset-taro: 3.4.1 => 3.4.1
      eslint-config-taro: 3.4.1 => 3.4.1
      react: ^17.0.2 => 17.0.2
@taro-bot2 taro-bot2 bot added F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels May 5, 2022
@TheKonka
Copy link
Member

TheKonka commented May 5, 2022

#2642

@zhangqiongyue
Copy link
Author

@OldKwan
Copy link

OldKwan commented Mar 19, 2024

遇到相似的问题, type="digit"且 controlled的时候赋值异样
我是 hooks写法, 暂时方案是 "类型转换"

    onInput={(e) => {
              const val = e.detail.value;
              if (Number(val) > 100) {
                setDensityInner(typeof densityInner === 'number' ? '100' : (100 as any));
                return;
              }
              if (val.includes('.') && val.split('.')[1].length > 2) {
                console.log('kwan: 123', densityInner);
                /**
                 * notes
                 * 疑似 Taro bug, 无法直接设置小数点位数
                 * 临时方案: 转换类型
                 */
                if (typeof densityInner === 'number') {
                  setDensityInner(String(densityInner));
                }
                if (typeof densityInner === 'string') {
                  setDensityInner(Number(densityInner) as any);
                }
                return;
              }
              setDensityInner(val);
            }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
None yet
Development

No branches or pull requests

3 participants