├── .env ├── public ├── favicon.ico ├── manifest.json └── index.html ├── src ├── index.js ├── utils │ └── masks.js └── app.js ├── .gitignore ├── package.json └── README.md /.env: -------------------------------------------------------------------------------- 1 | BROWSER=false -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DieguinhoHR/react-js-masks/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactDOM from 'react-dom' 3 | import App from './app' 4 | 5 | ReactDOM.render(, document.getElementById('root')) -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-js-masks", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "npm": "^6.9.0", 7 | "react": "^16.8.6", 8 | "react-dom": "^16.8.6", 9 | "react-scripts": "3.0.1", 10 | "styled-components": "4.2.0", 11 | "update": "^0.7.4" 12 | }, 13 | "scripts": { 14 | "start": "react-scripts start", 15 | "build": "react-scripts build", 16 | "test": "react-scripts test", 17 | "eject": "react-scripts eject" 18 | }, 19 | "eslintConfig": { 20 | "extends": "react-app" 21 | }, 22 | "browserslist": { 23 | "production": [ 24 | ">0.2%", 25 | "not dead", 26 | "not op_mini all" 27 | ], 28 | "development": [ 29 | "last 1 chrome version", 30 | "last 1 firefox version", 31 | "last 1 safari version" 32 | ] 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/utils/masks.js: -------------------------------------------------------------------------------- 1 | function cpfMask (value) { 2 | return value 3 | .replace(/\D/g, '') 4 | .replace(/(\d{3})(\d)/, '$1.$2') 5 | .replace(/(\d{3})(\d)/, '$1.$2') 6 | .replace(/(\d{3})(\d{1,2})/, '$1-$2') 7 | .replace(/(-\d{2})\d+?$/, '$1') 8 | } 9 | 10 | function dataMask (value) { 11 | return value 12 | .replace(/\D/g, '') 13 | .replace(/(\d{2})(\d)/, '$1/$2') 14 | .replace(/(\d{2})(\d)/, '$1/$2') 15 | .replace(/(\/\d{4})\d+?$/, '$1') 16 | } 17 | 18 | function cnpjMask (value) { 19 | return value 20 | .replace(/\D/g, '') 21 | .replace(/(\d{2})(\d)/, '$1.$2') 22 | .replace(/(\d{3})(\d)/, '$1.$2') 23 | .replace(/(\d{3})(\d)/, '$1/$2') 24 | .replace(/(\d{4})(\d)/, '$1-$2') 25 | .replace(/(-\d{2})\d+?$/, '$1') 26 | } 27 | 28 | function phoneMask (value) { 29 | return value 30 | .replace(/\D/g, '') 31 | .replace(/(\d{2})(\d)/, '($1) $2') 32 | .replace(/(\d{4})(\d)/, '$1-$2') 33 | .replace(/(\d{4})-(\d)(\d{4})/, '$1$2-$3') 34 | .replace(/(-\d{4})\d+?$/, '$1') 35 | } 36 | 37 | function cepMask (value) { 38 | return value 39 | .replace(/\D/g, '') 40 | .replace(/(\d{5})(\d)/, '$1-$2') 41 | .replace(/(-\d{3})\d+?$/, '$1') 42 | } 43 | 44 | function pisMask (value) { 45 | return value 46 | .replace(/\D/g, '') 47 | .replace(/(\d{3})(\d)/, '$1.$2') 48 | .replace(/(\d{5})(\d)/, '$1.$2') 49 | .replace(/(\d{5}\.)(\d{2})(\d)/, '$1$2-$3') 50 | .replace(/(-\d{1})\d+?$/, '$1') 51 | } 52 | 53 | export { 54 | cpfMask, 55 | cnpjMask, 56 | phoneMask, 57 | cepMask, 58 | pisMask, 59 | dataMask, 60 | } 61 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 26 |
27 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react' 2 | import { 3 | cpfMask, 4 | cnpjMask, 5 | phoneMask, 6 | cepMask, 7 | pisMask, 8 | dataMask, 9 | } from './utils/masks' 10 | import styled from 'styled-components' 11 | 12 | function App() { 13 | const [cpf, setCPF] = useState('') 14 | const [cnpj, setCNPJ] = useState('') 15 | const [phone, setPhone] = useState('') 16 | const [zipcode, setZipcode] = useState('') 17 | const [pis, setPis] = useState('') 18 | const [data, setData] = useState('') 19 | 20 | return ( 21 | 22 |

Form com inputs masks

23 |
24 | 25 | setCPF(e.target.value)} 31 | /> 32 | 33 | setCNPJ(e.target.value)} 39 | /> 40 | 41 | setPhone(e.target.value)} 47 | /> 48 | 49 | setZipcode(e.target.value)} 55 | /> 56 | 57 | setPis(e.target.value)} 63 | /> 64 | 65 | setData(e.target.value)} 71 | /> 72 |
73 |
74 | ) 75 | } 76 | 77 | const Container = styled.div` 78 | margin: 0 auto; 79 | width: 700px; 80 | ` 81 | 82 | const Label = styled.label` 83 | margin-bottom: 50px; 84 | ` 85 | 86 | const Input = styled.input` 87 | margin: 0 auto; 88 | width: 700px; 89 | margin-bottom: 19px; 90 | border: 5px solid #CCC; 91 | ` 92 | 93 | export default App 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reactjs Masks 2 | 3 | Máscaras para formulário em React.js sem utilização de libs adicionais. 4 | 5 | Esse repositório contêm exemplo de inputs de textos com máscaras para: CPF, CNPJ, Telefone, PIS, CEP e Data. 6 | 7 | Para adicional explicações, veja [esse video](https://youtu.be/r-8isv_TnVA?list=TLPQMTYxMDIwMjFP9hVSVfWBpw). 8 | 9 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 10 | 11 | ## Available Scripts 12 | 13 | In the project directory, you can run: 14 | 15 | ### `npm start` 16 | 17 | Runs the app in the development mode.
18 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 19 | 20 | The page will reload if you make edits.
21 | You will also see any lint errors in the console. 22 | 23 | ### `npm test` 24 | 25 | Launches the test runner in the interactive watch mode.
26 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 27 | 28 | ### `npm run build` 29 | 30 | Builds the app for production to the `build` folder.
31 | It correctly bundles React in production mode and optimizes the build for the best performance. 32 | 33 | The build is minified and the filenames include the hashes.
34 | Your app is ready to be deployed! 35 | 36 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 37 | 38 | ### `npm run eject` 39 | 40 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 41 | 42 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 43 | 44 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 45 | 46 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 47 | 48 | ## Learn More 49 | 50 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 51 | 52 | To learn React, check out the [React documentation](https://reactjs.org/). 53 | 54 | ### Code Splitting 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 57 | 58 | ### Analyzing the Bundle Size 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 61 | 62 | ### Making a Progressive Web App 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 65 | 66 | ### Advanced Configuration 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 69 | 70 | ### Deployment 71 | 72 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 73 | 74 | ### `npm run build` fails to minify 75 | 76 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 77 | --------------------------------------------------------------------------------