Skip to content

Commit

Permalink
pladaria/text-field-formatter-example
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Ladaria committed Sep 30, 2024
1 parent 21ce399 commit 4cf7db9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/__private_stories__/input-with-custom-formatter-story.tsx
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';

1 comment on commit 4cf7db9

@github-actions
Copy link

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

Please sign in to comment.