-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathInput.tsx
More file actions
220 lines (208 loc) · 5.72 KB
/
Input.tsx
File metadata and controls
220 lines (208 loc) · 5.72 KB
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
import { WheelEvent, ComponentType, forwardRef } from 'react';
import styled from 'styled-components';
import NumberInput from 'react-number-format';
import { getMargin } from '../../theme/utils';
import { Grid, GridProps } from '../Grid';
import { Icon } from '../Icon';
import { Flex } from '../Flex';
import { Box } from '../Box';
import { Text } from '../Text';
import { Tooltip } from '../Tooltip';
export type InputVariant = 'basic' | 'amount';
export enum IconPosition {
Left = 'left',
Right = 'right',
}
export enum LablePosition {
Top = 'top',
Left = 'left',
}
export type InputProps = {
variant: InputVariant;
margin?: string;
width?: string;
id?: string;
name?: string;
type?: 'text' | 'password' | 'email';
disabled?: boolean;
label?: string;
labelPosition?: LablePosition;
placeholder?: string;
value?: string | null;
onChange?: (state: any) => void;
tooltip?: string | ComponentType;
icon?: ComponentType;
iconPosition?: IconPosition;
unit?: string;
error?: string;
isDivisible?: boolean;
inputRef?: any;
readOnly?: boolean;
};
export type InputWrapperProps = GridProps & {
error?: string;
disabled?: boolean;
readOnly?: boolean;
};
const InputWrapper = styled(Grid)<InputWrapperProps>(
({ theme, error, disabled, readOnly }) => ({
backgroundColor: (theme.INPUT || { backgroundColor: 'unset' })
.backgroundColor,
padding: (theme.INPUT || { padding: 0 }).padding,
border: (theme.INPUT || { border: 0 }).border,
borderRadius: (theme.INPUT || { borderRadius: 0 }).borderRadius,
transition: (theme.INPUT || { transition: 'unset' }).transition,
...(error
? {
borderColor: theme.COLOR.danger2,
background: theme.COLOR.danger3,
input: {
background: theme.COLOR.danger3,
},
}
: {}),
...(!disabled && theme.INPUT && theme.INPUT['&:hover']
? { '&:hover': theme.INPUT['&:hover'] }
: {}),
...(theme.INPUT && theme.INPUT['&:focus']
? { '&:focus-within': theme.INPUT['&:focus'] }
: {}),
...(disabled && theme.INPUT && theme.INPUT['&:disabled']
? theme.INPUT['&:disabled']
: {}),
...(readOnly && theme.INPUT && theme.INPUT['&:readOnly']
? theme.INPUT['&:readOnly']
: {}),
}),
);
const InputComponent = styled.input(({ theme, readOnly }) => ({
...(theme.INPUT || {}),
margin: 0,
padding: 0,
border: 0,
borderRadius: 0,
borderColor: 'transparent',
WebkitAppearance: 'none',
outline: 'none',
...(readOnly
? { backgroundColor: theme.INPUT['&:readOnly'].backgroundColor }
: {}),
}));
const Unit = styled.div(({ theme }) => ({
margin: getMargin({ theme, margin: '0 0 0 s' }),
padding: 0,
color: theme.COLOR.gray3,
}));
export const Input = forwardRef<HTMLInputElement, InputProps>(
function ForwardRefInput(inputProps, ref) {
const {
variant,
margin,
width,
type,
label,
labelPosition = LablePosition.Top,
tooltip,
icon,
unit,
error,
isDivisible = true,
disabled,
readOnly,
iconPosition,
inputRef,
...props
} = inputProps;
const isBasic = variant === 'basic';
const isAmount = variant === 'amount';
const componentProps = {
...props,
disabled,
readOnly,
...(isBasic ? { type } : {}),
...(isAmount
? {
thousandSeparator: true,
allowNegative: false,
decimalScale: isDivisible ? 6 : 0,
onWheel: (e: WheelEvent<HTMLInputElement>) => {
e.currentTarget.blur();
},
}
: {}),
};
const renderIcon = (
_icon: ComponentType,
_iconPosition: IconPosition = IconPosition.Left,
_disabled = false,
) => (
<Icon
icon={_icon}
variant="basic"
size="24px"
color={_disabled ? 'gray4' : 'gray3'}
margin={_iconPosition === IconPosition.Left ? '0 s 0 0' : '0'}
/>
);
return (
<Flex
width={width ? width : "fit-content"}
variant="basic"
align={labelPosition === LablePosition.Left ? 'center' : 'start'}
dir={labelPosition === LablePosition.Left ? 'row' : 'column'}
margin={margin}
>
<Box
width="100%"
variant="raw"
display="block"
margin={labelPosition === LablePosition.Left ? '0 8px 0 0' : ''}
>
{label && (
<Flex
align="center"
variant="raw"
justify={tooltip ? 'spaced' : 'start'}
>
<Text
as="span"
variant="b2m"
color={disabled ? 'gray3' : 'gray1'}
>
{label}
</Text>
{tooltip && <Tooltip variant="icon" content={tooltip} />}
</Flex>
)}
</Box>
<InputWrapper
width="100%"
variant="raw"
align="center"
cols={`${icon ? 'auto ' : ''}1fr${unit ? ' auto' : ''}`}
error={error}
disabled={disabled}
readOnly={readOnly}
>
{icon &&
(!iconPosition || iconPosition === IconPosition.Left) &&
renderIcon(icon, iconPosition, disabled)}
<InputComponent
{...(inputRef ? inputRef : {ref})}
as={isAmount ? NumberInput : 'input'}
{...componentProps}
/>
{icon &&
iconPosition === IconPosition.Right &&
renderIcon(icon, iconPosition, disabled)}
{unit && <Unit>{unit}</Unit>}
</InputWrapper>
{error && (
<Text as="span" variant="b3" color="danger2">
{error}
</Text>
)}
</Flex>
);
},
);