zcomponents-ui 1.2.4 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/{index-DC_KVC71.js → index-DJALegom.js} +3 -9
- package/dist/index-DJALegom.js.map +1 -0
- package/dist/{index-CfSBwowY.js → index-zSB3Isbi.js} +3 -9
- package/dist/index-zSB3Isbi.js.map +1 -0
- package/dist/index.cjs.js +647 -14
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +142 -6
- package/dist/index.esm.js +649 -18
- package/dist/index.esm.js.map +1 -1
- package/dist/integrations-react-hook-form.d-ChOcioDB.d.ts +266 -0
- package/dist/react-hook-form.cjs.js +1 -1
- package/dist/react-hook-form.d.ts +4 -265
- package/dist/react-hook-form.esm.js +1 -1
- package/dist/types/components/ZDrop/integrations/react-hook-form/ZDropField/index.d.ts +3 -12
- package/dist/types/components/ZDrop/integrations/react-hook-form/ZDropField/types/zDropFieldTypes.d.ts +12 -0
- package/dist/types/components/ZDrop/types/zDropTypes.d.ts +2 -2
- package/dist/types/components/ZRange/components/ZRangeIndicator/index.d.ts +3 -0
- package/dist/types/components/ZRange/components/ZRangeInputs/index.d.ts +3 -0
- package/dist/types/components/ZRange/components/ZRangeLabel/index.d.ts +3 -0
- package/dist/types/components/ZRange/components/ZRangeSliderValue/index.d.ts +3 -0
- package/dist/types/components/ZRange/components/ZRangeTrack/index.d.ts +3 -0
- package/dist/types/components/ZRange/helpers/calculateStepPosition.d.ts +1 -0
- package/dist/types/components/ZRange/helpers/checkIsInputSwapAllowed.d.ts +1 -0
- package/dist/types/components/ZRange/helpers/convertToUnit.d.ts +6 -0
- package/dist/types/components/ZRange/helpers/getCssVariablePxValue.d.ts +1 -0
- package/dist/types/components/ZRange/helpers/getFractionDigitsFromStep.d.ts +1 -0
- package/dist/types/components/ZRange/helpers/getMinMax.d.ts +4 -0
- package/dist/types/components/ZRange/index.d.ts +3 -0
- package/dist/types/components/ZRange/integrations/react-hook-form/ZRangeField/index.d.ts +7 -0
- package/dist/types/components/ZRange/integrations/react-hook-form/ZRangeField/types/zRangeFieldTypes.d.ts +17 -0
- package/dist/types/components/ZRange/rangeValue.d.ts +13 -0
- package/dist/types/components/ZRange/types/zRangeTypes.d.ts +103 -0
- package/dist/types/index.d.ts +7 -2
- package/dist/zcomponents-ui.css +1 -1
- package/dist/zcomponents-ui.css.map +1 -1
- package/package.json +1 -1
- package/dist/index-CfSBwowY.js.map +0 -1
- package/dist/index-DC_KVC71.js.map +0 -1
package/dist/index.esm.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { c as classNames, g as getAvailableSpace, u as useOutsideClose } from './index-
|
|
2
|
-
export { a as ZDrop, Z as ZDropField } from './index-
|
|
3
|
-
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
4
|
-
import { useContext, useRef, useState, useCallback, useLayoutEffect, useEffect, createContext } from 'react';
|
|
5
|
-
import 'react-hook-form';
|
|
1
|
+
import { c as classNames, g as getAvailableSpace, u as useOutsideClose } from './index-DJALegom.js';
|
|
2
|
+
export { a as ZDrop, Z as ZDropField } from './index-DJALegom.js';
|
|
3
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
4
|
+
import { useContext, useRef, useState, useCallback, useLayoutEffect, useEffect, createContext, useMemo } from 'react';
|
|
5
|
+
import { useController } from 'react-hook-form';
|
|
6
6
|
|
|
7
|
-
var styles = {"zd-button":"ZDropButton-module_zd-button__XVZ-w","zd-button__toggle":"ZDropButton-module_zd-button__toggle__0jdWn","zd-button__toggle-title":"ZDropButton-module_zd-button__toggle-title__OaiVF","zd-button__toggle-icon":"ZDropButton-module_zd-button__toggle-icon__G4tnb","zd-button__content":"ZDropButton-module_zd-button__content__FS-6m","zd-button__search":"ZDropButton-module_zd-button__search__6Nbrx","zd-button__search-input-field":"ZDropButton-module_zd-button__search-input-field__K2avH","zd-button__search-input":"ZDropButton-module_zd-button__search-input__1e643","zd-button__search-clear-btn":"ZDropButton-module_zd-button__search-clear-btn__hJo2h","zd-button__search-clear-btn-icon":"ZDropButton-module_zd-button__search-clear-btn-icon__PWwHy","zd-button__list":"ZDropButton-module_zd-button__list__prV7p","zd-button__list-item":"ZDropButton-module_zd-button__list-item__uRK0D","zd-button__list-item--active":"ZDropButton-module_zd-button__list-item--active__oPoZA","zd-button__list-item-icon":"ZDropButton-module_zd-button__list-item-icon__xgqOm","zd-button__list-item-title":"ZDropButton-module_zd-button__list-item-title__RHTpc"};
|
|
7
|
+
var styles$1 = {"zd-button":"ZDropButton-module_zd-button__XVZ-w","zd-button__toggle":"ZDropButton-module_zd-button__toggle__0jdWn","zd-button__toggle-title":"ZDropButton-module_zd-button__toggle-title__OaiVF","zd-button__toggle-icon":"ZDropButton-module_zd-button__toggle-icon__G4tnb","zd-button__content":"ZDropButton-module_zd-button__content__FS-6m","zd-button__search":"ZDropButton-module_zd-button__search__6Nbrx","zd-button__search-input-field":"ZDropButton-module_zd-button__search-input-field__K2avH","zd-button__search-input":"ZDropButton-module_zd-button__search-input__1e643","zd-button__search-clear-btn":"ZDropButton-module_zd-button__search-clear-btn__hJo2h","zd-button__search-clear-btn-icon":"ZDropButton-module_zd-button__search-clear-btn-icon__PWwHy","zd-button__list":"ZDropButton-module_zd-button__list__prV7p","zd-button__list-item":"ZDropButton-module_zd-button__list-item__uRK0D","zd-button__list-item--active":"ZDropButton-module_zd-button__list-item--active__oPoZA","zd-button__list-item-icon":"ZDropButton-module_zd-button__list-item-icon__xgqOm","zd-button__list-item-title":"ZDropButton-module_zd-button__list-item-title__RHTpc"};
|
|
8
8
|
|
|
9
9
|
const ButtonDropdownToggle = (props) => {
|
|
10
10
|
const { title, toggleIcon, toggleClassName, children, onButtonToggleClick } = props;
|
|
11
|
-
const toggleClasses = classNames(styles["zd-button__toggle"], toggleClassName);
|
|
11
|
+
const toggleClasses = classNames(styles$1["zd-button__toggle"], toggleClassName);
|
|
12
12
|
const onToggleClick = (e) => {
|
|
13
13
|
onButtonToggleClick();
|
|
14
14
|
};
|
|
15
15
|
const onToggleMouseLeave = (e) => {
|
|
16
16
|
e.currentTarget.blur();
|
|
17
17
|
};
|
|
18
|
-
return (jsxs("button", { className: toggleClasses, onMouseLeave: onToggleMouseLeave, onClick: onToggleClick, children: [toggleIcon && (jsx("div", { className: styles["zd-button__toggle-icon"], children: toggleIcon })), title && (jsx("span", { className: styles["zd-button__toggle-title"], children: title })), children] }));
|
|
18
|
+
return (jsxs("button", { className: toggleClasses, onMouseLeave: onToggleMouseLeave, onClick: onToggleClick, children: [toggleIcon && (jsx("div", { className: styles$1["zd-button__toggle-icon"], children: toggleIcon })), title && (jsx("span", { className: styles$1["zd-button__toggle-title"], children: title })), children] }));
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
const ZDropButtonContent = (props) => {
|
|
@@ -140,19 +140,19 @@ const ZDropButtonContent = (props) => {
|
|
|
140
140
|
if (!isOpen) {
|
|
141
141
|
return null;
|
|
142
142
|
}
|
|
143
|
-
return (jsx("div", { className: styles["zd-button__content"], style: positionStyles, ref: contentRef, children: children }));
|
|
143
|
+
return (jsx("div", { className: styles$1["zd-button__content"], style: positionStyles, ref: contentRef, children: children }));
|
|
144
144
|
};
|
|
145
145
|
|
|
146
146
|
const ZDropButtonList = (props) => {
|
|
147
147
|
const { children } = props;
|
|
148
|
-
return jsx("ul", { className: styles["zd-button__list"], children: children });
|
|
148
|
+
return jsx("ul", { className: styles$1["zd-button__list"], children: children });
|
|
149
149
|
};
|
|
150
150
|
|
|
151
151
|
const ZDropButtonListItem = (props) => {
|
|
152
152
|
const { index, title, Icon, urlPath, className, linkAs: LinkTag = "a", isActive = false, children, } = props;
|
|
153
153
|
const { addToRefs, optionsRef, onItemSelect, searchInputRef } = useContext(ZDropButtonContext);
|
|
154
|
-
const itemContainerClasses = (isActive) => classNames(styles["zd-button__list-item"], {
|
|
155
|
-
[styles["zd-button__list-item--active"]]: !urlPath && isActive,
|
|
154
|
+
const itemContainerClasses = (isActive) => classNames(styles$1["zd-button__list-item"], {
|
|
155
|
+
[styles$1["zd-button__list-item--active"]]: !urlPath && isActive,
|
|
156
156
|
}, className);
|
|
157
157
|
const onItemClick = (e) => {
|
|
158
158
|
if (!urlPath) {
|
|
@@ -216,15 +216,15 @@ const ZDropButtonListItem = (props) => {
|
|
|
216
216
|
};
|
|
217
217
|
const ItemTypeComponent = urlPath ? LinkTag : "div";
|
|
218
218
|
return (jsx("li", { children: jsxs(ItemTypeComponent, { ref: (el) => addToRefs === null || addToRefs === void 0 ? void 0 : addToRefs(el, index), className: itemContainerClasses(isActive), tabIndex: 0, onClick: onItemClick, onMouseEnter: onItemMouseEnter, onMouseLeave: onItemMouseLeave, onKeyDown: onItemKeyDown, ...(urlPath &&
|
|
219
|
-
(LinkTag === "a" ? { href: urlPath } : { to: urlPath })), children: [Icon && (jsx("div", { className: styles["zd-button__list-item-icon"], children: Icon })), jsx("span", { className: styles["zd-button__list-item-title"], children: title }), children] }) }));
|
|
219
|
+
(LinkTag === "a" ? { href: urlPath } : { to: urlPath })), children: [Icon && (jsx("div", { className: styles$1["zd-button__list-item-icon"], children: Icon })), jsx("span", { className: styles$1["zd-button__list-item-title"], children: title }), children] }) }));
|
|
220
220
|
};
|
|
221
221
|
|
|
222
222
|
const ZDropButtonSearch = (props) => {
|
|
223
223
|
const { placeholder, searchIcon, clearIcon, searchClassName, shouldFocusOnOpen = false, } = props;
|
|
224
224
|
const { onSearch, searchInputRef, optionsRef, isOpen } = useContext(ZDropButtonContext);
|
|
225
225
|
const [isSearchActive, setIsSearchActive] = useState(false);
|
|
226
|
-
const searchFormClasses = classNames(styles["zd-button__search"]);
|
|
227
|
-
const searchInputFieldClasses = classNames(styles["zd-button__search-input-field"], { active: isSearchActive }, searchClassName);
|
|
226
|
+
const searchFormClasses = classNames(styles$1["zd-button__search"]);
|
|
227
|
+
const searchInputFieldClasses = classNames(styles$1["zd-button__search-input-field"], { active: isSearchActive }, searchClassName);
|
|
228
228
|
const onInputChange = (e) => {
|
|
229
229
|
onSearch === null || onSearch === void 0 ? void 0 : onSearch(e);
|
|
230
230
|
};
|
|
@@ -268,7 +268,7 @@ const ZDropButtonSearch = (props) => {
|
|
|
268
268
|
searchInputRef.current.focus();
|
|
269
269
|
}
|
|
270
270
|
}, []);
|
|
271
|
-
return (jsx("form", { className: searchFormClasses, onMouseEnter: onSearchMouseEnter, onMouseLeave: onSearchMouseLeave, onKeyDown: onSearchKeyDown, children: jsxs("div", { className: searchInputFieldClasses, children: [searchIcon && jsx("div", { children: searchIcon }), jsx("input", { className: styles["zd-button__search-input"], onChange: onInputChange, ref: searchInputRef, placeholder: placeholder }), jsx("button", { className: styles["zd-button__search-clear-btn"], type: "button", onClick: onClearClick, children: clearIcon || (jsx("span", { className: styles["zd-button__search-clear-btn-icon"] })) })] }) }));
|
|
271
|
+
return (jsx("form", { className: searchFormClasses, onMouseEnter: onSearchMouseEnter, onMouseLeave: onSearchMouseLeave, onKeyDown: onSearchKeyDown, children: jsxs("div", { className: searchInputFieldClasses, children: [searchIcon && jsx("div", { children: searchIcon }), jsx("input", { className: styles$1["zd-button__search-input"], onChange: onInputChange, ref: searchInputRef, placeholder: placeholder }), jsx("button", { className: styles$1["zd-button__search-clear-btn"], type: "button", onClick: onClearClick, children: clearIcon || (jsx("span", { className: styles$1["zd-button__search-clear-btn-icon"] })) })] }) }));
|
|
272
272
|
};
|
|
273
273
|
|
|
274
274
|
const ZDropButtonContext = createContext({});
|
|
@@ -278,7 +278,7 @@ const ZDropButton = (props) => {
|
|
|
278
278
|
const optionsRef = useRef([]);
|
|
279
279
|
const searchInputRef = useRef(null);
|
|
280
280
|
const [isOpen, setIsOpen] = useState(false);
|
|
281
|
-
const buttonContainerClasses = classNames(styles["zd-button"], className);
|
|
281
|
+
const buttonContainerClasses = classNames(styles$1["zd-button"], className);
|
|
282
282
|
const addToRefs = (element, index) => {
|
|
283
283
|
return (optionsRef.current[index] = element);
|
|
284
284
|
};
|
|
@@ -317,5 +317,636 @@ ZDropButton.List = ZDropButtonList;
|
|
|
317
317
|
ZDropButton.Item = ZDropButtonListItem;
|
|
318
318
|
ZDropButton.Search = ZDropButtonSearch;
|
|
319
319
|
|
|
320
|
-
|
|
320
|
+
const thumbInteractArea = 24;
|
|
321
|
+
const checkIsInputSwapAllowed = (activeIndex, comparedIndex, clientPosition, valuePosition) => {
|
|
322
|
+
return (activeIndex === comparedIndex &&
|
|
323
|
+
clientPosition !== null &&
|
|
324
|
+
clientPosition !== undefined &&
|
|
325
|
+
clientPosition < valuePosition + thumbInteractArea &&
|
|
326
|
+
clientPosition > valuePosition - thumbInteractArea);
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const getMinMax = (firstValue, secondValue) => {
|
|
330
|
+
const min = firstValue < secondValue ? firstValue : secondValue;
|
|
331
|
+
const max = firstValue > secondValue ? firstValue : secondValue;
|
|
332
|
+
return { min, max };
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
// import { UnitList } from "../types/zRangeTypes";
|
|
336
|
+
const convertToUnit = (rawValue, units = [], unitDivisors = [], isIndicatorUnitHidden = false, scaleFn = (x) => x) => {
|
|
337
|
+
var _a, _b;
|
|
338
|
+
if (!units.length)
|
|
339
|
+
return String(rawValue);
|
|
340
|
+
if (!Number.isFinite(rawValue))
|
|
341
|
+
return String(rawValue);
|
|
342
|
+
const scaled = scaleFn(rawValue);
|
|
343
|
+
const sign = scaled < 0 ? "-" : "";
|
|
344
|
+
const value = Math.abs(scaled);
|
|
345
|
+
const divisors = unitDivisors
|
|
346
|
+
.slice(0, units.length)
|
|
347
|
+
.map((d) => (d > 0 ? d : 1));
|
|
348
|
+
if (!divisors.length) {
|
|
349
|
+
return `${sign}${value.toFixed(units[0].fractionDigits)} ${units[0].unit}`;
|
|
350
|
+
}
|
|
351
|
+
let idx = 0;
|
|
352
|
+
for (let i = 0; i < units.length; i++) {
|
|
353
|
+
const d = (_a = divisors[i]) !== null && _a !== void 0 ? _a : 1;
|
|
354
|
+
if (value >= d)
|
|
355
|
+
idx = i;
|
|
356
|
+
}
|
|
357
|
+
const d = (_b = divisors[idx]) !== null && _b !== void 0 ? _b : 1;
|
|
358
|
+
const converted = value / d;
|
|
359
|
+
if (isIndicatorUnitHidden) {
|
|
360
|
+
return `${sign}${converted.toFixed(units[idx].fractionDigits)}`;
|
|
361
|
+
}
|
|
362
|
+
return `${sign}${converted.toFixed(units[idx].fractionDigits)} ${units[idx].unit}`;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const calculateStepPosition = (currentValue, element, min, max) => {
|
|
366
|
+
if (!element) {
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
return ((currentValue - min) * element.clientWidth) / (max - min);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
class ZRangeValue {
|
|
373
|
+
constructor(min, max, unitDivisors, unitList, scale) {
|
|
374
|
+
this.min = min;
|
|
375
|
+
this.max = max;
|
|
376
|
+
this.unitDivisors = unitDivisors;
|
|
377
|
+
this.unitList = unitList;
|
|
378
|
+
this.scale = scale;
|
|
379
|
+
this.min.getPosition = this.getMinPosition.bind(this);
|
|
380
|
+
this.max.getPosition = this.getMaxPosition.bind(this);
|
|
381
|
+
}
|
|
382
|
+
getMinPosition() {
|
|
383
|
+
return calculateStepPosition(this.min.value, this.min.inputRef.current, this.min.initialValue, this.max.initialValue);
|
|
384
|
+
}
|
|
385
|
+
getMaxPosition() {
|
|
386
|
+
return calculateStepPosition(this.max.value, this.max.inputRef.current, this.min.initialValue, this.max.initialValue);
|
|
387
|
+
}
|
|
388
|
+
getRangeMiddle() {
|
|
389
|
+
return (this.max.value - this.min.value) / 2;
|
|
390
|
+
}
|
|
391
|
+
getValueLabel(forValue) {
|
|
392
|
+
const value = getMinMax(this.min.value, this.max.value)[forValue];
|
|
393
|
+
const valueLabel = this.scale ? this.scale(value) : value;
|
|
394
|
+
return this.unitList
|
|
395
|
+
? convertToUnit(valueLabel, this.unitList, this.unitDivisors).toString()
|
|
396
|
+
: `${valueLabel}`;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
var styles = {"zr":"ZRange-module_zr__zQajG","zr__container":"ZRange-module_zr__container__XLdRx","zr__label":"ZRange-module_zr__label__eddhI","zr__value-indicator-icon":"ZRange-module_zr__value-indicator-icon__iCqcS","zr__inputs-wrapper":"ZRange-module_zr__inputs-wrapper__yzQxk","zr__slider":"ZRange-module_zr__slider__pD1kE","zr__slider-track":"ZRange-module_zr__slider-track__sZvOt","zr__slider-track-selected-range":"ZRange-module_zr__slider-track-selected-range__lXJE1","zr__slider-values-wrapper":"ZRange-module_zr__slider-values-wrapper__vxSnT","zr__slider-value":"ZRange-module_zr__slider-value__W9jMF","zr__slider-first-value":"ZRange-module_zr__slider-first-value__e7Ik6","zr__error-message":"ZRange-module_zr__error-message__QMDqT"};
|
|
401
|
+
|
|
402
|
+
const getCssVariablePxValue = (element, cssVariableName, fallbackPx) => {
|
|
403
|
+
const rawValue = getComputedStyle(element)
|
|
404
|
+
.getPropertyValue(cssVariableName)
|
|
405
|
+
.trim();
|
|
406
|
+
const parsedPx = parseFloat(rawValue);
|
|
407
|
+
return Number.isFinite(parsedPx) ? parsedPx : fallbackPx;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const ZRangeSliderValue = (props) => {
|
|
411
|
+
const { value, icon, sliderValueClassName } = props;
|
|
412
|
+
const thumbClasses = classNames(styles["zr__slider-value"], styles["zr__slider-first-value"], sliderValueClassName);
|
|
413
|
+
return (jsxs("div", { className: thumbClasses, children: [icon && jsx("span", { children: icon }), jsx("span", { children: value })] }));
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const indicatorTopGap = 2; // px
|
|
417
|
+
const ZRangeIndicator = (props) => {
|
|
418
|
+
const { valueIndicatorRef, indicatorStyles, indicatorValue, rangeTrackRef, inputElement, scaleFunction, unitList, unitDivisors, scale, icon, iconKey, isHighlighted, isIndicatorUnitHidden = false, } = props;
|
|
419
|
+
const [valueIndicatorStyles, setValueIndicatorStyles] = useState({});
|
|
420
|
+
const indicatorClasses = classNames("zr__value-indicator", {
|
|
421
|
+
"zr__value-indicator--active": typeof indicatorValue === "number" && isHighlighted,
|
|
422
|
+
}, indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicator, {
|
|
423
|
+
...((indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMin)
|
|
424
|
+
? { [indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMin]: iconKey === "min" }
|
|
425
|
+
: {}),
|
|
426
|
+
...((indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMax)
|
|
427
|
+
? { [indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMax]: iconKey === "max" }
|
|
428
|
+
: {}),
|
|
429
|
+
});
|
|
430
|
+
const indicatorIconClasses = classNames(styles["zr__value-indicator-icon"], indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorIcon);
|
|
431
|
+
const indicatorValueClasses = classNames("zr__value-indicator-value", indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorValue);
|
|
432
|
+
useLayoutEffect(() => {
|
|
433
|
+
var _a;
|
|
434
|
+
if (((_a = rangeTrackRef === null || rangeTrackRef === void 0 ? void 0 : rangeTrackRef.current) === null || _a === void 0 ? void 0 : _a.offsetHeight) && inputElement) {
|
|
435
|
+
setValueIndicatorStyles({
|
|
436
|
+
bottom: `${rangeTrackRef.current.offsetHeight +
|
|
437
|
+
getCssVariablePxValue(inputElement, "--thumb-size", 16) / 2 +
|
|
438
|
+
indicatorTopGap}px`,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}, [indicatorValue, valueIndicatorRef, rangeTrackRef]);
|
|
442
|
+
return (jsxs("div", { ref: valueIndicatorRef, style: valueIndicatorStyles, className: indicatorClasses, children: [icon != null && typeof indicatorValue === "number" && (jsx("div", { className: indicatorIconClasses, children: icon }, iconKey !== null && iconKey !== void 0 ? iconKey : "indicator-icon")), typeof indicatorValue === "number" && (jsx("span", { className: indicatorValueClasses, children: convertToUnit(scale ? scaleFunction(indicatorValue) : indicatorValue, unitList, unitDivisors, isIndicatorUnitHidden) }))] }));
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
const ZRangeTrack = (props) => {
|
|
446
|
+
const { rangeTrackRef, selectedRangeRef, selectedRangeStyle, trackStyles } = props;
|
|
447
|
+
const { trackContainer, trackRange, trackSelected } = trackStyles;
|
|
448
|
+
const trackClasses = classNames(styles["zr__slider"], trackContainer);
|
|
449
|
+
const rangeClasses = classNames(styles["zr__slider-track"], trackRange);
|
|
450
|
+
const selectedRangeClasses = classNames(styles["zr__slider-track-selected-range"], trackSelected);
|
|
451
|
+
return (jsxs("div", { className: trackClasses, children: [jsx("div", { ref: rangeTrackRef, className: rangeClasses }), jsx("div", { ref: selectedRangeRef, className: selectedRangeClasses, style: selectedRangeStyle })] }));
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
const ZRangeInputs = (props) => {
|
|
455
|
+
const { rangeValue, min, max, step, inputMinClasses, inputMaxClasses, onInputChange, onMouseDown, onTouchStart, onMouseMove, onTouchMove, onMouseUp, onTouchEnd, } = props;
|
|
456
|
+
return (jsxs("div", { className: styles["zr__inputs-wrapper"], children: [jsx("input", { ref: rangeValue.min.inputRef, name: "0", type: "range", min: min, max: max, step: typeof step === "number" ? step : "any", value: rangeValue.min.value, className: inputMinClasses, onInput: onInputChange, onMouseDown: onMouseDown, onTouchStart: onTouchStart, onMouseMove: onMouseMove, onTouchMove: onTouchMove, onMouseUp: onMouseUp, onTouchEnd: onTouchEnd }), jsx("input", { ref: rangeValue.max.inputRef, name: "1", type: "range", min: min, max: max, step: typeof step === "number" ? step : "any", value: rangeValue.max.value, className: inputMaxClasses, onInput: onInputChange, onMouseDown: onMouseDown, onTouchStart: onTouchStart, onMouseMove: onMouseMove, onTouchMove: onTouchMove, onMouseUp: onMouseUp, onTouchEnd: onTouchEnd })] }));
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const ZRangeLabel = (props) => {
|
|
460
|
+
const { label, labelClassName } = props;
|
|
461
|
+
const labelClasses = classNames(styles["zr__label"], labelClassName);
|
|
462
|
+
return jsx("div", { className: labelClasses, children: label });
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
const getFractionDigitsFromStep = (step) => {
|
|
466
|
+
if (!Number.isFinite(step)) {
|
|
467
|
+
return 0;
|
|
468
|
+
}
|
|
469
|
+
const stepAsString = String(step);
|
|
470
|
+
const scientificNotationMatch = stepAsString.match(/e-(\d+)/i);
|
|
471
|
+
if (scientificNotationMatch) {
|
|
472
|
+
return Number(scientificNotationMatch[1]);
|
|
473
|
+
}
|
|
474
|
+
const dot = stepAsString.indexOf(".");
|
|
475
|
+
return dot >= 0 ? stepAsString.length - dot - 1 : 0;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
const clamp01 = (v) => Math.min(1, Math.max(0, v));
|
|
479
|
+
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
|
480
|
+
const normalizeToStep = (value, step) => {
|
|
481
|
+
const digits = getFractionDigitsFromStep(step);
|
|
482
|
+
return Number(value.toFixed(digits));
|
|
483
|
+
};
|
|
484
|
+
const ZRange = (props) => {
|
|
485
|
+
var _a;
|
|
486
|
+
const { value, name, min, max, step, scale, unitDivisors, unitList, label, onSelect, onChange, icons, stylesClasses, isIndicatorUnitHidden, } = props;
|
|
487
|
+
const scaleFunction = (x) =>
|
|
488
|
+
// eslint-disable-next-line no-new-func
|
|
489
|
+
scale ? new Function("x", `return ${scale}`)(x) : x;
|
|
490
|
+
const containerRef = useRef(null);
|
|
491
|
+
const rangeTrackRef = useRef(null);
|
|
492
|
+
const selectedRangeRef = useRef(null);
|
|
493
|
+
const valueIndicatorRef = useRef(null);
|
|
494
|
+
const thumbOffsetRef = useRef({ 0: 0, 1: 0 });
|
|
495
|
+
const [indicatorValue, setIndicatorValue] = useState(undefined);
|
|
496
|
+
const [activeInputIndex, setActiveInputIndex] = useState(0);
|
|
497
|
+
const [rangeValue, setRangeValue] = useState(new ZRangeValue({
|
|
498
|
+
value: typeof (value === null || value === void 0 ? void 0 : value.min) === "number" ? value.min : min,
|
|
499
|
+
initialValue: min,
|
|
500
|
+
inputRef: useRef(null),
|
|
501
|
+
}, {
|
|
502
|
+
value: typeof (value === null || value === void 0 ? void 0 : value.max) === "number" ? value.max : max,
|
|
503
|
+
initialValue: max,
|
|
504
|
+
inputRef: useRef(null),
|
|
505
|
+
}, unitDivisors, unitList, scaleFunction));
|
|
506
|
+
const [clientPositionX, setClientPositionX] = useState(null);
|
|
507
|
+
const [isMoving, setIsMoving] = useState(false);
|
|
508
|
+
const [isHighlighted, setIsHighlighted] = useState(false);
|
|
509
|
+
const [selectedRangeStyle, setSelectedRangeStyle] = useState({
|
|
510
|
+
width: 0,
|
|
511
|
+
left: 0,
|
|
512
|
+
right: 0,
|
|
513
|
+
});
|
|
514
|
+
const stepValue = typeof step === "number" && step > 0 ? step : 1;
|
|
515
|
+
const maxForInput = min + Math.ceil((max - min) / stepValue) * stepValue;
|
|
516
|
+
const mapValueToInputRange = (domainValue) => domainValue >= max ? maxForInput : domainValue;
|
|
517
|
+
const mapInputRangeToValue = (inputValue) => Math.min(inputValue, max);
|
|
518
|
+
const rangeValueForInput = useMemo(() => {
|
|
519
|
+
return new ZRangeValue({
|
|
520
|
+
...rangeValue.min,
|
|
521
|
+
value: mapValueToInputRange(rangeValue.min.value),
|
|
522
|
+
initialValue: min,
|
|
523
|
+
inputRef: rangeValue.min.inputRef,
|
|
524
|
+
}, {
|
|
525
|
+
...rangeValue.max,
|
|
526
|
+
value: mapValueToInputRange(rangeValue.max.value),
|
|
527
|
+
initialValue: maxForInput,
|
|
528
|
+
inputRef: rangeValue.max.inputRef,
|
|
529
|
+
}, unitDivisors, unitList, scaleFunction);
|
|
530
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
531
|
+
}, [
|
|
532
|
+
rangeValue.min.value,
|
|
533
|
+
rangeValue.max.value,
|
|
534
|
+
min,
|
|
535
|
+
max,
|
|
536
|
+
maxForInput,
|
|
537
|
+
stepValue,
|
|
538
|
+
unitDivisors,
|
|
539
|
+
unitList,
|
|
540
|
+
scale,
|
|
541
|
+
]);
|
|
542
|
+
const containerClasses = classNames(styles["zr__container"], stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.container);
|
|
543
|
+
const inputMinClasses = classNames("zr__input", "zr__input-first", {
|
|
544
|
+
"zr__input--active": activeInputIndex === 0 && isHighlighted,
|
|
545
|
+
}, stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.input, activeInputIndex === 0 && isHighlighted && (stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive)
|
|
546
|
+
? stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive
|
|
547
|
+
: "");
|
|
548
|
+
const inputMaxClasses = classNames("zr__input", "zr__input-second", { "zr__input--active": activeInputIndex === 1 && isHighlighted }, stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.input, activeInputIndex === 1 && isHighlighted && (stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive)
|
|
549
|
+
? stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive
|
|
550
|
+
: "");
|
|
551
|
+
useEffect(() => {
|
|
552
|
+
if (!value)
|
|
553
|
+
return;
|
|
554
|
+
const nextMin = clamp(value.min, min, max);
|
|
555
|
+
const nextMax = clamp(value.max, min, max);
|
|
556
|
+
const nextRangeValue = new ZRangeValue({
|
|
557
|
+
...rangeValue.min,
|
|
558
|
+
value: nextMin,
|
|
559
|
+
initialValue: min,
|
|
560
|
+
inputRef: rangeValue.min.inputRef,
|
|
561
|
+
}, {
|
|
562
|
+
...rangeValue.max,
|
|
563
|
+
value: nextMax,
|
|
564
|
+
initialValue: max,
|
|
565
|
+
inputRef: rangeValue.max.inputRef,
|
|
566
|
+
}, unitDivisors, unitList, scaleFunction);
|
|
567
|
+
setRangeValue(nextRangeValue);
|
|
568
|
+
setIndicatorValue(undefined);
|
|
569
|
+
thumbOffsetRef.current[0] = 0;
|
|
570
|
+
thumbOffsetRef.current[1] = 0;
|
|
571
|
+
requestAnimationFrame(() => {
|
|
572
|
+
const val = activeInputIndex === 0 ? nextMin : nextMax;
|
|
573
|
+
setRangeValuePosition(val);
|
|
574
|
+
});
|
|
575
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
576
|
+
}, [value === null || value === void 0 ? void 0 : value.min, value === null || value === void 0 ? void 0 : value.max, min, max]);
|
|
577
|
+
const getThumbCenterXInContainer = (input, currentValueReal, containerEl) => {
|
|
578
|
+
const currentValue = mapValueToInputRange(currentValueReal);
|
|
579
|
+
const thumbTravelRatio = clamp01((currentValue - min) / (maxForInput - min));
|
|
580
|
+
const inputRect = input.getBoundingClientRect();
|
|
581
|
+
const containerRect = containerEl.getBoundingClientRect();
|
|
582
|
+
const thumbSize = getCssVariablePxValue(input, "--thumb-size", 16);
|
|
583
|
+
const usable = inputRect.width - thumbSize;
|
|
584
|
+
const thumbCenterXInInput = thumbSize / 2 + usable * thumbTravelRatio;
|
|
585
|
+
const inputLeftInContainer = inputRect.left - containerRect.left;
|
|
586
|
+
return inputLeftInContainer + thumbCenterXInInput;
|
|
587
|
+
};
|
|
588
|
+
const setRangeValuePosition = (currentValueReal, inputIndex = activeInputIndex) => {
|
|
589
|
+
var _a;
|
|
590
|
+
const indicator = valueIndicatorRef.current;
|
|
591
|
+
const container = containerRef.current;
|
|
592
|
+
const input = (_a = (inputIndex === 0
|
|
593
|
+
? rangeValue.min.inputRef.current
|
|
594
|
+
: rangeValue.max.inputRef.current)) !== null && _a !== void 0 ? _a : rangeValue.min.inputRef.current;
|
|
595
|
+
if (!indicator || !container || !input)
|
|
596
|
+
return;
|
|
597
|
+
const distanceLeft = getThumbCenterXInContainer(input, currentValueReal, container);
|
|
598
|
+
indicator.style.left = `${Math.round(distanceLeft)}px`;
|
|
599
|
+
indicator.style.transform = `translateX(-50%)`;
|
|
600
|
+
};
|
|
601
|
+
const updateRangeValues = (currentValueReal, inputIndex) => {
|
|
602
|
+
const normalizedValue = normalizeToStep(currentValueReal, stepValue);
|
|
603
|
+
const updatedRangeValue = new ZRangeValue({
|
|
604
|
+
...(inputIndex === 0
|
|
605
|
+
? { ...rangeValue.min, value: normalizedValue }
|
|
606
|
+
: rangeValue.min),
|
|
607
|
+
}, {
|
|
608
|
+
...(inputIndex === 0
|
|
609
|
+
? rangeValue.max
|
|
610
|
+
: { ...rangeValue.max, value: normalizedValue }),
|
|
611
|
+
}, rangeValue.unitDivisors, rangeValue.unitList, rangeValue.scale);
|
|
612
|
+
setRangeValue(updatedRangeValue);
|
|
613
|
+
setIndicatorValue(normalizedValue);
|
|
614
|
+
if (!onChange)
|
|
615
|
+
return;
|
|
616
|
+
const { min: currentMin, max: currentMax } = getMinMax(updatedRangeValue.min.value, updatedRangeValue.max.value);
|
|
617
|
+
onChange({
|
|
618
|
+
...updatedRangeValue,
|
|
619
|
+
min: { ...updatedRangeValue.min, value: currentMin },
|
|
620
|
+
max: { ...updatedRangeValue.max, value: currentMax },
|
|
621
|
+
}, name);
|
|
622
|
+
};
|
|
623
|
+
const onInputChange = (e) => {
|
|
624
|
+
if (!rangeTrackRef.current ||
|
|
625
|
+
!rangeValue.min.inputRef.current ||
|
|
626
|
+
!rangeValue.max.inputRef.current) {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
const rawValue = mapInputRangeToValue(Number(e.target.value));
|
|
630
|
+
const snappedValue = Math.round((rawValue - min) / stepValue) * stepValue + min;
|
|
631
|
+
const currentValue = clamp(snappedValue, min, max);
|
|
632
|
+
const rangeTrackRect = rangeTrackRef.current.getBoundingClientRect();
|
|
633
|
+
const clientPositionOnTrack = clientPositionX != null ? clientPositionX - rangeTrackRect.left : null;
|
|
634
|
+
const minValueStepPosition = rangeValue.getMinPosition();
|
|
635
|
+
const maxValueStepPosition = rangeValue.getMaxPosition();
|
|
636
|
+
if (checkIsInputSwapAllowed(activeInputIndex, 0, clientPositionOnTrack, maxValueStepPosition)) {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
if (checkIsInputSwapAllowed(activeInputIndex, 1, clientPositionOnTrack, minValueStepPosition)) {
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
const { min: currentMin, max: currentMax } = getMinMax(rangeValue.min.value, rangeValue.max.value);
|
|
643
|
+
const rangeMiddle = (currentMax - currentMin) / 2;
|
|
644
|
+
if (rangeValue.max.value > rangeValue.min.value) {
|
|
645
|
+
if (currentValue < currentMin + rangeMiddle) {
|
|
646
|
+
setActiveInputIndex(0);
|
|
647
|
+
setRangeValuePosition(currentValue);
|
|
648
|
+
updateRangeValues(currentValue, 0);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
if (currentValue > currentMin + rangeMiddle) {
|
|
652
|
+
setActiveInputIndex(1);
|
|
653
|
+
setRangeValuePosition(currentValue);
|
|
654
|
+
updateRangeValues(currentValue, 1);
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
if (rangeValue.min.value > rangeValue.max.value) {
|
|
659
|
+
if (currentValue < currentMin + rangeMiddle) {
|
|
660
|
+
setActiveInputIndex(1);
|
|
661
|
+
setRangeValuePosition(currentValue);
|
|
662
|
+
updateRangeValues(currentValue, 1);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
if (currentValue > currentMin + rangeMiddle) {
|
|
666
|
+
setActiveInputIndex(0);
|
|
667
|
+
setRangeValuePosition(currentValue);
|
|
668
|
+
updateRangeValues(currentValue, 0);
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
setRangeValuePosition(currentValue);
|
|
673
|
+
updateRangeValues(currentValue, activeInputIndex);
|
|
674
|
+
};
|
|
675
|
+
const onMoveStart = (e) => {
|
|
676
|
+
setIsMoving(true);
|
|
677
|
+
setIsHighlighted(true);
|
|
678
|
+
const clientX = "clientX" in e ? e.clientX : e.touches[0].clientX;
|
|
679
|
+
setClientPositionX(clientX);
|
|
680
|
+
const container = containerRef.current;
|
|
681
|
+
const input = activeInputIndex === 0
|
|
682
|
+
? rangeValue.min.inputRef.current
|
|
683
|
+
: rangeValue.max.inputRef.current;
|
|
684
|
+
if (container && input) {
|
|
685
|
+
const containerRect = container.getBoundingClientRect();
|
|
686
|
+
const pointerXInContainer = clientX - containerRect.left;
|
|
687
|
+
const currentValReal = activeInputIndex === 0 ? rangeValue.min.value : rangeValue.max.value;
|
|
688
|
+
const thumbXInContainer = getThumbCenterXInContainer(input, currentValReal, container);
|
|
689
|
+
const thumbSize = getCssVariablePxValue(input, "--thumb-size", 16);
|
|
690
|
+
const dist = Math.abs(pointerXInContainer - thumbXInContainer);
|
|
691
|
+
thumbOffsetRef.current[activeInputIndex] =
|
|
692
|
+
dist <= thumbSize / 2 ? pointerXInContainer - thumbXInContainer : 0;
|
|
693
|
+
}
|
|
694
|
+
if (!rangeTrackRef.current ||
|
|
695
|
+
!rangeValue.min.inputRef.current ||
|
|
696
|
+
!rangeValue.max.inputRef.current) {
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
const rangeTrackRect = rangeTrackRef.current.getBoundingClientRect();
|
|
700
|
+
const clientPositionOnTrack = clientX - rangeTrackRect.left;
|
|
701
|
+
const minValueStepPosition = rangeValue.getMinPosition();
|
|
702
|
+
const maxValueStepPosition = rangeValue.getMaxPosition();
|
|
703
|
+
if (checkIsInputSwapAllowed(activeInputIndex, 0, clientPositionOnTrack, maxValueStepPosition)) {
|
|
704
|
+
setActiveInputIndex(1);
|
|
705
|
+
thumbOffsetRef.current[1] = 0;
|
|
706
|
+
setRangeValuePosition(rangeValue.max.value, 1);
|
|
707
|
+
updateRangeValues(rangeValue.max.value, 1);
|
|
708
|
+
}
|
|
709
|
+
if (checkIsInputSwapAllowed(activeInputIndex, 1, clientPositionOnTrack, minValueStepPosition)) {
|
|
710
|
+
setActiveInputIndex(0);
|
|
711
|
+
thumbOffsetRef.current[0] = 0;
|
|
712
|
+
setRangeValuePosition(rangeValue.min.value);
|
|
713
|
+
updateRangeValues(rangeValue.min.value, 0);
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
const onMove = (e) => {
|
|
717
|
+
var _a, _b;
|
|
718
|
+
if (!isMoving)
|
|
719
|
+
return;
|
|
720
|
+
e.preventDefault();
|
|
721
|
+
e.stopPropagation();
|
|
722
|
+
const currentPositionX = "clientX" in e ? e.clientX : (_b = (_a = e.touches) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.clientX;
|
|
723
|
+
const element = activeInputIndex === 0
|
|
724
|
+
? rangeValue.min.inputRef
|
|
725
|
+
: rangeValue.max.inputRef;
|
|
726
|
+
if (!(element.current && currentPositionX != null))
|
|
727
|
+
return;
|
|
728
|
+
const slider = element.current;
|
|
729
|
+
const sliderRect = slider.getBoundingClientRect();
|
|
730
|
+
const offsetX = currentPositionX -
|
|
731
|
+
sliderRect.left -
|
|
732
|
+
thumbOffsetRef.current[activeInputIndex];
|
|
733
|
+
const thumbSize = getCssVariablePxValue(slider, "--thumb-size", 16);
|
|
734
|
+
const usable = Math.max(1, sliderRect.width - thumbSize);
|
|
735
|
+
const posOnUsable = clamp(offsetX - thumbSize / 2, 0, usable);
|
|
736
|
+
const ratio = posOnUsable / usable;
|
|
737
|
+
const rawValue = min + ratio * (maxForInput - min);
|
|
738
|
+
const roundedValue = Math.round((rawValue - min) / stepValue) * stepValue + min;
|
|
739
|
+
const limitedInputValue = clamp(roundedValue, min, maxForInput);
|
|
740
|
+
const limitedRealValue = mapInputRangeToValue(limitedInputValue);
|
|
741
|
+
setRangeValuePosition(limitedRealValue, activeInputIndex);
|
|
742
|
+
updateRangeValues(limitedRealValue, activeInputIndex);
|
|
743
|
+
};
|
|
744
|
+
const onMoveStop = (e) => {
|
|
745
|
+
var _a, _b, _c;
|
|
746
|
+
setIsMoving(false);
|
|
747
|
+
setIsHighlighted(false);
|
|
748
|
+
if (!((_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.value) &&
|
|
749
|
+
rangeValue.min.inputRef.current &&
|
|
750
|
+
rangeValue.max.inputRef.current) {
|
|
751
|
+
const firstValue = activeInputIndex === 0
|
|
752
|
+
? mapInputRangeToValue(Number((_b = rangeValue.min.inputRef.current) === null || _b === void 0 ? void 0 : _b.value))
|
|
753
|
+
: rangeValue.min.value;
|
|
754
|
+
const secondValue = activeInputIndex === 1
|
|
755
|
+
? mapInputRangeToValue(Number((_c = rangeValue.max.inputRef.current) === null || _c === void 0 ? void 0 : _c.value))
|
|
756
|
+
: rangeValue.max.value;
|
|
757
|
+
const { min: lastMin, max: lastMax } = getMinMax(firstValue, secondValue);
|
|
758
|
+
onSelect === null || onSelect === void 0 ? void 0 : onSelect({
|
|
759
|
+
...rangeValue,
|
|
760
|
+
min: { ...rangeValue.min, value: lastMin },
|
|
761
|
+
max: { ...rangeValue.max, value: lastMax },
|
|
762
|
+
}, name);
|
|
763
|
+
updateRangeValues(activeInputIndex === 0 ? lastMin : lastMax, activeInputIndex);
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
const targetValueReal = mapInputRangeToValue(Number(e.target.value));
|
|
767
|
+
const { min: currentMin, max: currentMax } = getMinMax(e.target.name === "0"
|
|
768
|
+
? targetValueReal
|
|
769
|
+
: rangeValue.min.value, e.target.name === "0"
|
|
770
|
+
? rangeValue.max.value
|
|
771
|
+
: targetValueReal);
|
|
772
|
+
onSelect === null || onSelect === void 0 ? void 0 : onSelect({
|
|
773
|
+
...rangeValue,
|
|
774
|
+
min: { ...rangeValue.min, value: currentMin },
|
|
775
|
+
max: { ...rangeValue.max, value: currentMax },
|
|
776
|
+
}, name);
|
|
777
|
+
};
|
|
778
|
+
const onMoveEnd = (e) => {
|
|
779
|
+
onMoveStop(e);
|
|
780
|
+
e.stopPropagation();
|
|
781
|
+
};
|
|
782
|
+
useEffect(() => {
|
|
783
|
+
if (!isMoving)
|
|
784
|
+
return;
|
|
785
|
+
const handleTouchEnd = () => {
|
|
786
|
+
setIsMoving(false);
|
|
787
|
+
setIsHighlighted(false);
|
|
788
|
+
};
|
|
789
|
+
document.addEventListener("touchend", handleTouchEnd);
|
|
790
|
+
return () => document.removeEventListener("touchend", handleTouchEnd);
|
|
791
|
+
}, [isMoving]);
|
|
792
|
+
useEffect(() => {
|
|
793
|
+
if (!isMoving)
|
|
794
|
+
return;
|
|
795
|
+
const handleMouseUp = () => {
|
|
796
|
+
setIsMoving(false);
|
|
797
|
+
setIsHighlighted(false);
|
|
798
|
+
};
|
|
799
|
+
document.addEventListener("mouseup", handleMouseUp);
|
|
800
|
+
return () => document.removeEventListener("mouseup", handleMouseUp);
|
|
801
|
+
}, [isMoving]);
|
|
802
|
+
const calculateSelectedRangeStyle = () => {
|
|
803
|
+
if (!rangeTrackRef.current)
|
|
804
|
+
return;
|
|
805
|
+
const innerWidth = rangeTrackRef.current.clientWidth;
|
|
806
|
+
const { min: valueMinReal, max: valueMaxReal } = getMinMax(rangeValue.min.value, rangeValue.max.value);
|
|
807
|
+
const valueMin = mapValueToInputRange(valueMinReal);
|
|
808
|
+
const valueMax = mapValueToInputRange(valueMaxReal);
|
|
809
|
+
const ratioMin = clamp01((valueMin - min) / (maxForInput - min));
|
|
810
|
+
const ratioMax = clamp01((valueMax - min) / (maxForInput - min));
|
|
811
|
+
const leftPx = innerWidth * ratioMin;
|
|
812
|
+
const rightPx = innerWidth * ratioMax;
|
|
813
|
+
setSelectedRangeStyle({
|
|
814
|
+
left: `${leftPx}px`,
|
|
815
|
+
width: `${Math.max(0, rightPx - leftPx)}px`,
|
|
816
|
+
});
|
|
817
|
+
};
|
|
818
|
+
useEffect(() => {
|
|
819
|
+
calculateSelectedRangeStyle();
|
|
820
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
821
|
+
}, [
|
|
822
|
+
rangeValue.min.value,
|
|
823
|
+
rangeValue.max.value,
|
|
824
|
+
min,
|
|
825
|
+
max,
|
|
826
|
+
stepValue,
|
|
827
|
+
maxForInput,
|
|
828
|
+
]);
|
|
829
|
+
useEffect(() => {
|
|
830
|
+
setRangeValuePosition(activeInputIndex === 0 ? rangeValue.min.value : rangeValue.max.value);
|
|
831
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
832
|
+
}, []);
|
|
833
|
+
useLayoutEffect(() => {
|
|
834
|
+
const track = rangeTrackRef.current;
|
|
835
|
+
if (!track)
|
|
836
|
+
return;
|
|
837
|
+
let rafId = null;
|
|
838
|
+
const updateValue = () => {
|
|
839
|
+
calculateSelectedRangeStyle();
|
|
840
|
+
const updatedValue = typeof indicatorValue === "number"
|
|
841
|
+
? indicatorValue
|
|
842
|
+
: activeInputIndex === 0
|
|
843
|
+
? rangeValue.min.value
|
|
844
|
+
: rangeValue.max.value;
|
|
845
|
+
setRangeValuePosition(updatedValue);
|
|
846
|
+
};
|
|
847
|
+
const updateSchedule = () => {
|
|
848
|
+
if (rafId !== null)
|
|
849
|
+
return;
|
|
850
|
+
rafId = requestAnimationFrame(() => {
|
|
851
|
+
rafId = null;
|
|
852
|
+
updateValue();
|
|
853
|
+
});
|
|
854
|
+
};
|
|
855
|
+
updateSchedule();
|
|
856
|
+
const observer = new ResizeObserver(updateSchedule);
|
|
857
|
+
observer.observe(track);
|
|
858
|
+
window.addEventListener("resize", updateSchedule);
|
|
859
|
+
return () => {
|
|
860
|
+
observer.disconnect();
|
|
861
|
+
window.removeEventListener("resize", updateSchedule);
|
|
862
|
+
if (rafId !== null)
|
|
863
|
+
cancelAnimationFrame(rafId);
|
|
864
|
+
};
|
|
865
|
+
}, [
|
|
866
|
+
min,
|
|
867
|
+
max,
|
|
868
|
+
stepValue,
|
|
869
|
+
maxForInput,
|
|
870
|
+
indicatorValue,
|
|
871
|
+
activeInputIndex,
|
|
872
|
+
rangeValue.min.value,
|
|
873
|
+
rangeValue.max.value,
|
|
874
|
+
]);
|
|
875
|
+
const lowValue = Math.min(rangeValue.min.value, rangeValue.max.value);
|
|
876
|
+
const indicatorIsMin = typeof indicatorValue === "number" ? indicatorValue === lowValue : false;
|
|
877
|
+
const indicatorIcon = (icons === null || icons === void 0 ? void 0 : icons.indicatorMin) && (icons === null || icons === void 0 ? void 0 : icons.indicatorMax)
|
|
878
|
+
? indicatorIsMin
|
|
879
|
+
? icons.indicatorMin
|
|
880
|
+
: icons.indicatorMax
|
|
881
|
+
: undefined;
|
|
882
|
+
const indicatorIconKey = indicatorIsMin ? "min" : "max";
|
|
883
|
+
return (jsxs("div", { className: containerClasses, children: [label && (jsx(ZRangeLabel, { label: label, labelClassName: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.label })), jsxs("div", { ref: containerRef, className: styles["zr"], children: [jsx(ZRangeInputs, { rangeValue: rangeValueForInput, min: min, max: maxForInput, step: stepValue, inputMinClasses: inputMinClasses, inputMaxClasses: inputMaxClasses, onInputChange: onInputChange, onMouseDown: onMoveStart, onTouchStart: onMoveStart, onMouseMove: onMove, onTouchMove: onMove, onMouseUp: onMoveEnd, onTouchEnd: onMoveEnd }), jsx(ZRangeTrack, { rangeTrackRef: rangeTrackRef, selectedRangeRef: selectedRangeRef, selectedRangeStyle: selectedRangeStyle, trackStyles: {
|
|
884
|
+
trackContainer: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.trackContainer,
|
|
885
|
+
trackRange: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.trackRange,
|
|
886
|
+
trackSelected: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.trackSelected,
|
|
887
|
+
} }), jsx(ZRangeIndicator, { valueIndicatorRef: valueIndicatorRef, indicatorStyles: {
|
|
888
|
+
indicator: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicator,
|
|
889
|
+
indicatorMin: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorMin,
|
|
890
|
+
indicatorMax: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorMax,
|
|
891
|
+
indicatorValue: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorValue,
|
|
892
|
+
indicatorIcon: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorIcon,
|
|
893
|
+
}, indicatorValue: indicatorValue, rangeTrackRef: rangeTrackRef, inputElement: activeInputIndex === 0
|
|
894
|
+
? rangeValue.min.inputRef.current
|
|
895
|
+
: (_a = rangeValue.max.inputRef.current) !== null && _a !== void 0 ? _a : rangeValue.min.inputRef.current, scaleFunction: scaleFunction, unitList: unitList, unitDivisors: unitDivisors, scale: scale, icon: indicatorIcon, iconKey: indicatorIconKey, isHighlighted: isHighlighted, isIndicatorUnitHidden: isIndicatorUnitHidden })] }), jsxs("div", { className: styles["zr__slider-values-wrapper"], children: [jsx(ZRangeSliderValue, { sliderValueClassName: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.sliderValue, value: rangeValue.getValueLabel("min"), icon: icons === null || icons === void 0 ? void 0 : icons.thumbMin }), jsx(ZRangeSliderValue, { sliderValueClassName: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.sliderValue, value: rangeValue.getValueLabel("max"), icon: icons === null || icons === void 0 ? void 0 : icons.thumbMax })] })] }));
|
|
896
|
+
};
|
|
897
|
+
|
|
898
|
+
const isRangeRangeValue = (value) => value &&
|
|
899
|
+
typeof value === "object" &&
|
|
900
|
+
typeof value.min === "number" &&
|
|
901
|
+
typeof value.max === "number";
|
|
902
|
+
const toFormRangeValue = (componentValue, fallback) => {
|
|
903
|
+
var _a, _b;
|
|
904
|
+
const v = componentValue;
|
|
905
|
+
return {
|
|
906
|
+
min: typeof ((_a = v === null || v === void 0 ? void 0 : v.min) === null || _a === void 0 ? void 0 : _a.value) === "number" ? v.min.value : fallback.min,
|
|
907
|
+
max: typeof ((_b = v === null || v === void 0 ? void 0 : v.max) === null || _b === void 0 ? void 0 : _b.value) === "number" ? v.max.value : fallback.max,
|
|
908
|
+
};
|
|
909
|
+
};
|
|
910
|
+
const getErrorMessage = (error) => {
|
|
911
|
+
if (!error || typeof error !== "object")
|
|
912
|
+
return undefined;
|
|
913
|
+
const maybe = error;
|
|
914
|
+
return typeof maybe.message === "string" ? maybe.message : undefined;
|
|
915
|
+
};
|
|
916
|
+
const ZRangeField = (props) => {
|
|
917
|
+
const { control, name, rules, defaultValue, shouldUnregister, onValueChange, onValueSelect, hideError, errorClassName, errorRenderer, min, max, commitMode = "select", ...rest } = props;
|
|
918
|
+
const errorMessageClasses = classNames(styles["zr__error-message"], errorClassName);
|
|
919
|
+
const fallback = defaultValue !== null && defaultValue !== void 0 ? defaultValue : { min, max };
|
|
920
|
+
const { field, fieldState } = useController({
|
|
921
|
+
name,
|
|
922
|
+
control,
|
|
923
|
+
rules,
|
|
924
|
+
shouldUnregister,
|
|
925
|
+
defaultValue: (defaultValue !== null && defaultValue !== void 0 ? defaultValue : fallback),
|
|
926
|
+
});
|
|
927
|
+
// UWAGA: value do ZRange pochodzi TYLKO z RHF (albo fallback),
|
|
928
|
+
// a RHF aktualizujemy na końcu (onSelect) — więc podczas dragowania value jest stabilne.
|
|
929
|
+
const formValue = isRangeRangeValue(field.value)
|
|
930
|
+
? field.value
|
|
931
|
+
: fallback;
|
|
932
|
+
const errorMessage = getErrorMessage(fieldState.error);
|
|
933
|
+
const handleRangeChange = (componentValue, fieldName) => {
|
|
934
|
+
const nextFormValue = toFormRangeValue(componentValue, fallback);
|
|
935
|
+
// Live update tylko jeśli ktoś tego chce
|
|
936
|
+
if (commitMode === "change") {
|
|
937
|
+
field.onChange(nextFormValue);
|
|
938
|
+
}
|
|
939
|
+
onValueChange === null || onValueChange === void 0 ? void 0 : onValueChange(nextFormValue, fieldName);
|
|
940
|
+
};
|
|
941
|
+
const handleRangeSelect = (componentValue, fieldName) => {
|
|
942
|
+
const nextFormValue = toFormRangeValue(componentValue, fallback);
|
|
943
|
+
// Commit dopiero na końcu => brak resetów w trakcie dragowania
|
|
944
|
+
field.onChange(nextFormValue);
|
|
945
|
+
field.onBlur();
|
|
946
|
+
onValueSelect === null || onValueSelect === void 0 ? void 0 : onValueSelect(nextFormValue, fieldName);
|
|
947
|
+
};
|
|
948
|
+
return (jsxs("div", { children: [jsx(ZRange, { name: name, min: min, max: max, value: formValue, onChange: handleRangeChange, onSelect: handleRangeSelect, ...rest }), !hideError && errorMessage ? (errorRenderer ? (jsx(Fragment, { children: errorRenderer(errorMessage) })) : (jsx("div", { className: errorMessageClasses, children: errorMessage }))) : null] }));
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
export { ZDropButton, ZRange, ZRangeField };
|
|
321
952
|
//# sourceMappingURL=index.esm.js.map
|