{"id":13422091,"url":"https://github.com/sanniassin/react-input-mask","last_synced_at":"2025-05-13T23:09:02.098Z","repository":{"id":33714185,"uuid":"37367784","full_name":"sanniassin/react-input-mask","owner":"sanniassin","description":"Input masking component for React. Made with attention to UX.","archived":false,"fork":false,"pushed_at":"2024-08-01T00:59:19.000Z","size":2618,"stargazers_count":2264,"open_issues_count":138,"forks_count":259,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-05-09T16:43:34.595Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sanniassin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-06-13T10:43:00.000Z","updated_at":"2025-05-06T16:12:36.000Z","dependencies_parsed_at":"2024-06-18T11:08:29.813Z","dependency_job_id":"5cdf5288-20ed-4cdf-9192-b3b6e232378b","html_url":"https://github.com/sanniassin/react-input-mask","commit_stats":{"total_commits":389,"total_committers":13,"mean_commits":"29.923076923076923","dds":0.6118251928020566,"last_synced_commit":"565475b8c55d5ccdd20dfe26a3a8e893f61a4c08"},"previous_names":[],"tags_count":48,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanniassin%2Freact-input-mask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanniassin%2Freact-input-mask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanniassin%2Freact-input-mask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanniassin%2Freact-input-mask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sanniassin","download_url":"https://codeload.github.com/sanniassin/react-input-mask/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253336925,"owners_count":21892798,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T23:00:36.749Z","updated_at":"2025-05-13T23:08:57.073Z","avatar_url":"https://github.com/sanniassin.png","language":"JavaScript","readme":"# react-input-mask\n\n[![Build Status](https://img.shields.io/travis/sanniassin/react-input-mask/master.svg?style=flat)](https://travis-ci.org/sanniassin/react-input-mask) [![npm version](https://img.shields.io/npm/v/react-input-mask.svg?style=flat)](https://www.npmjs.com/package/react-input-mask) [![npm downloads](https://img.shields.io/npm/dm/react-input-mask.svg?style=flat)](https://www.npmjs.com/package/react-input-mask)\n\nInput masking component for React. Made with attention to UX.\n\n**This is a development branch for version 3.0. For the latest stable version [see v2 branch](https://github.com/sanniassin/react-input-mask/tree/v2).**\n\n#### [Demo](http://sanniassin.github.io/react-input-mask/demo.html)\n\n# Table of Contents\n* [Installation](#installation)\n* [Usage](#usage)\n* [Properties](#properties)\n* [Known Issues](#known-issues)\n\n# Installation\n```npm install react-input-mask@next --save```\n\nreact-input-mask requires **React 16.8.0 or later.** If you need support for older versions, use [version 2](https://github.com/sanniassin/react-input-mask/tree/v2).\n\n# Usage\n```jsx\nimport React from \"react\"\nimport InputMask from \"react-input-mask\";\n\nfunction DateInput(props) {\n  return \u003cInputMask mask=\"99/99/9999\" onChange={props.onChange} value={props.value} /\u003e;\n}\n```\n\n# Properties\n|                           Name                            |               Type                | Default | Description |\n|        :-----------------------------------------:        |    :-------------------------:    | :-----: | :--------------------------------------------------------------------- |\n|                    **[`mask`](#mask)**                    | `{String\\|Array\u003cString, RegExp\u003e}` |         | Mask format |\n|         **[`maskPlaceholder`](#maskplaceholder)**         |            `{String}`             |   `_`   | Placeholder to cover unfilled parts of the mask |\n|          **[`alwaysShowMask`](#alwaysshowmask)**          |            `{Boolean}`            | `false` | Whether mask prefix and placeholder should be displayed when input is empty and has no focus |\n| **[`beforeMaskedStateChange`](#beforemaskedstatechange)** |            `{Function}`           |         | Function to modify value and selection before applying mask |\n|                **[`children`](#children)**                |          `{ReactElement}`         |         | Custom render function for integration with other input components |\n\n\n### `mask`\n\nMask format. Can be either a string or array of characters and regular expressions.\u003cbr /\u003e\u003cbr /\u003e\n\n\n```jsx\n\u003cInputMask mask=\"99/99/99\" /\u003e\n```\nSimple masks can be defined as strings. The following characters will define mask format:\n\n| Character | Allowed input |\n| :-------: | :-----------: |\n|     9     |      0-9      |\n|     a     |    a-z, A-Z   |\n|     *     | 0-9, a-z, A-Z |\n\nAny format character can be escaped with a backslash.\u003cbr /\u003e\u003cbr /\u003e\n\n\nMore complex masks can be defined as an array of regular expressions and constant characters.\n```jsx\n// Canadian postal code mask\nconst firstLetter = /(?!.*[DFIOQU])[A-VXY]/i;\nconst letter = /(?!.*[DFIOQU])[A-Z]/i;\nconst digit = /[0-9]/;\nconst mask = [firstLetter, digit, letter, \" \", digit, letter, digit];\nreturn \u003cInputMask mask={mask} /\u003e;\n```\n\n\n### `maskPlaceholder`\n```jsx\n// Will be rendered as 12/--/--\n\u003cInputMask mask=\"99/99/99\" maskPlaceholder=\"-\" value=\"12\" /\u003e\n\n// Will be rendered as 12/mm/yy\n\u003cInputMask mask=\"99/99/99\" maskPlaceholder=\"dd/mm/yy\" value=\"12\" /\u003e\n\n// Will be rendered as 12/\n\u003cInputMask mask=\"99/99/99\" maskPlaceholder={null} value=\"12\" /\u003e\n```\nCharacter or string to cover unfilled parts of the mask. Default character is \"\\_\". If set to `null` or empty string, unfilled parts will be empty as in a regular input.\n\n\n### `alwaysShowMask`\n\nIf enabled, mask prefix and placeholder will be displayed even when input is empty and has no focus.\n\n\n### `beforeMaskedStateChange`\nIn case you need to customize masking behavior, you can provide `beforeMaskedStateChange` function to change masked value and cursor position before it's applied to the input.\n\nIt receieves an object with `previousState`, `currentState` and `nextState` properties. Each state is an object with `value` and `selection` properites where `value` is a string and selection is an object containing `start` and `end` positions of the selection.\n1. **previousState:** Input state before change. Only defined on `change` event.\n2. **currentState:** Current raw input state. Not defined during component render.\n3. **nextState:** Input state with applied mask. Contains `value` and `selection` fields.\n\nSelection positions will be `null` if input isn't focused and during rendering.\n\n`beforeMaskedStateChange` must return a new state with `value` and `selection`.\n\n```jsx\n// Trim trailing slashes\nfunction beforeMaskedStateChange({ nextState }) {\n  let { value } = nextState;\n  if (value.endsWith(\"/\")) {\n    value = value.slice(0, -1);\n  }\n\n  return {\n    ...nextState,\n    value\n  };\n}\n\nreturn \u003cInputMask mask=\"99/99/99\" maskPlaceholder={null} beforeMaskedStateChange={beforeMaskedStateChange} /\u003e;\n```\n\nPlease note that `beforeMaskedStateChange` executes more often than `onChange` and must be pure.\n\n\n### `children`\nTo use another component instead of regular `\u003cinput /\u003e` provide it as children. The following properties, if used, should always be defined on the `InputMask` component itself: `onChange`, `onMouseDown`, `onFocus`, `onBlur`, `value`, `disabled`, `readOnly`.\n```jsx\nimport React from 'react';\nimport InputMask from 'react-input-mask';\nimport MaterialInput from '@material-ui/core/Input';\n\n// Will work fine\nfunction Input(props) {\n  return (\n    \u003cInputMask mask=\"99/99/9999\" value={props.value} onChange={props.onChange}\u003e\n      \u003cMaterialInput type=\"tel\" disableUnderline /\u003e\n    \u003c/InputMask\u003e\n  );\n}\n\n// Will throw an error because InputMask's and children's onChange props aren't the same\nfunction InvalidInput(props) {\n  return (\n    \u003cInputMask mask=\"99/99/9999\" value={props.value}\u003e\n      \u003cMaterialInput type=\"tel\" disableUnderline onChange={props.onChange} /\u003e\n    \u003c/InputMask\u003e\n  );\n}\n```\n\n# Known Issues\n### Autofill\nBrowser's autofill requires either empty value in input or value which exactly matches beginning of the autofilled value. I.e. autofilled value \"+1 (555) 123-4567\" will work with \"+1\" or \"+1 (5\", but won't work with \"+1 (\\_\\_\\_) \\_\\_\\_-\\_\\_\\_\\_\" or \"1 (555)\". There are several possible solutions:\n1. Set `maskChar` to null and trim space after \"+1\" with `beforeMaskedStateChange` if no more digits are entered.\n2. Apply mask only if value is not empty. In general, this is the most reliable solution because we can't be sure about formatting in autofilled value.\n3. Use less formatting in the mask.\n\nPlease note that it might lead to worse user experience (should I enter +1 if input is empty?). You should choose what's more important to your users — smooth typing experience or autofill. Phone and ZIP code inputs are very likely to be autofilled and it's a good idea to care about it, while security confirmation code in two-factor authorization shouldn't care about autofill at all.\n\n### Cypress tests\nThe following sequence could fail\n```js\ncy.get(\"input\")\n  .focus()\n  .type(\"12345\")\n  .should(\"have.value\", \"12/34/5___\"); // expected \u003cinput\u003e to have value 12/34/5___, but the value was 23/45/____\n````\n\nSince [focus is not an action command](https://docs.cypress.io/api/commands/focus.html#Focus-is-not-an-action-command), it behaves differently than the real user interaction and, therefore, less reliable.\n\nThere is a few possible workarounds\n```js\n// Start typing without calling focus() explicitly.\n// type() is an action command and focuses input anyway\ncy.get(\"input\")\n  .type(\"12345\")\n  .should(\"have.value\", \"12/34/5___\");\n\n// Use click() instead of focus()\ncy.get(\"input\")\n  .click()\n  .type(\"12345\")\n  .should(\"have.value\", \"12/34/5___\");\n\n// Or wait a little after focus()\ncy.get(\"input\")\n  .focus()\n  .wait(50)\n  .type(\"12345\")\n  .should(\"have.value\", \"12/34/5___\");\n````\n\n# Thanks\nThanks to [BrowserStack](https://www.browserstack.com/) for the help with testing on real devices\n","funding_links":[],"categories":["UI Components","Uncategorized","JavaScript","Typescript / Javascript","Demos","\u003csummary\u003eUI Components\u003c/summary\u003e","UI components"],"sub_categories":["Form Components","Uncategorized","Masked input"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanniassin%2Freact-input-mask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanniassin%2Freact-input-mask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanniassin%2Freact-input-mask/lists"}