-
Notifications
You must be signed in to change notification settings - Fork 4k
/
TextArea.js
68 lines (54 loc) · 1.73 KB
/
TextArea.js
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
import _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import { getComponentType, getUnhandledProps, useMergedRefs } from '../../lib'
/**
* A TextArea can be used to allow for extended user input.
* @see Form
*/
const TextArea = React.forwardRef(function (props, ref) {
const { rows = 3, value } = props
const elementRef = useMergedRefs(ref, React.useRef())
const handleChange = (e) => {
const newValue = _.get(e, 'target.value')
_.invoke(props, 'onChange', e, { ...props, value: newValue })
}
const handleInput = (e) => {
const newValue = _.get(e, 'target.value')
_.invoke(props, 'onInput', e, { ...props, value: newValue })
}
const rest = getUnhandledProps(TextArea, props)
const ElementType = getComponentType(props, { defaultAs: 'textarea' })
return (
<ElementType
{...rest}
onChange={handleChange}
onInput={handleInput}
ref={elementRef}
rows={rows}
value={value}
/>
)
})
TextArea.displayName = 'TextArea'
TextArea.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/**
* Called on change.
* @param {SyntheticEvent} event - The React SyntheticEvent object
* @param {object} data - All props and the event value.
*/
onChange: PropTypes.func,
/**
* Called on input.
* @param {SyntheticEvent} event - The React SyntheticEvent object
* @param {object} data - All props and the event value.
*/
onInput: PropTypes.func,
/** Indicates row count for a TextArea. */
rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** The value of the textarea. */
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
}
export default TextArea