-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pladaria/text-field-formatter-example
- Loading branch information
Pedro Ladaria
committed
Sep 30, 2024
1 parent
21ce399
commit 4cf7db9
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/__private_stories__/input-with-custom-formatter-story.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as React from 'react'; | ||
import {TextField} from '..'; | ||
import {useRifm} from 'rifm'; | ||
|
||
export default { | ||
title: 'Private/Inputs/Input with custom formatter', | ||
}; | ||
|
||
export const Default: StoryComponent = () => { | ||
const [value, setValue] = React.useState(''); | ||
|
||
// format as: 000.000.000-00 | ||
const format = (value: string) => { | ||
const digits = value.replace(/\D/g, ''); | ||
const d1 = digits.substring(0, 3); | ||
const d2 = digits.substring(3, 6); | ||
const d3 = digits.substring(6, 9); | ||
const d4 = digits.substring(9, 11); | ||
|
||
return `${d1}.${d2}.${d3}-${d4}`.replace(/[^\d]+$/g, ''); | ||
}; | ||
|
||
const rifm = useRifm({ | ||
format, | ||
value, | ||
onChange: setValue, | ||
}); | ||
|
||
return ( | ||
<TextField | ||
name="cpf" | ||
label="CPF Number" | ||
value={rifm.value} | ||
onChange={rifm.onChange} | ||
placeholder="Type here" | ||
helperText="CPF Number" | ||
/> | ||
); | ||
}; | ||
|
||
Default.storyName = 'CPF number'; |
4cf7db9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for mistica-web ready!
✅ Preview
https://mistica-eyx6lc147-flows-projects-65bb050e.vercel.app
Built with commit 4cf7db9.
This pull request is being automatically deployed with vercel-action