-
Notifications
You must be signed in to change notification settings - Fork 114
/
Input.js
146 lines (136 loc) · 3.15 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
import React from "react";
import PropTypes from "prop-types";
import { ControlBehavior } from "@weave-design/behaviors";
import InputPresenter from "./presenters/InputPresenter";
import InputHaloPresenter from "./presenters/InputHaloPresenter";
import {
availableTagNames,
availableVariants,
tagNames,
variants,
} from "./constants";
function Wrapper(props) {
if (props.variant === variants.PLAIN) {
return props.children;
}
return <InputHaloPresenter {...props} />;
}
Wrapper.propTypes = {
children: PropTypes.node,
tagName: PropTypes.oneOf(availableTagNames),
variant: PropTypes.oneOf(availableVariants),
};
function Input(props) {
const {
onFocus: onFocusProp,
onBlur: onBlurProp,
onMouseEnter: onMouseEnterProp,
onMouseLeave: onMouseLeaveProp,
disabled: disabledProp,
inputRef,
stylesheet,
tagName,
variant,
error,
...otherProps
} = props;
return (
<ControlBehavior
onFocus={onFocusProp}
onBlur={onBlurProp}
onMouseEnter={onMouseEnterProp}
onMouseLeave={onMouseLeaveProp}
>
{({
hasHover,
onMouseEnter,
onMouseLeave,
hasFocus,
onFocus,
onBlur,
}) => (
<Wrapper
isDisabled={disabledProp}
hasFocus={hasFocus}
hasHover={hasHover}
stylesheet={stylesheet}
tagName={tagName}
variant={variant}
error={error}
{...otherProps}
>
<InputPresenter
disabled={disabledProp}
hasFocus={hasFocus}
hasHover={hasHover}
inputRef={inputRef}
onBlur={onBlur}
onFocus={onFocus}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
stylesheet={stylesheet}
tagName={tagName}
variant={variant}
{...otherProps}
/>
</Wrapper>
)}
</ControlBehavior>
);
}
Input.propTypes = {
/**
* The initial value of the control
*/
defaultValue: PropTypes.string,
/**
* Prevents the user from interacting with the input
*/
disabled: PropTypes.bool,
/**
* A callback ref that gets passed to the HTML input
*/
inputRef: PropTypes.func,
/**
* Fired when an element has lost focus
*/
onBlur: PropTypes.func,
/**
* Fired when an element has received focus
*/
onFocus: PropTypes.func,
/**
* Fired when a pointing device is moved over the element
*/
onMouseEnter: PropTypes.func,
/**
* Fired when the pointer of a pointing device is moved out of an element
*/
onMouseLeave: PropTypes.func,
/**
* Adds custom/overriding styles
*/
stylesheet: PropTypes.func,
/**
* Renders the HTML element that is passed in
*/
tagName: PropTypes.oneOf(availableTagNames),
/**
* The value of the control
*/
value: PropTypes.string,
/**
* The visual variant of the input
*/
variant: PropTypes.oneOf(availableVariants),
/**
* Specify if the value of the input is wrong
*/
error: PropTypes.bool,
};
Input.defaultProps = {
tagName: tagNames.INPUT,
variant: variants.LINE,
error: false,
};
export default Input;