简体   繁体   English

React 中屏蔽卡号输入

[英]Mask card number input in React

I'm learning React and want to make an input with two constraints:我正在学习 React 并希望使用两个约束进行输入:

  • 16 numbers, 16个数字,
  • put a space after every fourth.每四个之后放一个空格。
import React, { Component } from 'react';

export default class CardNumberInput extends Component {
  constructor() {
    super();
    this.state = { value: '' };
  }

  handleChange(event) {
    React.findDOMNode(this.refs.cardInput).mask("0000 0000 0000 0000");
    this.setState({ value: event.target.value });
  }

  render() {
    let value = this.state.value;

    return (
      <div>
        <label htmlFor="cardInput">Card Number: </label>
        <input ref="cardInput" id="cardInput" onChange={this.handleChange} type="number" value={value} />
      </div>
    );
  }
}

I don't know whether I'm doing it right (use refs), because console.log(React.findDOMNode(this.refs.cardInput)) returns null o_O我不知道我是否做得对(使用 refs),因为 console.log(React.findDOMNode(this.refs.cardInput)) 返回 null o_O

ps .mask is from http://igorescobar.github.io/jQuery-Mask-Plugin/ ps .mask 来自http://igorescobar.github.io/jQuery-Mask-Plugin/

The mask function must be applied to a jquery object, not a plain dom element, and you also need to put this line in componentDidMount. 必须将mask函数应用于jquery对象,而不是普通的dom元素,并且还需要将此行放在componentDidMount中。

componentDidMount: function () {
    var $input_elem = $(React.findDOMNode(this.refs.cardInput));
    // now you have a jquery object
    $input_elem.mask("0000 0000 0000 0000", callback_options);
}

However, the callback options for this plugin still need to be integrated with the react lifecycle methods. 但是,此插件的回调选项仍需要与react生命周期方法集成。 First try making it an uncontrolled component (use defaultValue instead of value) and check that it works. 首先尝试使它成为一个不受控制的组件(使用defaultValue而不是value)并检查它是否有效。

You are rendering this.state.value every time you make a change, which is overwriting the mask. 每次进行更改时都会渲染this.state.value,这会覆盖蒙版。

The mask is being overwritten by the render. 渲染覆盖了掩码。

You need to move the mask() to render so that it masks the value before it writes to the dom. 您需要移动mask()进行渲染,以便在写入dom之前屏蔽该值。

Computed data: Don't worry about precomputing values based on state — it's easier to ensure that your UI is consistent if you do all computation within render(). 计算数据:不要担心基于状态预先计算值 - 如果在render()中进行所有计算,则更容易确保UI是一致的。 For example, if you have an array of list items in state and you want to render the count as a string, simply render this.state.listItems.length + ' list items' in your render() method rather than storing it on state. 例如,如果状态中有一个列表项数组,并且您希望将计数呈现为字符串,则只需在render()方法中呈现this.state.listItems.length +'list items',而不是将其存储在状态中。 https://facebook.github.io/react/docs/interactivity-and-dynamic-uis.html https://facebook.github.io/react/docs/interactivity-and-dynamic-uis.html

I created a package that exposes an input component that displays a masked value according to the mask it receives.我创建了一个,它公开一个输入组件,该组件根据它接收到的掩码显示掩码值。

The mask will change keeping the cursor at the correct position (even if you change part of the value in the middle of the input, paste some characters, or delete a part of it, and even if the mask changes).掩码会发生变化,将光标保持在正确的位置(即使您更改了输入中间的部分值,粘贴了一些字符,或删除了其中的一部分,即使掩码发生了变化)。

You can see a Demo with examples at:您可以在以下位置查看带有示例的演示:

https://lucasbasquerotto.github.io/react-masked-input https://lucasbasquerotto.github.io/react-masked-input

You can see in the 1st example a mask similar to a credit card, and the value your receive doesn't have mask (only the display value).您可以在第一个示例中看到类似于信用卡的掩码,并且您收到的值没有掩码(只有显示值)。 Just change the mask to 9999 9999 9999 9999 and it will work for your case.只需将掩码更改为9999 9999 9999 9999 ,它将适用于您的情况。

To install the package: npm i react-hook-mask安装包: npm i react-hook-mask

You can use dynamic masks if later you want to change the credit card mask based on the value (because some credit cards have different masks).如果稍后您想根据值更改信用卡掩码,则可以使用动态掩码(因为某些信用卡具有不同的掩码)。

I created a demo with an example according to what you asked (it uses the mask 9999 9999 9999 9999 ):我根据您的要求创建了一个带有示例的演示(它使用掩码9999 9999 9999 9999 ):

https://codesandbox.io/s/credit-card-mask-8f3w8?file=/src/index.js https://codesandbox.io/s/credit-card-mask-8f3w8?file=/src/index.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM