forked from react-component/input
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextarea-show-count.tsx
More file actions
55 lines (51 loc) · 1.38 KB
/
textarea-show-count.tsx
File metadata and controls
55 lines (51 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* eslint-disable no-console */
import { TextArea } from '@rc-component/input';
import React, { useState, type ChangeEvent } from 'react';
import '../../assets/index.less';
export default function App() {
const [value, setValue] = useState('hello\nworld');
const onChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
const {
target: { value: currentValue },
} = e;
setValue(currentValue);
};
return (
<div>
<p>Uncontrolled</p>
<TextArea autoSize showCount />
<p>controlled</p>
<TextArea value={value} onChange={onChange} showCount maxLength={100} />
<p>with height</p>
<TextArea
value={value}
onChange={onChange}
showCount
style={{ height: 200, width: '100%', resize: 'vertical' }}
/>
<hr />
<p>Count.exceedFormatter</p>
<TextArea
defaultValue="👨👨👧👦"
count={{
show: true,
max: 5,
}}
/>
<TextArea
defaultValue="🔥"
count={{
show: true,
max: 5,
exceedFormatter: (val, { max }) => {
const segments = [...new Intl.Segmenter().segment(val)];
return segments
.filter((seg) => seg.index + seg.segment.length <= max)
.map((seg) => seg.segment)
.join('');
},
}}
/>
</div>
);
}