Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gabrielnafuzi/brazilian-masks

simple brazilian masks to use on day to day (CNPJ, CPF, CEP, Phone, PIS)
https://github.com/gabrielnafuzi/brazilian-masks

typescript

Last synced: 12 days ago
JSON representation

simple brazilian masks to use on day to day (CNPJ, CPF, CEP, Phone, PIS)

Awesome Lists containing this project

README

        

# Brazilian Masks

Some masks commonly used in forms in Brazil

## Available Masks

- phone (dynamically changes)
- cnpj
- cpf
- cpfOrCnpj (dynamically changes)
- cep
- pis

## Utility functions

- clear (remove all non-numeric characters)

## Installation

```sh
npm i @nafuzi/brazilian-masks
```

or

```sh
yarn add @nafuzi/brazilian-masks
```

### Simple use (Vanilla JS)

```js
import { clear, cep } from '@nafuzi/brazilian-masks'

const cepInput = document.getElementById('cep-input')

cepInput.addEventListener('input', (event) => {
const { value } = event.target

cepInput.value = cep(value)
})
```

### React (TypeScript)

```tsx
// MaskedInput.tsx
import type { ChangeEvent, HTMLProps } from 'react'
import masks, { type MasksName } from '@nafuzi/brazilian-masks'

type MaskedInputProps = {
mask: MasksName
onChange: (maskedValue: string) => void
} & Omit, 'onChange'>

export const MaskedInput = ({
name,
value,
onChange,
mask,
...rest
}: MaskedInputProps) => {
const handleOnChange = (event: ChangeEvent) => {
const maskedValue = masks[mask](event.target.value)

onChange(maskedValue)
}

return
}

// App.tsx
import { useState } from 'react'

import { MaskedInput } from './components/MaskedInput'

const App = () => {
const [cpf, setCpf] = useState('')

return (
setCpf(value)}
mask="cpf"
/>
)
}

export default App
```

## Contributing

Feel free to create issues and open PR's with features, improvements, bugfix, etc.

## License

MIT