Skip to content

Commit ee8239a

Browse files
committed
new config builder and lang support
Signed-off-by: danieloprado <[email protected]>
1 parent be81b69 commit ee8239a

File tree

12 files changed

+1076
-114
lines changed

12 files changed

+1076
-114
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"typescript.tsdk": "node_modules/typescript/lib",
3-
"tslint.enable": false,
43
"files.exclude": {
54
"**/.git": true,
65
"**/.svn": true,
@@ -10,6 +9,9 @@
109
"**/*.js": {
1110
"when": "$(basename).ts"
1211
},
12+
"**/*.d.ts": {
13+
"when": "$(basename).ts"
14+
},
1315
"**/*?.js": {
1416
"when": "$(basename).tsx"
1517
},

components/FieldCoreBase.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default abstract class FieldCoreBase<
5252

5353
if (!mask) {
5454
mask = { apply: (v: string) => v, clean: v => v };
55-
this.props.mask && console.warn(`@react-form-fields/core: Mask '${this.props.mask}' not found`)
55+
this.props.mask && console.warn(`@react-form-fields/core: Mask '${this.props.mask}' not found`);
5656
}
5757

5858
return mask;

components/ValidationContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ export default class ValidationContext extends React.PureComponent<{}> {
3737
<FieldValidation.Provider value={this.registerFields}>
3838
{this.props.children}
3939
</FieldValidation.Provider>
40-
)
40+
);
4141
}
4242
}

components/ValidationContextRegister.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ export default class ValidationContextRegister extends React.PureComponent<IProp
3232
<FieldValidation.Consumer>
3333
{this.setContext}
3434
</FieldValidation.Consumer>
35-
)
35+
);
3636
}
3737
}

config/builder.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ErrorMessages } from 'validatorjs';
2+
3+
import { IConfig } from '.';
4+
import { IMask } from '../mask';
5+
6+
export default class ConfigBuilder {
7+
protected config: IConfig;
8+
9+
constructor() {
10+
this.clean();
11+
}
12+
13+
public addMask(name: string, apply: IMask['apply'], clean: IMask['clean']) {
14+
this.config.masks = [...this.config.masks, { name, apply, clean }];
15+
return this;
16+
}
17+
18+
public addMasks(masks: IMask[]) {
19+
this.config.masks = [...this.config.masks, ...masks];
20+
return this;
21+
}
22+
23+
public addCustomMessages(lang: string, customMessages: ErrorMessages) {
24+
this.config.validation = { lang, customMessages };
25+
return this;
26+
}
27+
28+
public build() {
29+
return this.config;
30+
}
31+
32+
public clean() {
33+
this.config = { masks: [], validation: null };
34+
return this;
35+
}
36+
}

config.ts renamed to config/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { ErrorMessages } from 'validatorjs';
22

3-
import { IMask, register as registerMasks } from './mask';
4-
import * as validator from './validator';
3+
import { IMask, register as registerMasks } from '../mask';
4+
import * as validator from '../validator';
55

66
let config: IConfig = {};
77

88
export interface IConfig {
9-
masks?: IMask[],
9+
masks?: IMask[];
1010
validation?: {
1111
lang: string;
1212
customMessages?: ErrorMessages;
13-
}
13+
};
1414
}
1515

1616
export function getConfig(): IConfig {

lang/pt-br/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { IConfig } from '../../config';
2+
import masks from './masks';
3+
import validation from './validation';
4+
5+
const langPTBR: IConfig = {
6+
masks,
7+
validation
8+
};
9+
10+
export default langPTBR;

mask/common/pt-br.ts renamed to lang/pt-br/masks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IMask, IMaskFunction } from '..';
1+
import { IMask, IMaskFunction } from '../../mask';
22

33
const cpf: IMaskFunction = {
44
apply: (value: string) => {
@@ -30,13 +30,13 @@ const cnpj: IMaskFunction = {
3030
.replace(/[-.\\]$/, '');
3131
},
3232
clean: (value: string) => value.replace(/\D/gi, '').substr(0, 14)
33-
}
33+
};
3434

3535
const masks: IMask[] = [{
3636
name: 'zipcode',
3737
apply: (value: string) => {
3838
if (!value) return '';
39-
return value.replace(/^(\d{0,5})(\d{0,3}).*/, '$1-$2').replace(/-$/, '')
39+
return value.replace(/^(\d{0,5})(\d{0,3}).*/, '$1-$2').replace(/-$/, '');
4040
},
4141
clean: (value: string) => value.replace(/\D/gi, '').substr(0, 8)
4242
}, {

validator/custom-languages/pt-br.ts renamed to lang/pt-br/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'validatorjs/dist/lang/pt';
22

33
import { IConfig } from '../../config';
44

5-
const language: IConfig['validation'] = {
5+
const validation: IConfig['validation'] = {
66
lang: 'custom-pt-br',
77
customMessages: {
88
same: 'Não coincide.',
@@ -45,4 +45,4 @@ const language: IConfig['validation'] = {
4545
}
4646
};
4747

48-
export default language;
48+
export default validation;

package.json

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"validation",
1010
"material"
1111
],
12-
"version": "1.4.3",
12+
"version": "1.4.4",
1313
"main": "./index.js",
1414
"types": "./index.d.ts",
1515
"license": "MIT",
@@ -27,22 +27,31 @@
2727
"scripts": {
2828
"docs:dev": "(cd docs/project && yarn && yarn start)",
2929
"docs:build": "sh docs/build.sh",
30-
"precommit": "yarn tsc --noEmit",
3130
"prepare": "yarn tsc"
3231
},
32+
"husky": {
33+
"hooks": {
34+
"pre-commit": "concurrently -r \"yarn tslint -p tsconfig.json\" \"yarn tsc --noEmit\"",
35+
"post-merge": "yarn"
36+
}
37+
},
3338
"dependencies": {
3439
"react": ">=16.3.0",
35-
"tslib": ">=1.9.0",
36-
"validatorjs": ">=3.0.0"
40+
"tslib": ">=1.9.3",
41+
"validatorjs": "^3.15.1"
3742
},
3843
"peerDependencies": {
3944
"react": ">=16.3.0"
4045
},
4146
"devDependencies": {
42-
"@types/node": "10.9.3",
43-
"@types/react": "16.4.12",
44-
"@types/validatorjs": "3.7.1",
45-
"husky": "0.14.3",
46-
"typescript": "3.0.1"
47+
"@types/node": "10.12.18",
48+
"@types/react": "16.7.20",
49+
"@types/validatorjs": "3.15.0",
50+
"concurrently": "4.1.0",
51+
"husky": "1.3.1",
52+
"tslint": "5.12.1",
53+
"tslint-eslint-rules": "5.4.0",
54+
"tslint-react": "3.6.0",
55+
"typescript": "3.2.4"
4756
}
48-
}
57+
}

0 commit comments

Comments
 (0)