Skip to content

Commit 38c373c

Browse files
feat: useKeyFilter docs added
1 parent 2741478 commit 38c373c

4 files changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { useKeyFilter } from '@primereact/hooks';
2+
import { InputText } from 'primereact/inputtext';
3+
import { Label } from 'primereact/label';
4+
import * as React from 'react';
5+
6+
export default function PatternDemo() {
7+
const { onKeyPress: onIntegerKeyPress } = useKeyFilter({ pattern: 'int' });
8+
const { onKeyPress: onNumberKeyPress } = useKeyFilter({ pattern: 'num' });
9+
const { onKeyPress: onMoneyKeyPress } = useKeyFilter({ pattern: 'money' });
10+
const { onKeyPress: onHexKeyPress } = useKeyFilter({ pattern: 'hex' });
11+
const { onKeyPress: onAlphaKeyPress } = useKeyFilter({ pattern: 'alpha' });
12+
const { onKeyPress: onAlphanumKeyPress } = useKeyFilter({ pattern: 'alphanum' });
13+
14+
const [integer, setInteger] = React.useState('');
15+
const [number, setNumber] = React.useState('');
16+
const [money, setMoney] = React.useState('');
17+
const [hex, setHex] = React.useState('');
18+
const [alphabetic, setAlphabetic] = React.useState('');
19+
const [alphanumeric, setAlphanumeric] = React.useState('');
20+
21+
return (
22+
<div className="card">
23+
<div className="flex flex-wrap gap-3 mb-4">
24+
<div className="flex-auto">
25+
<Label htmlFor="integer" className="font-bold block mb-2">
26+
Integer
27+
</Label>
28+
<InputText id="integer" value={integer} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setInteger(e.target.value)} onKeyPress={onIntegerKeyPress} className="w-full" />
29+
</div>
30+
<div className="flex-auto">
31+
<Label htmlFor="number" className="font-bold block mb-2">
32+
Number
33+
</Label>
34+
<InputText id="number" value={number} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setNumber(e.target.value)} onKeyPress={onNumberKeyPress} className="w-full" />
35+
</div>
36+
<div className="flex-auto">
37+
<Label htmlFor="money" className="font-bold block mb-2">
38+
Money
39+
</Label>
40+
<InputText id="money" value={money} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setMoney(e.target.value)} onKeyPress={onMoneyKeyPress} className="w-full" />
41+
</div>
42+
</div>
43+
<div className="flex flex-wrap gap-3">
44+
<div className="flex-auto">
45+
<Label htmlFor="hex" className="font-bold block mb-2">
46+
Hex
47+
</Label>
48+
<InputText id="hex" value={hex} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setHex(e.target.value)} onKeyPress={onHexKeyPress} className="w-full" />
49+
</div>
50+
<div className="flex-auto">
51+
<Label htmlFor="alphabetic" className="font-bold block mb-2">
52+
Alphabetic
53+
</Label>
54+
<InputText id="alphabetic" value={alphabetic} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setAlphabetic(e.target.value)} onKeyPress={onAlphaKeyPress} className="w-full" />
55+
</div>
56+
<div className="flex-auto">
57+
<Label htmlFor="alphanumeric" className="font-bold block mb-2">
58+
Alphanumeric
59+
</Label>
60+
<InputText id="alphanumeric" value={alphanumeric} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setAlphanumeric(e.target.value)} onKeyPress={onAlphanumKeyPress} className="w-full" />
61+
</div>
62+
</div>
63+
</div>
64+
);
65+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useKeyFilter } from '@primereact/hooks';
2+
import { InputText } from 'primereact/inputtext';
3+
import { Label } from 'primereact/label';
4+
import * as React from 'react';
5+
6+
export default function RegexDemo() {
7+
const { onKeyPress: onSpaceKeyPress } = useKeyFilter({ pattern: /[^\s]/ });
8+
const { onKeyPress: onCharsKeyPress } = useKeyFilter({ pattern: /^[^<>*!]+$/ });
9+
10+
const [spacekey, setSpacekey] = React.useState('');
11+
const [chars, setChars] = React.useState('');
12+
13+
return (
14+
<div className="card">
15+
<div className="flex flex-wrap gap-3">
16+
<div className="flex-auto">
17+
<Label for="spacekey" className="font-bold block mb-2">
18+
Block Space
19+
</Label>
20+
<InputText id="spacekey" value={spacekey} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setSpacekey(e.target.value)} onKeyPress={onSpaceKeyPress} fluid />
21+
</div>
22+
<div className="flex-auto">
23+
<Label htmlFor="chars" className="font-bold block mb-2">
24+
Block &lt; &gt; * !
25+
</Label>
26+
<InputText id="chars" value={chars} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setChars(e.target.value)} onKeyPress={onCharsKeyPress} fluid />
27+
</div>
28+
</div>
29+
</div>
30+
);
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useKeyFilter } from '@primereact/hooks';
2+
import { InputText } from 'primereact/inputtext';
3+
import { Label } from 'primereact/label';
4+
import * as React from 'react';
5+
6+
export default function RegexWordDemo() {
7+
const { onKeyPress, validate } = useKeyFilter({ pattern: /^[+]?(\d{1,12})?$/, validateOnly: true });
8+
9+
const [text, setText] = React.useState('');
10+
11+
const validateInput = (e: React.ChangeEvent<HTMLInputElement>) => {
12+
if (validate(e)) {
13+
setText(e.target.value);
14+
}
15+
};
16+
17+
return (
18+
<div className="card flex justify-center">
19+
<div>
20+
<Label htmlFor="numkeys" className="font-bold block mb-2">
21+
Block Numeric (allow &quot;+&quot; only once at start)
22+
</Label>
23+
<InputText id="numkeys" value={text} onChange={(e: React.ChangeEvent<HTMLInputElement>) => validateInput(e)} onKeyPress={onKeyPress} fluid />
24+
</div>
25+
</div>
26+
);
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: useKeyFilter
3+
description: useKeyFilter used to block individual keystrokes based on a pattern.
4+
component: usekeyfilter
5+
---
6+
7+
## Usage
8+
9+
```tsx
10+
import { useKeyFilter } from '@primereact/hooks';
11+
```
12+
13+
```tsx
14+
const keyfilter = useKeyFilter();
15+
```
16+
17+
## Examples
18+
19+
### Pattern
20+
21+
useKeyFilter provides various presets configured with the _pattern_ option.
22+
23+
<DocDemoViewer name="usekeyfilter:pattern-demo" />
24+
25+
### Regex
26+
27+
In addition to the presets, a regular expression can be configured for customization of blocking a single key press.
28+
29+
<DocDemoViewer name="usekeyfilter:regex-demo" />
30+
31+
### Regex Word
32+
33+
In addition to the presets, a regular expression can be used to validate the entire word using _validateOnly_ option.
34+
35+
<DocDemoViewer name="usekeyfilter:regex-word-demo" />

0 commit comments

Comments
 (0)