Skip to content

Commit 086c07e

Browse files
committed
feat: use-input-mask
0 parents  commit 086c07e

File tree

15 files changed

+494
-0
lines changed

15 files changed

+494
-0
lines changed

.gitignore

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# TypeScript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
# next.js build output
61+
.next
62+
.docz
63+
dist
64+
build
65+
66+
.DS_Store
67+
package-lock.json
68+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=http://registry.npmjs.org/

.prettierrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"printWidth": 80,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": false,
6+
"singleQuote": true,
7+
"trailingComma": "all",
8+
"bracketSpacing": true,
9+
"jsxBracketSameLine": false,
10+
"proseWrap": "always"
11+
}

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2019 Kent C. Dodds
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# use-input-mask
2+
3+
![MIT License][license-badge]
4+
[![downloads][downloads-badge]][npmcharts]
5+
[![PRs Welcome][prs-badge]][prs]
6+
7+
## Installation
8+
9+
```bash
10+
$ yarn add use-input-mask
11+
# or
12+
$ npm i use-input-mask
13+
```
14+
15+
## Usage
16+
17+
```jsx
18+
import React from 'react'
19+
20+
import useInputMask from 'use-input-mask'
21+
22+
import { createAutoCorrectedDatePipe } from "text-mask-addons";
23+
24+
const MyDateInput = props => {
25+
const input = useRef(null);
26+
27+
const autoCorrectedDatePipe = createAutoCorrectedDatePipe("dd/mm/yyyy HH:MM");
28+
29+
const onChange = useInputMask({
30+
input,
31+
onChange: props.onChange,
32+
mask: [/\d/, /\d/, "/", /\d/, /\d/, "/", /\d/, /\d/, /\d/, /\d/],
33+
pipe: autoCorrectedDatePipe,
34+
keepCharPositions: true
35+
});
36+
37+
return <input {...props} ref={input} onChange={onChange} />;
38+
};
39+
40+
export default MyDateInput
41+
```
42+
43+
## Roadmap
44+
* [ ] tests
45+
* [ ] ci/cd
46+
* [ ] semantic-release
47+
* [ ] docs
48+
* [ ] all-contributors
49+
50+
## Inspiration
51+
52+
[text-mask](https://github.com/text-mask/text-mask)
53+
54+
## LIENSE
55+
56+
MIT
57+
58+
[license-badge]: https://img.shields.io/npm/l/use-input-mask.svg?style=flat-square
59+
[downloads-badge]: https://img.shields.io/npm/dm/use-input-mask.svg?style=flat-square
60+
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
61+
[prs]: http://makeapullrequest.com
62+
[npmcharts]: http://npmcharts.com/compare/use-input-mask

docs/Currency.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React, { useRef } from 'react'
2+
3+
import useInputMask from '../src'
4+
5+
import { createNumberMask } from 'text-mask-addons'
6+
7+
const CurrencyInput = props => {
8+
const input = useRef(null)
9+
10+
const maskMoney = createNumberMask({
11+
prefix: 'R$ ',
12+
includeThousandsSeparator: true,
13+
thousandsSeparatorSymbol: '.',
14+
allowDecimal: true,
15+
integerLimit: 9,
16+
decimalSymbol: ',',
17+
requireDecimal: true,
18+
})
19+
20+
const onChange = useInputMask({
21+
input,
22+
onChange: props.onChange,
23+
mask: value => {
24+
const mask = maskMoney(value)
25+
26+
const decimalsRegex = /,([0-9]{1,2})/
27+
const result = decimalsRegex.exec(value)
28+
29+
if (!!result && result[1].length < 2) {
30+
mask.push('0')
31+
} else if (!result) {
32+
mask.push('00')
33+
}
34+
35+
return mask
36+
},
37+
})
38+
39+
return <input {...props} ref={input} onChange={onChange} />
40+
}
41+
42+
export default CurrencyInput

docs/Currency.mdx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
name: Currency
3+
menu: Examples
4+
---
5+
6+
import { Playground } from 'docz'
7+
8+
import CurrencyInput from './Currency'
9+
10+
# Currency
11+
12+
<Playground>
13+
<CurrencyInput
14+
onChange={e => console.log(e.target.value)}
15+
placeholder="Currency input"
16+
/>
17+
</Playground>
18+
19+
## Code
20+
21+
```js
22+
import React, { useRef } from 'react'
23+
24+
import useInputMask from 'use-input-mask'
25+
26+
import { createNumberMask } from 'text-mask-addons'
27+
28+
const CurrencyInput = props => {
29+
const input = useRef(null)
30+
31+
const maskMoney = createNumberMask({
32+
prefix: 'R$ ',
33+
includeThousandsSeparator: true,
34+
thousandsSeparatorSymbol: '.',
35+
allowDecimal: true,
36+
integerLimit: 9,
37+
decimalSymbol: ',',
38+
requireDecimal: true,
39+
})
40+
41+
const onChange = useInputMask({
42+
input,
43+
onChange: props.onChange,
44+
mask: value => {
45+
const mask = maskMoney(value)
46+
47+
const decimalsRegex = /,([0-9]{1,2})/
48+
const result = decimalsRegex.exec(value)
49+
50+
if (!!result && result[1].length < 2) {
51+
mask.push('0')
52+
} else if (!result) {
53+
mask.push('00')
54+
}
55+
56+
return mask
57+
},
58+
})
59+
60+
return <input {...props} ref={input} onChange={onChange} />
61+
}
62+
63+
export default CurrencyInput
64+
```

docs/Date.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { useRef } from 'react'
2+
3+
import useInputMask from '../src'
4+
5+
import { createAutoCorrectedDatePipe } from 'text-mask-addons'
6+
7+
const DateInput = props => {
8+
const input = useRef(null)
9+
10+
const autoCorrectedDatePipe = createAutoCorrectedDatePipe('dd/mm/yyyy HH:MM')
11+
12+
const onChange = useInputMask({
13+
input,
14+
onChange: props.onChange,
15+
mask: [/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/],
16+
pipe: autoCorrectedDatePipe,
17+
placeholder: 'Please enter a date',
18+
keepCharPositions: true,
19+
})
20+
21+
return <input {...props} ref={input} onChange={onChange} />
22+
}
23+
24+
export default DateInput

docs/Date.mdx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
name: Date
3+
menu: Examples
4+
---
5+
6+
import { Playground } from 'docz'
7+
8+
import DateInput from './Date'
9+
10+
# Date
11+
12+
<Playground>
13+
<DateInput
14+
onChange={e => console.log(e.target.value)}
15+
placeholder="Date input"
16+
/>
17+
</Playground>
18+
19+
## Code
20+
21+
```js
22+
import React, { useRef } from 'react'
23+
24+
import useInputMask from 'use-input-mask'
25+
26+
import { createAutoCorrectedDatePipe } from 'text-mask-addons'
27+
28+
const DateInput = props => {
29+
const input = useRef(null)
30+
31+
const autoCorrectedDatePipe = createAutoCorrectedDatePipe('dd/mm/yyyy HH:MM')
32+
33+
const onChange = useInputMask({
34+
input,
35+
onChange: props.onChange,
36+
mask: [/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/],
37+
pipe: autoCorrectedDatePipe,
38+
placeholder: 'Please enter a date',
39+
keepCharPositions: true,
40+
})
41+
42+
return <input {...props} ref={input} onChange={onChange} />
43+
}
44+
45+
export default DateInput
46+
```

docs/Home.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
name: Home
3+
route: /
4+
---
5+
6+
# Getting Started
7+
8+
https://github.com/elsangedy/use-input-mask
9+
10+
https://github.com/text-mask/text-mask
11+
12+
## Install
13+
14+
```bash
15+
yarn add use-input-mask
16+
```
17+
18+
## Hook
19+
20+
```js
21+
import useInputMask from 'use-input-mask'
22+
23+
const onChange = useInputMask({
24+
input,
25+
onChange,
26+
guide,
27+
keepCharPositions,
28+
mask,
29+
pipe,
30+
placeholderChar,
31+
showMask,
32+
initialValue,
33+
})
34+
```

0 commit comments

Comments
 (0)