Skip to content

Commit

Permalink
feat: add support for locale + regex + simpler code
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Dec 15, 2022
1 parent f07f445 commit 3640f1d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export default function App() {
<TextInputWithError
mode="outlined"
{...email('email', {

validate: (v) => {
return looksLikeMail(v) ? true : 'Email-address is invalid';
},
Expand All @@ -136,10 +137,10 @@ export default function App() {
<TextInputWithError
mode="outlined"
{...telephone('telephone', {
validate: (v) => {
console.log({ v });
return looksLikeTelephone(v) ? true : 'Telephone is invalid';
},
required: true,
minLength: 3,
maxLength: 10,
shouldFollowRegexes: [telephoneRegex],
label: "Telefoon"
})}
/>
Expand Down Expand Up @@ -174,17 +175,10 @@ function TextInputWithError({ errorMessage, ...rest }: React.ComponentProps<type
}


// you can add your own validate functions
function looksLikeTelephone(str: string): boolean {
if (str.length !== 10) {
return false;
}
let isNum = /^\d+$/.test(str);
if (!isNum) {
return false;
}
return true;
}
const telephoneRegex = {
regex: new RegExp(/^\d+$/),
errorMessage: 'Telephone is invalid',
};

// you can add your own validate functions
function looksLikeMail(str: string): boolean {
Expand Down
23 changes: 9 additions & 14 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ export default function App() {
<TextInputWithError
mode="outlined"
{...fh.telephone('telephone', {
validate: (v) => {
return looksLikeTelephone(v) ? true : 'Telephone is invalid';
},
required: true,
minLength: 3,
maxLength: 10,
shouldFollowRegexes: [telephoneRegex],
label: 'Telephone',
})}
/>
Expand Down Expand Up @@ -123,11 +124,7 @@ export default function App() {
required: true,
minLength: 3,
maxLength: 10,
validate: (v) => {
return looksLikeTelephone(v || '')
? true
: 'Telephone is invalid';
},
shouldFollowRegexes: [telephoneRegex],
label: 'Organization telephone',
})}
/>
Expand Down Expand Up @@ -213,12 +210,10 @@ function AddressCompanyEdit({
);
}

function looksLikeTelephone(str: string): boolean {
if (str.length !== 10) {
return false;
}
return /^\d+$/.test(str);
}
const telephoneRegex = {
regex: new RegExp(/^\d+$/),
errorMessage: 'Telephone is invalid',
};

function looksLikeMail(str: string): boolean {
let lastAtPos = str.lastIndexOf('@');
Expand Down

0 comments on commit 3640f1d

Please sign in to comment.