-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathInput.tsx
More file actions
151 lines (141 loc) · 3.77 KB
/
Input.tsx
File metadata and controls
151 lines (141 loc) · 3.77 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
import { FC, WheelEvent, ComponentType } 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 { Text } from '../Text';
import { Tooltip } from '../Tooltip';
export type InputVariant = 'basic' | 'amount';
export type InputProps = {
variant: InputVariant;
margin?: string;
id?: string;
name?: string;
type?: 'text' | 'password' | 'email' | 'phone';
disabled?: boolean;
label?: string;
placeholder?: string;
value?: string | null;
onChange?: (state: any) => void;
tooltip?: string | ComponentType;
icon?: ComponentType;
unit?: string;
error?: string;
countryCode?: string;
isDivisible?: boolean;
thousandSeparator?: boolean;
inputRef?: any;
};
export type InputWrapperProps = GridProps & {
error?: string;
disabled?: boolean;
};
const InputWrapper = styled(Grid)<InputWrapperProps>(
({ theme, error, disabled }) => ({
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.danger } : {}),
...(theme.INPUT && theme.INPUT['&:focus']
? { '&:focus-within': theme.INPUT['&:focus'] }
: {}),
...(disabled && theme.INPUT && theme.INPUT['&:disabled']
? theme.INPUT['&:disabled']
: {}),
}),
);
const Component = styled.input(({ theme }) => ({
...(theme.INPUT || {}),
margin: 0,
padding: 0,
border: 0,
borderRadius: 0,
borderColor: 'transparent',
WebkitAppearance: 'none',
outline: 'none',
}));
const Unit = styled.div(({ theme }) => ({
margin: getMargin({ theme, margin: '0 0 0 s' }),
padding: 0,
color: theme.COLOR.gray3,
}));
export const Input: FC<InputProps> = ({
variant,
margin,
type,
label,
tooltip,
icon,
unit,
error,
isDivisible = true,
thousandSeparator = true,
countryCode = '+1',
disabled,
...props
}) => {
const isBasic = variant === 'basic';
const isAmount = variant === 'amount';
const componentProps = {
...props,
disabled,
...(isBasic ? { type } : {}),
...(isAmount
? {
thousandSeparator,
allowNegative: false,
decimalScale: isDivisible ? 6 : 0,
onWheel: (e: WheelEvent<HTMLInputElement>) => {
e.currentTarget.blur();
},
}
: {}),
};
return (
<Text as="label" variant="b2m" display="block" margin={margin}>
{label && tooltip && (
<Flex variant="raw" justify={tooltip ? 'spaced' : 'start'}>
<Text as="span" variant="b2m">
{label}
</Text>
{tooltip && <Tooltip variant="icon" content={tooltip} />}
</Flex>
)}
<InputWrapper
variant="raw"
align="center"
cols={`${icon ? 'auto ' : ''}1fr${unit ? ' auto' : ''}`}
error={error}
disabled={disabled}
>
{icon && (
<Icon
icon={icon}
variant="basic"
size="24px"
color="gray3"
margin="0 s 0 0"
/>
)}
<Component
as={isAmount ? NumberInput : 'input'}
{...(type === 'phone'
? { format: `${countryCode} (###) ###-####`, mask: '_' }
: {})}
{...componentProps}
/>
{unit && <Unit>{unit}</Unit>}
</InputWrapper>
{error && (
<Text as="span" variant="b3" color="danger">
{error}
</Text>
)}
</Text>
);
};