-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (29 loc) · 807 Bytes
/
index.js
File metadata and controls
33 lines (29 loc) · 807 Bytes
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
/** @jsxRuntime classic */
/** @jsx jsx */
import { Textarea as TextareaUI, jsx } from 'theme-ui'
import React, { useState } from 'react'
const Textarea = React.forwardRef(({ countType, maximumLen, ...props }, ref) => {
const [count, setCount] = useState(0)
const { 'data-haserrors': haserrors } = props
return (
<>
<TextareaUI
ref={ref}
className={haserrors ? 'error-input' : ''}
{...props}
onChange={(e) => {
if (countType === 'word')
setCount(e.target.value.trim().split(/[\s,.\n]+/).length)
// By default is char count
else setCount(e.target.value.length)
}}
/>
{maximumLen && (
<span>
{count}/{maximumLen}
</span>
)}
</>
)
})
export default Textarea