Skip to content

Commit b29ea06

Browse files
committed
docs: add input mask examples
1 parent 08ffea9 commit b29ea06

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
title: React Input Mask
3+
name: Input mask
4+
description: React input masks allow you to regulate the format of data entered. Yyou can enhance data entry accuracy by implementing react input masks for fields with consistent formatting requirements. This ensures that users input data correctly, such as accurately formatted phone numbers in a designated phone number field.
5+
menu: Forms
6+
route: /forms/input-mask
7+
---
8+
9+
import { CFormInput } from '@coreui/react/src/index'
10+
11+
import { IMaskMixin } from 'react-imask'
12+
13+
## Usage
14+
15+
While CoreUI for React does not currently include a 'mask' feature, it can be enhanced by leveraging existing libraries like `react-imask` to extend the functionality of our components.
16+
17+
To enable the extension of the `<CFormInput />` component, please ensure that you have installed `react-imask` first.
18+
19+
```bash
20+
npm install react-imask
21+
```
22+
23+
or
24+
25+
```bash
26+
yarn add react-imask
27+
```
28+
29+
Once you have installed react-imask, you can proceed to create a custom component with a mask.
30+
31+
```jsx
32+
import { IMaskMixin } from 'react-imask'
33+
```
34+
35+
```jsx
36+
const CFormInputMasked = IMaskMixin(({ inputRef, ...props }) => (
37+
<CFormInput
38+
{...props}
39+
ref={inputRef} // bind internal input
40+
/>
41+
))
42+
```
43+
44+
Here is an example of a date mask for reference.
45+
46+
export const MaskExample = () => {
47+
const CFormInputMasked = IMaskMixin(({ inputRef, ...props }) => (
48+
<CFormInput {...props} ref={inputRef} />
49+
))
50+
return (
51+
<CFormInputMasked
52+
mask={Date}
53+
min={new Date(1990, 0, 1)}
54+
max={new Date(2020, 0, 1)}
55+
lazy={false}
56+
/>
57+
)
58+
}
59+
60+
<Example>
61+
<MaskExample />
62+
</Example>
63+
64+
```jsx
65+
const CFormInputMasked = IMaskMixin(({ inputRef, ...props }) => (
66+
<CFormInput {...props} ref={inputRef} />
67+
))
68+
return (
69+
<CFormInputMasked
70+
mask={Date}
71+
min={new Date(1990, 0, 1)}
72+
max={new Date(2020, 0, 1)}
73+
lazy={false}
74+
/>
75+
)
76+
```
77+
78+
## Patterns
79+
80+
Please take a moment to review some example masks you can use for reference. These examples serve as helpful templates for implementing input masks and can assist you in effectively controlling data entry formats.
81+
82+
### Phone
83+
84+
export const PhoneMaskExample = () => {
85+
const CFormInputMasked = IMaskMixin(({ inputRef, ...props }) => (
86+
<CFormInput {...props} ref={inputRef} />
87+
))
88+
return <CFormInputMasked mask="+{1}(000)000-00-00" />
89+
}
90+
91+
<Example>
92+
<PhoneMaskExample />
93+
</Example>
94+
95+
```jsx
96+
const CFormInputMasked = IMaskMixin(({ inputRef, ...props }) => (
97+
<CFormInput {...props} ref={inputRef} />
98+
))
99+
return (
100+
return <CFormInputMasked mask="+{1}(000)000-00-00" />
101+
)
102+
```
103+
104+
### Credit Card
105+
106+
export const CreditCardMaskExample = () => {
107+
const CFormInputMasked = IMaskMixin(({ inputRef, ...props }) => (
108+
<CFormInput {...props} ref={inputRef} />
109+
))
110+
return <CFormInputMasked mask="0000 0000 0000 0000" />
111+
}
112+
113+
<Example>
114+
<CreditCardMaskExample />
115+
</Example>
116+
117+
```jsx
118+
const CFormInputMasked = IMaskMixin(({ inputRef, ...props }) => (
119+
<CFormInput {...props} ref={inputRef} />
120+
))
121+
return (
122+
return <CFormInputMasked mask="0000 0000 0000 0000" />
123+
)
124+
```
125+
126+
For more information on how to use the input mask, you can refer to the following resource: https://imask.js.org/

packages/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"react-docgen-typescript": "^2.2.2",
5858
"react-dom": "^18.2.0",
5959
"react-helmet": "^6.1.0",
60+
"react-imask": "^7.0.1",
6061
"rimraf": "^5.0.1",
6162
"sass": "^1.63.4"
6263
},

packages/docs/src/nav.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ const nav = [
116116
name: 'Input',
117117
to: '/forms/input/',
118118
},
119+
{
120+
name: 'Input Mask',
121+
to: '/forms/input-mask/',
122+
},
119123
{
120124
name: 'Input Group',
121125
to: '/forms/input-group/',

0 commit comments

Comments
 (0)