Skip to content

Commit

Permalink
chore(demo): add description how to merge Maskito React ref with othe…
Browse files Browse the repository at this point in the history
…r 3d-party refs (#1656)
  • Loading branch information
nsbarsukov authored Sep 23, 2024
1 parent d3336b8 commit e099f9f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -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 (
<input
ref={(node) => {
maskitoRef(node);
externalRef.current = node;
}}
placeholder="Enter a number"
/>
);
};
21 changes: 21 additions & 0 deletions projects/demo/src/pages/frameworks/react/examples/merge-ref.md
Original file line number Diff line number Diff line change
@@ -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 (
<input
ref={(node) => {
maskitoRef(node);
anyExternalRef.current = node;
}}
/>
);
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,13 +29,15 @@ 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<string, TuiRawLoaderContent> = {
'index.tsx': import('./examples/2-element-predicate/index.tsx?raw'),
'awesome-input.tsx': import(
'./examples/2-element-predicate/awesome-input.tsx?raw'
),
};

protected readonly controlledInputDemo = import('./examples/controlled-input.md?raw');
protected readonly bestBadPractice = import('./examples/best-bad-practice.md?raw');
}
11 changes: 11 additions & 0 deletions projects/demo/src/pages/frameworks/react/react-doc.style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.no-tabs {
padding-top: 0;

::ng-deep .t-example {
margin-top: 0;
}
}

section {
margin-top: 3.5rem;
}
87 changes: 53 additions & 34 deletions projects/demo/src/pages/frameworks/react/react-doc.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<tui-notification
appearance="warning"
size="m"
class="tui-space_top-6"
>
<div>
<strong>Prerequisites</strong>
Expand All @@ -29,7 +28,7 @@
</div>
</tui-notification>

<section class="tui-space_top-12">
<section id="getting-started">
<h2>Getting Started</h2>

<p>Install libraries</p>
Expand All @@ -45,25 +44,69 @@ <h2>Getting Started</h2>

<p>See the result of above code example in action:</p>

<tui-doc-example [style.padding-top.px]="0">
<tui-doc-example class="no-tabs">
<react-example-1 />
</tui-doc-example>
</section>

<section class="tui-space_top-12">
<section id="controlled-input">
<h2>Controlled masked input</h2>

<p>
<strong>Maskito</strong>
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
<code>onInput</code>
instead of React-specific
<code>onChange</code>
event. Do not worry, both events works similarly! Read more about it in the
<a
href="https://react.dev/reference/react-dom/components/input#props"
rel="noreferrer"
target="_blank"
tuiLink
>
official React documentation.
</a>
</p>

<tui-doc-code [code]="controlledInputDemo" />
</section>

<section id="merge-refs">
<h2>Merge Maskito ref with the third-party ref</h2>

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

<p>
<strong>
Use
<a
href="https://react.dev/reference/react-dom/components/common#ref-callback"
rel="noreferrer"
target="_blank"
tuiLink
>
ref callback
</a>
!
</strong>
</p>

<tui-doc-code [code]="mergeRefDemo" />
</section>

<section id="element-predicate">
<h2>Query nested input element</h2>

<p>
Pass a predicate to
<code>elementPredicate</code>
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.
</p>

<tui-notification
size="m"
class="tui-space_bottom-4"
>
<tui-notification size="m">
<div>
By default
<strong>Maskito</strong>
Expand All @@ -81,31 +124,7 @@ <h2>Query nested input element</h2>
</tui-doc-example>
</section>

<section class="tui-space_top-12">
<h2>Controlled masked input</h2>

<p>
<strong>Maskito</strong>
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
<code>onInput</code>
instead of React-specific
<code>onChange</code>
event. Do not worry, both events works similarly! Read more about it in the
<a
href="https://react.dev/reference/react-dom/components/input#props"
rel="noreferrer"
target="_blank"
tuiLink
>
official React documentation.
</a>
</p>

<tui-doc-code [code]="controlledInputDemo" />
</section>

<section class="tui-space_top-12">
<section>
<h2>Best practices & Anti-Patterns</h2>

<p>
Expand Down

0 comments on commit e099f9f

Please sign in to comment.