From e099f9fe3129f91d300b4ed7107358a2428190e9 Mon Sep 17 00:00:00 2001 From: Nikita Barsukov Date: Mon, 23 Sep 2024 09:30:51 +0300 Subject: [PATCH] chore(demo): add description how to merge Maskito React ref with other 3d-party refs (#1656) --- .../react/examples/3-merge-ref/index.tsx | 21 +++++ .../frameworks/react/examples/merge-ref.md | 21 +++++ .../frameworks/react/react-doc.component.ts | 5 +- .../frameworks/react/react-doc.style.less | 11 +++ .../frameworks/react/react-doc.template.html | 87 +++++++++++-------- 5 files changed, 110 insertions(+), 35 deletions(-) create mode 100644 projects/demo/src/pages/frameworks/react/examples/3-merge-ref/index.tsx create mode 100644 projects/demo/src/pages/frameworks/react/examples/merge-ref.md create mode 100644 projects/demo/src/pages/frameworks/react/react-doc.style.less diff --git a/projects/demo/src/pages/frameworks/react/examples/3-merge-ref/index.tsx b/projects/demo/src/pages/frameworks/react/examples/3-merge-ref/index.tsx new file mode 100644 index 000000000..738b1b84d --- /dev/null +++ b/projects/demo/src/pages/frameworks/react/examples/3-merge-ref/index.tsx @@ -0,0 +1,21 @@ +import type {MaskitoOptions} from '@maskito/core'; +import {useMaskito} from '@maskito/react'; +import type {ComponentType} from 'react'; + +const digitsOnlyMask: MaskitoOptions = { + mask: /^\d+$/, +}; + +export const App: ComponentType = () => { + const maskitoRef = useMaskito({options: digitsOnlyMask}); + + return ( + { + maskitoRef(node); + externalRef.current = node; + }} + placeholder="Enter a number" + /> + ); +}; diff --git a/projects/demo/src/pages/frameworks/react/examples/merge-ref.md b/projects/demo/src/pages/frameworks/react/examples/merge-ref.md new file mode 100644 index 000000000..1ed60c287 --- /dev/null +++ b/projects/demo/src/pages/frameworks/react/examples/merge-ref.md @@ -0,0 +1,21 @@ +```tsx +import {useMaskito} from '@maskito/react'; + +const options: MaskitoOptions = { + mask: /^\d+$/, +}; + +function App() { + const anyExternalRef = useRef(null); + const maskitoRef = useMaskito({options}); + + return ( + { + maskitoRef(node); + anyExternalRef.current = node; + }} + /> + ); +} +``` 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 ec29aee9d..3d1677bc7 100644 --- a/projects/demo/src/pages/frameworks/react/react-doc.component.ts +++ b/projects/demo/src/pages/frameworks/react/react-doc.component.ts @@ -20,6 +20,7 @@ import {ReactExample2} from './examples/2-element-predicate/example.component'; TuiNotification, ], templateUrl: './react-doc.template.html', + styleUrls: ['./react-doc.style.less'], changeDetection: ChangeDetectionStrategy.OnPush, }) export default class ReactDocPageComponent { @@ -28,6 +29,9 @@ export default class ReactDocPageComponent { './examples/1-use-maskito-basic-usage/use-maskito-basic-usage.tsx?raw' ); + protected readonly controlledInputDemo = import('./examples/controlled-input.md?raw'); + protected readonly mergeRefDemo = import('./examples/merge-ref.md?raw'); + protected readonly elementPredicateExample: Record = { 'index.tsx': import('./examples/2-element-predicate/index.tsx?raw'), 'awesome-input.tsx': import( @@ -35,6 +39,5 @@ export default class ReactDocPageComponent { ), }; - protected readonly controlledInputDemo = import('./examples/controlled-input.md?raw'); protected readonly bestBadPractice = import('./examples/best-bad-practice.md?raw'); } diff --git a/projects/demo/src/pages/frameworks/react/react-doc.style.less b/projects/demo/src/pages/frameworks/react/react-doc.style.less new file mode 100644 index 000000000..677d96d2b --- /dev/null +++ b/projects/demo/src/pages/frameworks/react/react-doc.style.less @@ -0,0 +1,11 @@ +.no-tabs { + padding-top: 0; + + ::ng-deep .t-example { + margin-top: 0; + } +} + +section { + margin-top: 3.5rem; +} 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 28133426f..92999e00e 100644 --- a/projects/demo/src/pages/frameworks/react/react-doc.template.html +++ b/projects/demo/src/pages/frameworks/react/react-doc.template.html @@ -12,7 +12,6 @@
Prerequisites @@ -29,7 +28,7 @@
-
+

Getting Started

Install libraries

@@ -45,25 +44,69 @@

Getting Started

See the result of above code example in action:

- +
-
+
+

Controlled masked input

+ +

+ Maskito + core is developed as framework-agnostic library. It does not depend on any JS-framework's peculiarities. It + uses only native browser API. That is why you should use native + onInput + instead of React-specific + onChange + event. Do not worry, both events works similarly! Read more about it in the + + official React documentation. + +

+ + +
+ +
+

Merge Maskito ref with the third-party ref

+ +

Do you need to use multiple hooks that return refs which both should be attached to the masked textfield?

+ +

+ + Use + + ref callback + + ! + +

+ + +
+ +

Query nested input element

Pass a predicate to elementPredicate to find input element for you, if you do not have a direct access to it. For example, you use component from - some UI Kit library. + some UI Kit library.

- +
By default Maskito @@ -81,31 +124,7 @@

Query nested input element

-
-

Controlled masked input

- -

- Maskito - core is developed as framework-agnostic library. It does not depend on any JS-framework's peculiarities. It - uses only native browser API. That is why you should use native - onInput - instead of React-specific - onChange - event. Do not worry, both events works similarly! Read more about it in the - - official React documentation. - -

- - -
- -
+

Best practices & Anti-Patterns