diff --git a/projects/demo/src/assets/icons/typescript.svg b/projects/demo/src/assets/icons/typescript.svg new file mode 100644 index 000000000..b16e25ab0 --- /dev/null +++ b/projects/demo/src/assets/icons/typescript.svg @@ -0,0 +1,10 @@ + + + + + diff --git a/projects/demo/src/pages/frameworks/react/examples/3-react-hook-form/index.tsx b/projects/demo/src/pages/frameworks/react/examples/3-react-hook-form/index.tsx new file mode 100644 index 000000000..76a561283 --- /dev/null +++ b/projects/demo/src/pages/frameworks/react/examples/3-react-hook-form/index.tsx @@ -0,0 +1,27 @@ +import type {MaskitoOptions} from '@maskito/core'; +import {maskitoNumberOptionsGenerator} from '@maskito/kit'; +import {useMaskito} from '@maskito/react'; +import type {ComponentType} from 'react'; +import {useForm} from 'react-hook-form'; + +import {withMaskitoRegister} from './with-maskito-register'; + +const options: MaskitoOptions = maskitoNumberOptionsGenerator({ + precision: 2, +}); + +export const App: ComponentType = () => { + const maskitoRef = useMaskito({options}); + const {register, watch} = useForm(); + + const value = watch('controlName'); + + console.info('[controlName]: ', value); + + return ( + + ); +}; diff --git a/projects/demo/src/pages/frameworks/react/examples/3-react-hook-form/with-maskito-register.ts b/projects/demo/src/pages/frameworks/react/examples/3-react-hook-form/with-maskito-register.ts new file mode 100644 index 000000000..9928c8692 --- /dev/null +++ b/projects/demo/src/pages/frameworks/react/examples/3-react-hook-form/with-maskito-register.ts @@ -0,0 +1,19 @@ +import type {RefCallback} from 'react'; +import type {UseFormRegisterReturn} from 'react-hook-form'; + +export const withMaskitoRegister = ( + registerResult: UseFormRegisterReturn, + maskitoRef: RefCallback, +): UseFormRegisterReturn & {onInput: UseFormRegisterReturn['onChange']} => { + const ref: RefCallback = (node): void => { + registerResult.ref(node); + maskitoRef(node); + }; + + return { + ...registerResult, + ref, + onInput: registerResult.onChange, + onChange: undefined, + }; +}; diff --git a/projects/demo/src/pages/frameworks/react/react-doc.component.ts b/projects/demo/src/pages/frameworks/react/react-doc.component.ts index 3d1677bc7..5eb9626a3 100644 --- a/projects/demo/src/pages/frameworks/react/react-doc.component.ts +++ b/projects/demo/src/pages/frameworks/react/react-doc.component.ts @@ -1,9 +1,11 @@ +import {NgSwitch, NgSwitchCase} from '@angular/common'; import {ChangeDetectionStrategy, Component} from '@angular/core'; import {RouterLink} from '@angular/router'; import {DemoPath} from '@demo/constants'; import type {TuiRawLoaderContent} from '@taiga-ui/addon-doc'; import {TuiAddonDoc} from '@taiga-ui/addon-doc'; import {TuiLink, TuiNotification} from '@taiga-ui/core'; +import {TuiTabs} from '@taiga-ui/kit'; import {ReactExample1} from './examples/1-use-maskito-basic-usage/example.component'; import {ReactExample2} from './examples/2-element-predicate/example.component'; @@ -12,12 +14,15 @@ import {ReactExample2} from './examples/2-element-predicate/example.component'; standalone: true, selector: 'react-doc-page', imports: [ + NgSwitch, + NgSwitchCase, ReactExample1, ReactExample2, RouterLink, TuiAddonDoc, TuiLink, TuiNotification, + TuiTabs, ], templateUrl: './react-doc.template.html', styleUrls: ['./react-doc.style.less'], @@ -39,5 +44,12 @@ export default class ReactDocPageComponent { ), }; + protected readonly reactHookFormExample: Record = { + 'index.tsx': import('./examples/3-react-hook-form/index.tsx?raw'), + 'with-maskito-register.ts': import( + './examples/3-react-hook-form/with-maskito-register.ts?raw' + ), + }; + protected readonly bestBadPractice = import('./examples/best-bad-practice.md?raw'); } diff --git a/projects/demo/src/pages/frameworks/react/react-doc.template.html b/projects/demo/src/pages/frameworks/react/react-doc.template.html index 92999e00e..9fddbe6de 100644 --- a/projects/demo/src/pages/frameworks/react/react-doc.template.html +++ b/projects/demo/src/pages/frameworks/react/react-doc.template.html @@ -124,6 +124,67 @@

Query nested input element

+
+

Integration with third-party library for forms

+ +

+ There is not silver bullet how to integrate + Maskito + with + any + library for form-building. Explore all examples above – the provided knowledge about element predicate, + ref merging and + OnInput +  event will help you a lot to achieve it. +

+ +

+ This example demonstrates how to use + Maskito + with popular library + + react-hook-form + + . +

+ + + + + + + + + + + +
+

Best practices & Anti-Patterns