-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
197 lines (165 loc) · 5.62 KB
/
index.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
import * as React from 'react';
import { createFormattedNumberInput } from 'react-formatted-number-input';
import { formatMoney } from '../../../formatters';
import { Slider } from '../Slider';
import styled from '../../../';
const Minus = () => (
<svg width="16" height="2" xmlns="http://www.w3.org/2000/svg">
<path fill="currentColor" d="M1 2h14a1 1 0 0 0 0-2H1a1 1 0 1 0 0 2z" />
</svg>
);
const Plus = () => (
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg">
<path fill="currentColor" d="M9 7h6a1 1 0 0 1 0 2H9v6a1 1 0 0 1-2 0V9H1a1 1 0 1 1 0-2h6V1a1 1 0 1 1 2 0v6z" />
</svg>
);
const Container = styled.div({
marginBottom: '3em',
});
const InputContainer = styled.div(({ theme }) => ({
border: `1px solid ${theme.colors.mediumGray}`,
borderRadius: theme.borderRadius.small,
display: 'flex',
height: 70,
position: 'relative',
zIndex: 2,
marginBottom: '1em',
}));
const Input = styled.input(({ theme }) => ({
appearance: 'none',
boxShadow: 'none',
border: `solid ${theme.colors.mediumGray}`,
borderWidth: '0 1px',
borderRadius: 0,
textAlign: 'center',
width: '100%',
flex: '80% 1 1',
font: theme.fonts.desktop.xl,
[theme.breakpoints.mobileAndLower]: {
font: theme.fonts.mobile.xl,
},
'::-ms-clear': {
display: 'none',
},
}));
const AmountInput = createFormattedNumberInput<any>(Input);
const Button = styled.button(({ theme }) => ({
fontFamily: 'inherit',
background: 'transparent',
border: 0,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
flexShrink: 0,
width: 55,
fontSize: 40,
padding: 0,
cursor: 'pointer',
borderRadius: theme.borderRadius.small,
transition: 'background-color 100ms',
color: theme.colors.black,
':hover': {
background: theme.colors.offWhite,
},
}));
const Range = styled.div(({ theme }) => ({
display: 'flex',
justifyContent: 'space-between',
font: theme.fonts.desktop.medium,
[theme.breakpoints.mobileAndLower]: {
font: theme.fonts.mobile.medium,
},
}));
interface Props {
currency: string;
min: number;
max: number;
value: number;
onChange: (value: number) => void;
stepSize: number;
}
interface State {
isDrafting: boolean;
draftValue?: number;
}
export class AmountSelector extends React.Component<Props, State> {
static displayName = 'Collector.AmountSelector';
state: State = {
isDrafting: false,
};
private increase = () => {
const remainder = this.props.value % this.props.stepSize;
const value = remainder > 0 ? this.props.value + this.props.stepSize - remainder : this.props.value + this.props.stepSize;
this.props.onChange(Math.min(value, this.props.max));
};
private decrease = () => {
const remainder = this.props.value % this.props.stepSize;
const value = remainder > 0 ? this.props.value - remainder : this.props.value - this.props.stepSize;
this.props.onChange(Math.max(value, this.props.min));
};
private handleRangeInputChange = (event: React.FormEvent<HTMLInputElement>) => {
const middle = this.props.max - this.props.min;
let value = Number(event.currentTarget.value);
if (value > middle) {
value = Math.ceil(value / this.props.stepSize) * this.props.stepSize;
value = Math.min(value, this.props.max);
} else {
value = Math.floor(value / this.props.stepSize) * this.props.stepSize;
value = Math.max(value, this.props.min);
}
this.props.onChange(value);
};
private changeDraftValue = (draftValue: number) => {
this.setState({
isDrafting: true,
draftValue,
});
};
private commitDraftValue = () => {
if (this.state.isDrafting && this.state.draftValue != null) {
const normalized = this.clampAndRound(this.state.draftValue);
this.props.onChange(normalized);
this.setState({
isDrafting: false,
draftValue: undefined,
});
}
};
private clampAndRound = (value: number) => {
if (value <= this.props.min) {
return this.props.min;
} else if (value >= this.props.max) {
return this.props.max;
} else {
return Math.round(value / this.props.stepSize) * this.props.stepSize;
}
};
render() {
const { min, max, value, currency } = this.props;
const { isDrafting, draftValue } = this.state;
return (
<Container>
<InputContainer>
<Button onClick={this.decrease} data-testid="decrease">
<Minus />
</Button>
<AmountInput
value={isDrafting ? draftValue : value}
onChange={this.changeDraftValue}
onBlur={this.commitDraftValue}
pattern="[0-9]*"
data-testid="input"
/>
<Button onClick={this.increase} data-testid="increase">
<Plus />
</Button>
</InputContainer>
<Slider type="range" min={min} max={max} value={value} onChange={this.handleRangeInputChange} data-testid="slider" />
<Range>
<div>{formatMoney(min, currency, false)}</div>
<div>{formatMoney(max, currency, false)}</div>
</Range>
</Container>
);
}
}