This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
/
Input.tsx
231 lines (194 loc) · 6.34 KB
/
Input.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
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
import { Accessibility, inputBehavior } from '@fluentui/accessibility'
import { handleRef, Ref } from '@fluentui/react-component-ref'
import * as customPropTypes from '@fluentui/react-proptypes'
import * as React from 'react'
import * as PropTypes from 'prop-types'
import cx from 'classnames'
import * as _ from 'lodash'
import {
AutoControlledComponent,
RenderResultConfig,
partitionHTMLProps,
UIComponentProps,
ChildrenComponentProps,
commonPropTypes,
applyAccessibilityKeyHandlers,
} from '../../utils'
import { SupportedIntrinsicInputProps } from '../../utils/htmlPropsUtils'
import { WithAsProp, ShorthandValue, ComponentEventHandler, withSafeTypeForAs } from '../../types'
import Icon, { IconProps } from '../Icon/Icon'
import Box, { BoxProps } from '../Box/Box'
export interface InputSlotClassNames {
input: string
}
export interface InputProps
extends UIComponentProps,
ChildrenComponentProps,
SupportedIntrinsicInputProps {
/**
* Accessibility behavior if overridden by the user.
*/
accessibility?: Accessibility
/** A property that will change the icon on the input and clear the input on click on Cancel. */
clearable?: boolean
/** The default value of the input. */
defaultValue?: string | string[]
/** An input can take the width of its container. */
fluid?: boolean
/** Optional Icon to display inside the Input. */
icon?: ShorthandValue<IconProps>
/** An Input with icon can format the icon to appear at the start or at the end of the input field. */
iconPosition?: 'start' | 'end'
/** An input can be used inline with text. */
inline?: boolean
/** Shorthand for the input component. */
input?: ShorthandValue<BoxProps>
/** An input can have inverted colors. */
inverted?: boolean
/**
* Called on change.
*
* @param event - React's original SyntheticEvent.
* @param data - All props and proposed value.
*/
onChange?: ComponentEventHandler<InputProps & { value: string }>
/** The HTML input type. */
type?: string
/** Ref for input DOM node. */
inputRef?: React.Ref<HTMLElement>
/** The value of the input. */
value?: string | number
/** Shorthand for the wrapper component. */
wrapper?: ShorthandValue<BoxProps>
}
export interface InputState {
value?: InputProps['value']
}
class Input extends AutoControlledComponent<WithAsProp<InputProps>, InputState> {
inputRef = React.createRef<HTMLElement>()
static className = 'ui-input'
static displayName = 'Input'
static slotClassNames: InputSlotClassNames
static propTypes = {
...commonPropTypes.createCommon({
content: false,
}),
clearable: PropTypes.bool,
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
fluid: PropTypes.bool,
icon: customPropTypes.itemShorthandWithoutJSX,
iconPosition: PropTypes.oneOf(['start', 'end']),
input: customPropTypes.itemShorthand,
inputRef: customPropTypes.ref,
inline: PropTypes.bool,
inverted: PropTypes.bool,
onChange: PropTypes.func,
type: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
wrapper: customPropTypes.wrapperShorthand,
}
static defaultProps = {
accessibility: inputBehavior,
type: 'text',
wrapper: {},
iconPosition: 'end',
}
static autoControlledProps = ['value']
actionHandlers = {
clear: (e: React.KeyboardEvent) => {
if (this.props.clearable && this.state.value !== '') {
e.stopPropagation()
e.nativeEvent && e.nativeEvent.stopPropagation()
this.handleOnClear(e)
}
},
}
renderComponent({
accessibility,
ElementType,
unhandledProps,
styles,
variables,
}: RenderResultConfig<InputProps>) {
const { className, input, inputRef, type, wrapper } = this.props
const { value = '' } = this.state
const [htmlInputProps, restProps] = partitionHTMLProps(unhandledProps)
return Box.create(wrapper, {
defaultProps: () => ({
...accessibility.attributes.root,
className: cx(Input.className, className),
children: (
<>
<Ref
innerRef={(inputElement: HTMLElement) => {
handleRef(this.inputRef, inputElement)
handleRef(inputRef, inputElement)
}}
>
{Box.create(input || type, {
defaultProps: () => ({
...htmlInputProps,
as: 'input',
type,
value,
className: Input.slotClassNames.input,
styles: styles.input,
onChange: this.handleChange,
...applyAccessibilityKeyHandlers(accessibility.keyHandlers.input, htmlInputProps),
}),
})}
</Ref>
{Icon.create(this.computeIcon(), {
defaultProps: () => ({
styles: styles.icon,
variables: variables.icon,
}),
overrideProps: this.handleIconOverrides,
})}
</>
),
styles: styles.root,
...restProps,
}),
overrideProps: {
as: (wrapper && (wrapper as any).as) || ElementType,
},
})
}
handleIconOverrides = predefinedProps => ({
onClick: (e: React.SyntheticEvent) => {
this.handleOnClear(e)
this.inputRef.current.focus()
_.invoke(predefinedProps, 'onClick', e, this.props)
},
})
handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = _.get(e, 'target.value')
_.invoke(this.props, 'onChange', e, { ...this.props, value })
this.setState({ value })
}
handleOnClear = (e: React.SyntheticEvent) => {
if (this.props.clearable) {
_.invoke(this.props, 'onChange', e, { ...this.props, value: '' })
this.setState({ value: '' })
}
}
computeIcon = (): ShorthandValue<IconProps> => {
const { clearable, icon } = this.props
const { value } = this.state
if (clearable && (value as string).length !== 0) {
return 'stardust-close'
}
return icon || null
}
}
Input.slotClassNames = {
input: `${Input.className}__input`,
}
/**
* An Input is a field used to elicit an input from a user.
*
* @accessibility
* For good screen reader experience set `aria-label` or `aria-labelledby` attribute for input.
*/
export default withSafeTypeForAs<typeof Input, InputProps, 'div'>(Input)