-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathInput.js
239 lines (200 loc) · 5.93 KB
/
Input.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import {
childrenUtils,
createHTMLInput,
createShorthandFactory,
customPropTypes,
getComponentType,
getUnhandledProps,
partitionHTMLProps,
useKeyOnly,
useValueAndKey,
setRef,
} from '../../lib'
import Button from '../Button'
import Icon from '../Icon'
import Label from '../Label'
/**
* An Input is a field used to elicit a response from a user.
* @see Button
* @see Form
* @see Icon
* @see Label
*/
const Input = React.forwardRef(function (props, ref) {
const {
action,
actionPosition,
children,
className,
disabled,
error,
fluid,
focus,
icon,
iconPosition,
input,
inverted,
label,
labelPosition,
loading,
size,
tabIndex,
transparent,
type = 'text',
} = props
const computeIcon = () => {
if (!_.isNil(icon)) {
return icon
}
if (loading) {
return 'spinner'
}
}
const computeTabIndex = () => {
if (!_.isNil(tabIndex)) {
return tabIndex
}
if (disabled) {
return -1
}
}
const handleChange = (e) => {
const newValue = _.get(e, 'target.value')
props.onChange?.(e, { ...props, value: newValue })
}
const partitionProps = () => {
const unhandledProps = getUnhandledProps(Input, props)
const [htmlInputProps, rest] = partitionHTMLProps(unhandledProps)
return [
{
...htmlInputProps,
disabled,
type,
tabIndex: computeTabIndex(),
onChange: handleChange,
ref,
},
rest,
]
}
const classes = cx(
'ui',
size,
useKeyOnly(disabled, 'disabled'),
useKeyOnly(error, 'error'),
useKeyOnly(fluid, 'fluid'),
useKeyOnly(focus, 'focus'),
useKeyOnly(inverted, 'inverted'),
useKeyOnly(loading, 'loading'),
useKeyOnly(transparent, 'transparent'),
useValueAndKey(actionPosition, 'action') || useKeyOnly(action, 'action'),
useValueAndKey(iconPosition, 'icon') || useKeyOnly(icon || loading, 'icon'),
useValueAndKey(labelPosition, 'labeled') || useKeyOnly(label, 'labeled'),
'input',
className,
)
const ElementType = getComponentType(props)
const [htmlInputProps, rest] = partitionProps()
// Render with children
// ----------------------------------------
if (!childrenUtils.isNil(children)) {
// add htmlInputProps to the `<input />` child
const childElements = _.map(React.Children.toArray(children), (child) => {
if (child.type === 'input') {
return React.cloneElement(child, {
...htmlInputProps,
...child.props,
ref: (c) => {
setRef(child.ref, c)
setRef(ref, c)
},
})
}
return child
})
return (
<ElementType {...rest} className={classes}>
{childElements}
</ElementType>
)
}
// Render Shorthand
// ----------------------------------------
const actionElement = Button.create(action, { autoGenerateKey: false })
const labelElement = Label.create(label, {
defaultProps: {
className: cx(
'label',
// add 'left|right corner'
_.includes(labelPosition, 'corner') && labelPosition,
),
},
autoGenerateKey: false,
})
return (
<ElementType {...rest} className={classes}>
{actionPosition === 'left' && actionElement}
{labelPosition !== 'right' && labelElement}
{createHTMLInput(input || type, { defaultProps: htmlInputProps, autoGenerateKey: false })}
{Icon.create(computeIcon(), { autoGenerateKey: false })}
{actionPosition !== 'left' && actionElement}
{labelPosition === 'right' && labelElement}
</ElementType>
)
})
Input.displayName = 'Input'
Input.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** An Input can be formatted to alert the user to an action they may perform. */
action: PropTypes.oneOfType([PropTypes.bool, customPropTypes.itemShorthand]),
/** An action can appear along side an Input on the left or right. */
actionPosition: PropTypes.oneOf(['left']),
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** An Input field can show that it is disabled. */
disabled: PropTypes.bool,
/** An Input field can show the data contains errors. */
error: PropTypes.bool,
/** Take on the size of its container. */
fluid: PropTypes.bool,
/** An Input field can show a user is currently interacting with it. */
focus: PropTypes.bool,
/** Optional Icon to display inside the Input. */
icon: PropTypes.oneOfType([PropTypes.bool, customPropTypes.itemShorthand]),
/** An Icon can appear inside an Input on the left or right. */
iconPosition: PropTypes.oneOf(['left']),
/** Shorthand for creating the HTML Input. */
input: customPropTypes.itemShorthand,
/** Format to appear on dark backgrounds. */
inverted: PropTypes.bool,
/** Optional Label to display along side the Input. */
label: customPropTypes.itemShorthand,
/** A Label can appear outside an Input on the left or right. */
labelPosition: PropTypes.oneOf(['left', 'right', 'left corner', 'right corner']),
/** An Icon Input field can show that it is currently loading data. */
loading: PropTypes.bool,
/**
* Called on change.
*
* @param {ChangeEvent} event - React's original SyntheticEvent.
* @param {object} data - All props and a proposed value.
*/
onChange: PropTypes.func,
/** An Input can vary in size. */
size: PropTypes.oneOf(['mini', 'small', 'large', 'big', 'huge', 'massive']),
/** An Input can receive focus. */
tabIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** Transparent Input has no background. */
transparent: PropTypes.bool,
/** The HTML input type. */
type: PropTypes.string,
}
Input.create = createShorthandFactory(Input, (type) => ({ type }))
export default Input