Skip to content

Commit 81a380a

Browse files
committed
❇️ Add min and max props to AmountControl
1 parent 0493875 commit 81a380a

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/react-chayns-amountcontrol/component/AmountControl.jsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export default class AmountControl extends PureComponent {
3434
equalize: PropTypes.string,
3535
focusOnClick: PropTypes.bool,
3636
contentWidth: PropTypes.number,
37+
min: PropTypes.number,
38+
max: PropTypes.number,
3739
stopPropagation: PropTypes.bool,
3840
};
3941

@@ -59,6 +61,8 @@ export default class AmountControl extends PureComponent {
5961
focusOnClick: true,
6062
contentWidth: null,
6163
stopPropagation: false,
64+
min: null,
65+
max: null,
6266
plusIcon: faPlus,
6367
minusIcon: faMinus,
6468
removeIcon: faMinus,
@@ -119,15 +123,23 @@ export default class AmountControl extends PureComponent {
119123
}
120124

121125
addItem = () => {
122-
const { amount, onAdd } = this.props;
126+
const { amount, onAdd, max } = this.props;
127+
128+
if (max && amount + 1 > max) {
129+
return;
130+
}
123131

124132
if (onAdd) onAdd();
125133

126134
this.changeAmount(amount + 1);
127135
};
128136

129137
removeItem = () => {
130-
const { amount, onRemove } = this.props;
138+
const { amount, onRemove, min } = this.props;
139+
140+
if (min && amount - 1 < min) {
141+
return;
142+
}
131143

132144
if (onRemove) onRemove();
133145

@@ -145,6 +157,8 @@ export default class AmountControl extends PureComponent {
145157
amount: oldAmount,
146158
disableAdd,
147159
disableRemove,
160+
min,
161+
max,
148162
} = this.props;
149163

150164
if (onInput) {
@@ -153,7 +167,9 @@ export default class AmountControl extends PureComponent {
153167

154168
if (onChange) {
155169
if ((disableAdd && amount > oldAmount)
156-
|| (disableRemove && amount < oldAmount)) {
170+
|| (disableRemove && amount < oldAmount)
171+
|| (min && amount < min)
172+
|| (max && amount > max)) {
157173
this.setState({
158174
tempValue: oldAmount,
159175
});
@@ -191,6 +207,8 @@ export default class AmountControl extends PureComponent {
191207
contentWidth,
192208
stopPropagation,
193209
plusIcon,
210+
max,
211+
min,
194212
} = this.props;
195213
const { tempAmount, tempValue, showInput } = this.state;
196214

@@ -204,7 +222,7 @@ export default class AmountControl extends PureComponent {
204222
stopPropagation={stopPropagation}
205223
icon={this.getRemoveIcon()}
206224
onClick={this.removeItem}
207-
disabled={disabled || disableRemove}
225+
disabled={disabled || disableRemove || (min && (amount <= min))}
208226
className={classNames('cc__amount-control__remove', { 'cc__amount-control--icon': amount > 0 || icon })}
209227
color={(icon && !tempAmount) ? iconColor : removeColor}
210228
/>
@@ -231,7 +249,7 @@ export default class AmountControl extends PureComponent {
231249
stopPropagation={stopPropagation}
232250
icon={plusIcon}
233251
onClick={this.addItem}
234-
disabled={disabled || disableAdd}
252+
disabled={disabled || disableAdd || (max && (amount >= max))}
235253
className={classNames('cc__amount-control__add', { 'cc__amount-control--icon': amount > 0 })}
236254
color={addColor}
237255
/>

0 commit comments

Comments
 (0)