Skip to content

Commit

Permalink
Implement CPF mask
Browse files Browse the repository at this point in the history
Related to issue #4

Signed-off-by: antoniocoj <[email protected]>
Signed-off-by: Lucas Amoêdo <[email protected]>
  • Loading branch information
LucasAmoedo committed Apr 29, 2017
1 parent f858791 commit 3801639
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Categories/Owners/OwnerCreate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react';
import $ from 'jquery';
import { change } from 'redux-form';
import { formatCPF } from '../Util';
import {
Create, SelectInput, SimpleForm, TextInput,
} from 'admin-on-rest/lib/mui';
Expand Down Expand Up @@ -108,6 +109,7 @@ export class OwnerCreate extends Component {
source="cpf"
label="CPF"
validate={required}
normalize={formatCPF}
/>
<TextInput
source="ownerName"
Expand Down
23 changes: 23 additions & 0 deletions src/Categories/Util/formatCPF.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const formatCPF = (cpf, prevCpf) => {
if (!cpf) {
return cpf;
}

const CPFNumbersOnly = cpf.replace(/[^\d]/g, '');
if (!prevCpf || cpf.length > prevCpf.length) {
if (CPFNumbersOnly.length === 3)
return `${CPFNumbersOnly}.`;
if (CPFNumbersOnly.length === 6)
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.`;
if (CPFNumbersOnly.length === 9)
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.${CPFNumbersOnly.slice(6,9)}-`;
}
if (CPFNumbersOnly.length <= 3)
return `${CPFNumbersOnly}`;
if (CPFNumbersOnly.length <= 6)
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3)}`;
if (CPFNumbersOnly.length <= 9)
return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.${CPFNumbersOnly.slice(6)}`;

return `${CPFNumbersOnly.slice(0,3)}.${CPFNumbersOnly.slice(3,6)}.${CPFNumbersOnly.slice(6,9)}-${CPFNumbersOnly.slice(9,11)}`;
}
1 change: 1 addition & 0 deletions src/Categories/Util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { formatCPF } from './formatCPF';

0 comments on commit 3801639

Please sign in to comment.