aio-range 2.0.2 → 2.0.4

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.
Files changed (3) hide show
  1. package/index.d.ts +2 -0
  2. package/index.js +101 -26
  3. package/package.json +2 -3
package/index.d.ts CHANGED
@@ -72,6 +72,8 @@ export type I_Range = {
72
72
  labels?: AI_label[];
73
73
  ranges?: I_range[];
74
74
  rangeOption?: {
75
+ onStartDrag?: (index: number) => void;
76
+ onEndDrag?: () => void;
75
77
  pointHtml?: (p: {
76
78
  index: number;
77
79
  value: number;
package/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { createElement as _createElement } from "react";
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import { createContext, createRef, useContext, useEffect, useRef, useState } from "react";
4
- import * as UT from 'aio-utils';
5
- import { $ } from 'aio-deps';
4
+ import { Swip } from 'aio-class';
6
5
  import './index.css';
7
6
  const RangeContext = createContext({});
8
7
  const RangeProvider = ({ value, children }) => _jsx(RangeContext.Provider, { value: value, children: children });
@@ -16,7 +15,7 @@ export const AIOSpinner = (props) => {
16
15
  return (_jsx(RangeProvider, { value: { rangeHook }, children: _jsxs("div", Object.assign({}, rangeHook.getRootProps(props, 'spinner'), { children: [_jsx(SpinnerText, { text: props.text }), _jsx(SpinnerSvg, {}), _jsx(RangeLabels, { type: 'spinner' }), _jsx(RangeValues, { type: 'spinner' })] })) }));
17
16
  };
18
17
  const SliderGroove = ({ grooveAttrs }) => {
19
- return _jsx("div", Object.assign({}, UT.AddToAttrs(grooveAttrs, { className: 'ai-slider-groove' })));
18
+ return _jsx("div", Object.assign({}, AddToAttrs(grooveAttrs, { className: 'ai-slider-groove' })));
20
19
  };
21
20
  const SpinnerText = ({ text }) => {
22
21
  if (text === undefined || text === null) {
@@ -84,7 +83,7 @@ const SliderFills = () => {
84
83
  const SliderRect = ({ range }) => {
85
84
  let { rangeHook } = useRangeProvider();
86
85
  let { color, from, to, className, style, offset, thickness, roundCap } = range;
87
- className = UT.classListToString(['ai-slider-range', range.className]);
86
+ className = classListToString(['ai-slider-range', range.className]);
88
87
  const { vertical, reverse } = rangeHook.rootProps;
89
88
  let startSide = rangeHook.getXPByValue(from), endSide = rangeHook.getXPByValue(to);
90
89
  let Style = Object.assign({ [vertical ? 'height' : 'width']: (endSide - startSide) + '%', [vertical ? 'width' : 'height']: range.thickness, [vertical ? (reverse ? 'bottom' : 'top') : (reverse ? 'right' : 'left')]: startSide + '%', [vertical ? 'left' : 'top']: `calc(50% - ${(thickness / 2) + offset}px)`, borderRadius: roundCap ? 48 : 0, background: color }, style);
@@ -113,7 +112,7 @@ const SpinnerArc = ({ range }) => {
113
112
  a = endAngle;
114
113
  }
115
114
  }
116
- return _jsx("path", { d: UT.svgArc(x, y, radius, a, b), stroke: color, strokeWidth: thickness, fill: 'transparent', strokeLinecap: roundCap ? 'round' : undefined, className: range.className }, `from${from}to${to}`);
115
+ return _jsx("path", { d: svgArc(x, y, radius, a, b), stroke: color, strokeWidth: thickness, fill: 'transparent', strokeLinecap: roundCap ? 'round' : undefined, className: range.className }, `from${from}to${to}`);
117
116
  };
118
117
  const SpinnerHandle = ({ rangeValue }) => {
119
118
  let { rangeHook } = useRangeProvider();
@@ -142,7 +141,7 @@ const SpinnerHandle = ({ rangeValue }) => {
142
141
  return { width, height, left: offset, background: color };
143
142
  }
144
143
  }
145
- let PROPS = UT.AddToAttrs({}, { className: 'ai-spinner-handle', style: getStyle(), attrs: { draggable: false } });
144
+ let PROPS = AddToAttrs({}, { className: 'ai-spinner-handle', style: getStyle(), attrs: { draggable: false } });
146
145
  return (_createElement("div", Object.assign({}, PROPS, { key: 'rangehandle' + rangeValue.index })));
147
146
  };
148
147
  const RangeLabel = (props) => {
@@ -156,9 +155,9 @@ const RangeLabel = (props) => {
156
155
  if (props.type !== "spinner" || !label.autoHide || vertical) {
157
156
  return;
158
157
  }
159
- let container = $(rangeHook.mainDom.current);
160
- let labels = container.find(`.ai-range-label`);
161
- if (!labels.length) {
158
+ const container = rangeHook.mainDom.current;
159
+ const labels = container.querySelectorAll('.ai-range-label');
160
+ if (!labels || !labels.length) {
162
161
  return;
163
162
  }
164
163
  let firstLabel = labels.eq(0);
@@ -189,7 +188,10 @@ const RangeLabel = (props) => {
189
188
  }
190
189
  }
191
190
  }
192
- useEffect(() => { $(window).on('resize', updateLabels); }, []);
191
+ useEffect(() => {
192
+ window.addEventListener('resize', updateLabels);
193
+ return () => { window.removeEventListener('resize', updateLabels); };
194
+ }, []);
193
195
  useEffect(() => { updateLabels(); });
194
196
  function RENDER(init) {
195
197
  if (!init && !dynamic) {
@@ -237,21 +239,21 @@ const useRange = (PROPS, type) => {
237
239
  }
238
240
  clearTimeout(temp.timeOut);
239
241
  temp.timeOut = setTimeout(() => {
240
- new UT.Swip({
242
+ new Swip({
241
243
  reverseX: !!props.reverse,
242
- //vertical condition
243
244
  reverseY: !!props.reverse && !!props.vertical,
244
- dom: () => $(mainDom.current),
245
- start: (p) => {
245
+ dom: mainDom.current,
246
+ childIndexAttr: 'data-range-index',
247
+ start: ({ index }) => {
248
+ var _a;
246
249
  const { disabled } = propsRef.current;
247
250
  if (disabled === true) {
248
251
  return false;
249
252
  }
250
- let { event } = p;
251
- if (event.target !== null) {
252
- let index = UT.GetAttr($(event.target), 'data-range-index');
253
- temp.index = index === null ? false : +index;
254
- temp.start = [...values];
253
+ temp.index = index;
254
+ temp.start = [...values];
255
+ if ((_a = PROPS.rangeOption) === null || _a === void 0 ? void 0 : _a.onStartDrag) {
256
+ PROPS.rangeOption.onStartDrag(index || 0);
255
257
  }
256
258
  return [0, 0];
257
259
  },
@@ -261,6 +263,12 @@ const useRange = (PROPS, type) => {
261
263
  changeHandle({ dx: change.dx, dy: change.dy, deltaCenterAngle: change.deltaCenterAngle, centerAngle: mousePosition.centerAngle });
262
264
  }
263
265
  },
266
+ end: () => {
267
+ var _a;
268
+ if ((_a = PROPS.rangeOption) === null || _a === void 0 ? void 0 : _a.onEndDrag) {
269
+ PROPS.rangeOption.onEndDrag();
270
+ }
271
+ },
264
272
  onClick: function (p) {
265
273
  const { disabled } = propsRef.current;
266
274
  if (disabled) {
@@ -479,7 +487,9 @@ const useRange = (PROPS, type) => {
479
487
  return xp * (end - start) / 100;
480
488
  }
481
489
  function getXPByX(x) {
482
- return x * 100 / $(mainDom.current)[vertical ? 'height' : 'width']();
490
+ const container = mainDom.current;
491
+ const size = vertical ? container.clientHeight : container.clientWidth;
492
+ return (x * 100) / size;
483
493
  }
484
494
  const getArc = (rc) => {
485
495
  let roundCap = rc.roundCap || false, full = rc.full || false, offset = rc.offset, color = rc.color || '#000', className = rc.className;
@@ -490,9 +500,12 @@ const useRange = (PROPS, type) => {
490
500
  return { thickness, radius, color, roundCap, full, className, from, to };
491
501
  };
492
502
  function handleZIndex(index) {
493
- let containers = $(mainDom.current).find(`.ai-range-value-container`);
494
- containers.css({ zIndex: 10 });
495
- containers.eq(index).css({ zIndex: 100 });
503
+ const dom = mainDom.current;
504
+ const containers = dom.querySelectorAll('.ai-range-value-container');
505
+ containers === null || containers === void 0 ? void 0 : containers.forEach((container) => { container.style.zIndex = '10'; });
506
+ if (containers === null || containers === void 0 ? void 0 : containers[index]) {
507
+ containers[index].style.zIndex = '100';
508
+ }
496
509
  }
497
510
  function getOffset(rangeValue) {
498
511
  const { pointOffset = (() => 0) } = props.rangeOption || {};
@@ -535,7 +548,7 @@ const useRange = (PROPS, type) => {
535
548
  function getRootProps(props, type) {
536
549
  let { attrs = {} } = props;
537
550
  let rootStyle = getRootStyle(props, type);
538
- return UT.AddToAttrs(attrs, { className: getRootClassName(props, type), style: rootStyle, attrs: { ref: mainDom } });
551
+ return AddToAttrs(attrs, { className: getRootClassName(props, type), style: rootStyle, attrs: { ref: mainDom } });
539
552
  }
540
553
  function getDistance(labelItem, type) {
541
554
  let { offset = 0 } = labelItem;
@@ -560,7 +573,7 @@ const useRange = (PROPS, type) => {
560
573
  background: config.color
561
574
  } }));
562
575
  }
563
- const htmlProps = UT.AddToAttrs({}, { className: ['ai-range-label'], attrs: { draggable: false } });
576
+ const htmlProps = AddToAttrs({}, { className: ['ai-range-label'], attrs: { draggable: false } });
564
577
  const containerStyle = Object.assign({ [getSide()]: getXPByValue(itemValue) + '%' }, getDistance(labelItem, 'slider'));
565
578
  const containerProps = { className: 'ai-range-label-container', style: containerStyle, draggable: false };
566
579
  return (_jsx("div", Object.assign({}, containerProps, { children: _jsx("div", Object.assign({}, htmlProps, { children: html })) })));
@@ -595,7 +608,7 @@ const useRange = (PROPS, type) => {
595
608
  let containerProps = { className: 'ai-range-label-container', style: containerStyle, draggable: false };
596
609
  const distance = getDistance(labelItem, "spinner");
597
610
  let textStyle = Object.assign(Object.assign({}, distance), { transform: labelItem.fixAngle ? `rotate(${-angle}deg)` : undefined });
598
- const htmlProps = UT.AddToAttrs({}, { className: ['ai-range-label'], style: textStyle, attrs: { draggable: false } });
611
+ const htmlProps = AddToAttrs({}, { className: ['ai-range-label'], style: textStyle, attrs: { draggable: false } });
599
612
  return (_jsx("div", Object.assign({}, containerProps, { children: _jsx("div", Object.assign({}, htmlProps, { children: html })) })));
600
613
  }
601
614
  return {
@@ -605,3 +618,65 @@ const useRange = (PROPS, type) => {
605
618
  getSliderLabel
606
619
  };
607
620
  };
621
+ function svgArcRange(centerX, centerY, radius, angleInDegrees) {
622
+ let angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
623
+ return {
624
+ x: centerX + (radius * Math.cos(angleInRadians)),
625
+ y: centerY + (radius * Math.sin(angleInRadians))
626
+ };
627
+ }
628
+ function svgArc(x, y, radius, startAngle, endAngle) {
629
+ if (startAngle === endAngle || endAngle - startAngle === 360) {
630
+ startAngle = 0;
631
+ endAngle = 360;
632
+ }
633
+ if (startAngle === 360) {
634
+ startAngle = 359.99;
635
+ }
636
+ if (endAngle === 360) {
637
+ endAngle = 359.99;
638
+ }
639
+ let start = svgArcRange(x, y, radius, endAngle);
640
+ let end = svgArcRange(x, y, radius, startAngle);
641
+ let largeArcFlag;
642
+ if (endAngle - startAngle < -180) {
643
+ largeArcFlag = '0';
644
+ }
645
+ else if (endAngle - startAngle < 0) {
646
+ largeArcFlag = '1';
647
+ }
648
+ else if (endAngle - startAngle <= 180) {
649
+ largeArcFlag = '0';
650
+ }
651
+ else if (endAngle - startAngle <= 360) {
652
+ largeArcFlag = '1';
653
+ }
654
+ else {
655
+ largeArcFlag = '0';
656
+ }
657
+ return ["M", start.x, start.y, "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y].join(" ");
658
+ }
659
+ function classListToString(classes) {
660
+ let list = [];
661
+ const getClasses = (cls) => {
662
+ if (Array.isArray(cls)) {
663
+ return cls.filter((o) => !!o && typeof o === 'string');
664
+ }
665
+ else if (!!cls && typeof cls === 'string') {
666
+ return cls.split(' ');
667
+ }
668
+ return [];
669
+ };
670
+ for (let i = 0; i < classes.length; i++) {
671
+ const cls = classes[i];
672
+ list = [...list, ...getClasses(cls)];
673
+ }
674
+ return list.length ? list.join(' ') : '';
675
+ }
676
+ function AddToAttrs(attrs, p) {
677
+ attrs = attrs || {};
678
+ const className = classListToString([attrs.className, p === null || p === void 0 ? void 0 : p.className]);
679
+ let newStyle = Object.assign(Object.assign({}, attrs.style), p === null || p === void 0 ? void 0 : p.style);
680
+ const res = Object.assign(Object.assign(Object.assign({}, attrs), { className, style: newStyle }), p === null || p === void 0 ? void 0 : p.attrs);
681
+ return res;
682
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-range",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "linear and circular range input",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,8 +12,7 @@
12
12
  "url": "git+https://github.com/mohammadFeiz/aio-range.git"
13
13
  },
14
14
  "dependencies": {
15
- "aio-deps": "latest",
16
- "aio-utils": "latest"
15
+ "aio-class":"latest"
17
16
  },
18
17
  "keywords": [
19
18
  "range",