-
Notifications
You must be signed in to change notification settings - Fork 612
/
index.tsx
196 lines (181 loc) · 4.95 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React from 'react'
import {
NativeSyntheticEvent,
Text,
TextInput,
TextInputChangeEventData,
TextInputProperties,
TouchableWithoutFeedback,
View,
} from 'react-native'
import { Omit } from 'utility-types'
import Icon from '../icon'
import { Theme, WithTheme, WithThemeStyles } from '../style'
import { TextAreaItemPropsType } from './PropsType'
import TextareaItemStyles, { TextareaItemStyle } from './style/index'
export type TextInputProps = Omit<
TextInputProperties,
'onChange' | 'onFocus' | 'onBlur'
>
function fixControlledValue(value?: string) {
if (typeof value === 'undefined' || value === null) {
return ''
}
return value
}
export interface TextareaItemProps
extends TextAreaItemPropsType,
TextInputProps,
WithThemeStyles<TextareaItemStyle> {
last?: boolean
onContentSizeChange?: (e: any) => void
}
export default class TextAreaItem extends React.Component<
TextareaItemProps,
any
> {
static defaultProps = {
onChange() {},
onFocus() {},
onBlur() {},
onErrorClick() {},
clear: true,
error: false,
editable: true,
rows: 1,
count: 0,
keyboardType: 'default',
autoHeight: false,
last: false,
}
textAreaRef: TextInput | null
constructor(props: TextareaItemProps) {
super(props)
this.state = {
inputCount: fixControlledValue(props.value || props.defaultValue).length,
}
}
onChange = (event: NativeSyntheticEvent<TextInputChangeEventData>) => {
const text = event.nativeEvent.text
const { onChange } = this.props
this.setState({
inputCount: text.length,
})
if (onChange) {
onChange(text)
}
}
onContentSizeChange =
(theme: Theme) =>
(event: {
nativeEvent: { contentSize: { width: number; height: number } }
}) => {
let height
const { autoHeight, onContentSizeChange } = this.props
const rows = this.props.rows as number
if (autoHeight) {
height = event.nativeEvent.contentSize.height
} else if (rows > 1) {
height = 6 * rows * 4
} else {
height = theme.list_item_height
}
this.setState({
height,
})
if (onContentSizeChange) {
onContentSizeChange(event)
}
}
getHeight = (theme: Theme) => {
const { rows } = this.props
if (this.state.height) {
return this.state.height
}
return rows !== undefined && rows > 1
? 6 * rows * 4
: theme.list_item_height
}
render() {
const {
rows,
error,
clear,
count,
autoHeight,
last,
onErrorClick,
styles,
style,
...restProps
} = this.props
const { value, defaultValue } = restProps
const { inputCount } = this.state
return (
<WithTheme themeStyles={TextareaItemStyles} styles={styles}>
{(s, theme) => {
let valueProps
if ('value' in this.props) {
valueProps = {
value: fixControlledValue(value),
}
} else {
valueProps = {
defaultValue,
}
}
const containerStyle = {
borderBottomWidth: last ? 0 : theme.border_width_sm,
}
const textareaStyle = {
color: error ? '#f50' : theme.color_text_base,
paddingRight: error ? 2 * theme.h_spacing_lg : 0,
}
const maxLength = count! > 0 ? count : undefined
return (
<View
style={[s.container, containerStyle, { position: 'relative' }]}>
<TextInput
clearButtonMode={clear ? 'while-editing' : 'never'}
underlineColorAndroid="transparent"
style={[
s.input,
textareaStyle,
{ height: Math.max(45, this.getHeight(theme)) },
style,
]}
{...restProps}
{...valueProps}
onChange={(event) => this.onChange(event)}
onContentSizeChange={this.onContentSizeChange(theme)}
multiline={rows! > 1 || autoHeight}
numberOfLines={rows}
maxLength={maxLength}
ref={(ref) => (this.textAreaRef = ref)}
/>
{error ? (
<TouchableWithoutFeedback onPress={onErrorClick}>
<View style={[s.errorIcon]}>
<Icon
name="info-circle"
style={{
color: theme.brand_error,
}}
/>
</View>
</TouchableWithoutFeedback>
) : null}
{rows! > 1 && count! > 0 ? (
<View style={[s.count]}>
<Text style={s.countText}>
{inputCount} / {count}
</Text>
</View>
) : null}
</View>
)
}}
</WithTheme>
)
}
}