-
Notifications
You must be signed in to change notification settings - Fork 112
/
TextArea.tsx
141 lines (131 loc) · 3.79 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as React from 'react';
import * as PropTypes from 'prop-types';
import TextareaAutosize from '@twilio-paste/react-textarea-autosize-library';
import {styled, css} from '@twilio-paste/styling-library';
import {safelySpreadBoxProps, getCustomElementStyles} from '@twilio-paste/box';
import type {BoxProps} from '@twilio-paste/box';
import {InputBox} from '@twilio-paste/input-box';
export type TextAreaVariants = 'default' | 'inverse';
export interface TextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement>, Pick<BoxProps, 'element'> {
className?: never;
disabled?: boolean;
hasError?: boolean;
height?: never;
id?: string;
insertAfter?: React.ReactNode;
insertBefore?: React.ReactNode;
name?: string;
placeholder?: string;
readOnly?: boolean;
required?: boolean;
resize?: 'none' | 'vertical';
size?: never;
style?: never;
variant?: TextAreaVariants;
width?: never;
}
// @ts-expect-error can't work out how to stop the styled div color prop from emotion clashing with our color style prop in BoxProps
const TextAreaElement = styled(TextareaAutosize)<TextAreaProps>(
(props) =>
css({
appearance: 'none',
background: 'transparent',
border: 'none',
borderRadius: 'borderRadius20',
boxShadow: 'none',
color: 'inherit',
display: 'block',
fontFamily: 'inherit',
fontSize: 'fontSize30',
fontWeight: 'fontWeightMedium',
lineHeight: 'lineHeight20',
maxHeight: props.resize === 'vertical' ? 'none' : 'size30',
outline: 'none',
paddingBottom: 'space30',
paddingLeft: 'space40',
paddingRight: 'space40',
paddingTop: 'space30',
resize: props.resize,
width: '100%',
'&::placeholder': {
color: props.variant === 'inverse' ? 'colorTextInverseWeak' : 'colorTextWeak',
fontStyle: 'italic',
},
'&:focus::placeholder': {
color: props.variant === 'inverse' ? 'colorTextInverseWeak' : 'colorTextWeak',
},
'&:disabled': {
color: props.variant === 'inverse' ? 'colorTextInverseWeaker' : 'colorTextWeaker',
cursor: 'not-allowed',
// Fixes value color in Safari
'-webkit-text-fill-color': props.variant === 'inverse' ? 'colorTextInverseWeaker' : 'colorTextWeaker',
'-webkit-opacity': '1',
},
}),
getCustomElementStyles
);
const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>(
(
{
children,
disabled,
element = 'TEXTAREA',
hasError,
insertBefore,
insertAfter,
readOnly,
variant,
resize = 'none',
// size, height and width should not be passed down
size,
height,
width,
...props
},
ref
) => {
return (
<InputBox
disabled={disabled}
element={element}
hasError={hasError}
insertAfter={insertAfter}
insertBefore={insertBefore}
readOnly={readOnly}
variant={variant}
>
<TextAreaElement
{...safelySpreadBoxProps(props)}
aria-invalid={hasError}
aria-readonly={readOnly}
disabled={disabled}
data-paste-element={`${element}_ELEMENT`}
readOnly={readOnly}
ref={ref}
rows={3}
minRows={3}
spellCheck
resize={resize}
variant={variant}
>
{children}
</TextAreaElement>
</InputBox>
);
}
);
TextArea.displayName = 'TextArea';
TextArea.propTypes = {
disabled: PropTypes.bool,
element: PropTypes.string,
hasError: PropTypes.bool,
id: PropTypes.string,
name: PropTypes.string,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
placeholder: PropTypes.string,
readOnly: PropTypes.bool,
required: PropTypes.bool,
};
export {TextArea};