1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.IncrementorButtons = exports.MinusButton = exports.PlusButton = void 0;
|
7 |
|
8 | var _preact = require("preact");
|
9 |
|
10 | var _classnames = _interopRequireDefault(require("classnames"));
|
11 |
|
12 | var _ = require("./");
|
13 |
|
14 | var _decimals = require("./utils/decimals");
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
19 |
|
20 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
21 |
|
22 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
23 |
|
24 | const HOLD_DELAY = 180;
|
25 | const ITERATION_DELAY = 90;
|
26 |
|
27 | const getPlusMinusButton = icon => class extends _preact.Component {
|
28 | constructor(props) {
|
29 | super(props);
|
30 | this.runIt = this.runIt.bind(this);
|
31 | this.onClick = this.onClick.bind(this);
|
32 | this.onPress = this.onPress.bind(this);
|
33 | this.clear = this.clear.bind(this);
|
34 | this.holdStart = false;
|
35 | }
|
36 |
|
37 | runIt() {
|
38 | const {
|
39 | onClick,
|
40 | disabled
|
41 | } = this.props;
|
42 |
|
43 | if (onClick && !disabled) {
|
44 | onClick();
|
45 | }
|
46 | }
|
47 |
|
48 | componentDidMount() {
|
49 | if (this.base && this.base.addEventListener) {
|
50 | this.base.addEventListener('touchstart', this.onPress);
|
51 | this.base.addEventListener('touchend', this.clear);
|
52 | this.base.addEventListener('touchcancel', this.clear);
|
53 | }
|
54 | }
|
55 |
|
56 | componentWillUnmount() {
|
57 | if (this.base && this.base.removeEventListener) {
|
58 | this.base.removeEventListener('touchstart', this.onPress);
|
59 | this.base.removeEventListener('touchend', this.clear);
|
60 | this.base.removeEventListener('touchcancel', this.clear);
|
61 | }
|
62 | }
|
63 |
|
64 | onClick(e) {
|
65 | e.preventDefault();
|
66 | e.stopImmediatePropagation();
|
67 | this.clear();
|
68 | this.runIt();
|
69 | }
|
70 |
|
71 | onPress() {
|
72 | const iterate = () => {
|
73 | clearTimeout(this.iterationTimer);
|
74 | let delay = this.props.iterationDelay || ITERATION_DELAY;
|
75 | const holdDuration = Date.now() - this.holdStart;
|
76 |
|
77 | if (holdDuration > 4000) {
|
78 | delay = delay * 0.4;
|
79 | } else if (holdDuration > 2000) {
|
80 | delay = delay * 0.5;
|
81 | }
|
82 |
|
83 | this.iterationTimer = setTimeout(() => {
|
84 | if (this.holdStart && !this.props.disabled) {
|
85 | this.runIt();
|
86 | iterate();
|
87 | }
|
88 | }, delay);
|
89 | };
|
90 |
|
91 | this.holdTimer = setTimeout(() => {
|
92 | this.holdStart = Date.now();
|
93 | iterate();
|
94 | }, this.props.holdDelay || HOLD_DELAY);
|
95 | }
|
96 |
|
97 | clear() {
|
98 | clearTimeout(this.holdTimer);
|
99 | clearTimeout(this.iterationTimer);
|
100 | this.holdStart = false;
|
101 | }
|
102 |
|
103 | render() {
|
104 | const {
|
105 | className,
|
106 | disabled,
|
107 | name
|
108 | } = this.props;
|
109 | return (0, _preact.h)(_.Button, {
|
110 | "data-e2e": "".concat(name || '').concat(icon.charAt(0).toUpperCase()).concat(icon.slice(1), "Button"),
|
111 | type: "button",
|
112 | onContextMenu: e => {
|
113 | e.preventDefault();
|
114 | e.stopImmediatePropagation();
|
115 | return false;
|
116 | },
|
117 | onClick: this.onClick,
|
118 | onMouseUp: this.clear,
|
119 | onMouseLeave: this.clear,
|
120 | onMouseDown: this.onPress,
|
121 | disabled: disabled,
|
122 | className: (0, _classnames.default)('ba br2 pa2 mv1', {
|
123 | mr2: icon === 'add'
|
124 | }, className)
|
125 | }, (0, _preact.h)("div", {
|
126 | className: "flex items-center"
|
127 | }, (0, _preact.h)(_.Icon, {
|
128 | size: "18",
|
129 | icon: icon
|
130 | }), this.props.children));
|
131 | }
|
132 |
|
133 | };
|
134 |
|
135 | const PlusButton = getPlusMinusButton('add');
|
136 | exports.PlusButton = PlusButton;
|
137 | const MinusButton = getPlusMinusButton('remove');
|
138 | exports.MinusButton = MinusButton;
|
139 |
|
140 | const IncrementorButtons = ({
|
141 | displayValue,
|
142 | updateValue,
|
143 | step = 1,
|
144 | max = Infinity,
|
145 | min = 0,
|
146 | className = '',
|
147 | width = '200px',
|
148 | showStep = true,
|
149 | unit = '',
|
150 | displayValueClasses = 'f4',
|
151 | value,
|
152 | startingValue,
|
153 | formatter,
|
154 | name,
|
155 | style
|
156 | }) => {
|
157 | const targetNumberOfDecimals = (0, _decimals.getNumberOfDecimals)(step);
|
158 | let valueToShow = displayValue != null ? displayValue : value;
|
159 |
|
160 | if (!valueToShow) {
|
161 | valueToShow = targetNumberOfDecimals === 0 ? value : (0, _decimals.ensureDecimals)(value, targetNumberOfDecimals);
|
162 | }
|
163 |
|
164 | if (formatter) {
|
165 | valueToShow = formatter(valueToShow);
|
166 | }
|
167 |
|
168 | let stepDown = step;
|
169 | let stepUp = step;
|
170 | const effectiveValue = value != null ? value : startingValue;
|
171 | return (0, _preact.h)("div", {
|
172 | className: "flex items-center justify-between ".concat(className),
|
173 | style: _objectSpread({
|
174 | width
|
175 | }, style)
|
176 | }, (0, _preact.h)("div", {
|
177 | "data-e2e": "".concat(name, "Value"),
|
178 | className: "dib ".concat(displayValueClasses)
|
179 | }, valueToShow, unit && " ".concat(unit)), (0, _preact.h)("div", null, (0, _preact.h)(PlusButton, {
|
180 | name: name,
|
181 | disabled: effectiveValue + stepUp > max,
|
182 | onClick: () => updateValue((0, _decimals.roundToDecimal)(effectiveValue + stepUp, targetNumberOfDecimals))
|
183 | }, showStep ? step : null), (0, _preact.h)(MinusButton, {
|
184 | name: name,
|
185 | disabled: effectiveValue - stepDown < min,
|
186 | onClick: () => updateValue((0, _decimals.roundToDecimal)(effectiveValue - stepDown, targetNumberOfDecimals))
|
187 | }, showStep ? step : null)));
|
188 | };
|
189 |
|
190 | exports.IncrementorButtons = IncrementorButtons; |
\ | No newline at end of file |