forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Textarea.tsx
101 lines (92 loc) · 2.21 KB
/
Textarea.tsx
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import styled, {css} from 'styled-components'
import React, {TextareaHTMLAttributes, ReactElement} from 'react'
import {TextInputBaseWrapper} from './_TextInputWrapper'
import {FormValidationStatus} from './utils/types/FormValidationStatus'
import sx, {SxProp} from './sx'
export type TextareaProps = {
/**
* Apply inactive visual appearance to the Textarea
*/
disabled?: boolean
/**
* Indicates whether the Textarea is a required form field
*/
required?: boolean
/**
* Indicates whether the Textarea validation state
*/
validationStatus?: FormValidationStatus
/**
* Block
*/
block?: boolean
/**
* Allows resizing of the textarea
*/
resize?: 'none' | 'both' | 'horizontal' | 'vertical'
} & TextareaHTMLAttributes<HTMLTextAreaElement> &
SxProp
const StyledTextarea = styled.textarea<TextareaProps>`
border: 0;
font-size: inherit;
font-family: inherit;
background-color: transparent;
-webkit-appearance: none;
color: inherit;
width: 100%;
resize: both;
&:focus {
outline: 0;
}
${props =>
props.resize &&
css`
resize: ${props.resize};
`}
${props =>
props.disabled &&
css`
resize: none;
`}
${sx};
`
/**
* An accessible, native textarea component that supports validation states.
* This component accepts all native HTML <textarea> attributes as props.
*/
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
(
{
value,
disabled,
sx: sxProp,
required,
validationStatus,
rows = 7,
cols = 30,
resize = 'both',
block,
...rest
}: TextareaProps,
ref
): ReactElement => {
return (
<TextInputBaseWrapper sx={sxProp} validationStatus={validationStatus} disabled={disabled} block={block}>
<StyledTextarea
value={value}
resize={resize}
required={required}
aria-required={required ? 'true' : 'false'}
aria-invalid={validationStatus === 'error' ? 'true' : 'false'}
ref={ref}
disabled={disabled}
rows={rows}
cols={cols}
{...rest}
/>
</TextInputBaseWrapper>
)
}
)
Textarea.displayName = 'Textarea'
export default Textarea