aio-range 2.0.1 → 2.0.3

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 +0 -9
  2. package/index.js +113 -61
  3. package/package.json +2 -3
package/index.d.ts CHANGED
@@ -23,7 +23,6 @@ export type AI_label = {
23
23
  zIndex?: number;
24
24
  setting: (value: number, p: {
25
25
  angle: number;
26
- disabled: boolean;
27
26
  index: number;
28
27
  }) => AI_labelItem;
29
28
  };
@@ -76,22 +75,18 @@ export type I_Range = {
76
75
  pointHtml?: (p: {
77
76
  index: number;
78
77
  value: number;
79
- disabled: boolean;
80
78
  angle: number;
81
79
  }) => ReactNode | false | undefined;
82
80
  pointContent?: (p: {
83
81
  index: number;
84
82
  value: number;
85
- disabled: boolean;
86
83
  angle: number;
87
84
  }) => ReactNode;
88
85
  pointOffset?: (p: {
89
86
  index: number;
90
87
  value: number;
91
- disabled: boolean;
92
88
  angle: number;
93
89
  }) => number | undefined;
94
- disabled?: (value: number) => boolean;
95
90
  fill?: (startPoint: {
96
91
  index: number;
97
92
  value: number;
@@ -107,7 +102,6 @@ export type I_Range = {
107
102
  handle?: (p: {
108
103
  index: number;
109
104
  value: number;
110
- disabled: boolean;
111
105
  angle: number;
112
106
  }) => {
113
107
  widthPercent?: number;
@@ -133,7 +127,6 @@ export declare const AIOSlider: FC<I_Range>;
133
127
  export declare const AIOSpinner: FC<I_Range>;
134
128
  type I_RangeValue = {
135
129
  value: number;
136
- disabled: boolean;
137
130
  angle: number;
138
131
  index: number;
139
132
  };
@@ -144,7 +137,6 @@ declare const useRange: (PROPS: I_Range, type: 'slider' | 'spinner') => {
144
137
  fixAngle: (angle: number) => number;
145
138
  getValueByAngle: (angle: number) => number;
146
139
  mainDom: any;
147
- isValueDisabled: (value: number) => boolean;
148
140
  rootProps: I_Range_default;
149
141
  getRootProps: (props: I_Range, type: 'slider' | 'spinner') => any;
150
142
  getLabelList: (label: AI_label) => number[];
@@ -188,7 +180,6 @@ declare const useRange: (PROPS: I_Range, type: 'slider' | 'spinner') => {
188
180
  };
189
181
  };
190
182
  getRangeValue: (itemValue: number, index: number) => {
191
- disabled: boolean;
192
183
  value: number;
193
184
  index: number;
194
185
  angle: 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) {
@@ -197,8 +199,7 @@ const RangeLabel = (props) => {
197
199
  }
198
200
  const list = rangeHook.getLabelList(label);
199
201
  return (_jsx("div", { className: 'ai-range-labels', style: { zIndex }, children: list.map((itemValue, index) => {
200
- let disabled = rangeHook.isValueDisabled(itemValue);
201
- let labelItem = label.setting(itemValue, { disabled, angle: 0, index });
202
+ let labelItem = label.setting(itemValue, { angle: 0, index });
202
203
  if (props.type === "slider") {
203
204
  return (rangeHook.getSliderLabel(labelItem, itemValue));
204
205
  }
@@ -218,7 +219,7 @@ const AIRangePoint = ({ rangeValue, type }) => {
218
219
  return null;
219
220
  }
220
221
  const containerStyle = type === "spinner" ? rangeHook.getSpinnerPointContainerStyle(rangeValue) : rangeHook.getSliderPointContainerStyle(rangeValue);
221
- return (_jsx("div", { style: containerStyle, className: 'ai-range-point-container', draggable: false, "data-index": rangeValue.index, onMouseDown: () => rangeHook.handleZIndex(rangeValue.index), children: point }, 'rangepoint' + rangeValue.index));
222
+ return (_jsx("div", { style: containerStyle, className: 'ai-range-point-container', draggable: false, "data-range-index": rangeValue.index, onMouseDown: () => rangeHook.handleZIndex(rangeValue.index), children: point }, 'rangepoint' + rangeValue.index));
222
223
  };
223
224
  const useRange = (PROPS, type) => {
224
225
  const getProps = () => {
@@ -233,27 +234,23 @@ const useRange = (PROPS, type) => {
233
234
  const [values, setValues] = useState([]);
234
235
  const { vertical, reverse, start = 0, end = 100, min = start, max = end, step = 1, rotate = 0, round = 1, size = 72 } = props;
235
236
  useEffect(() => {
236
- if (!props.onChange || props.disabled === true) {
237
+ if (!props.onChange) {
237
238
  return;
238
239
  }
239
240
  clearTimeout(temp.timeOut);
240
241
  temp.timeOut = setTimeout(() => {
241
- new UT.Swip({
242
+ new Swip({
242
243
  reverseX: !!props.reverse,
243
- //vertical condition
244
244
  reverseY: !!props.reverse && !!props.vertical,
245
- dom: () => $(mainDom.current),
246
- start: (p) => {
245
+ dom: mainDom.current,
246
+ childIndexAttr: 'data-range-index',
247
+ start: ({ index }) => {
247
248
  const { disabled } = propsRef.current;
248
249
  if (disabled === true) {
249
250
  return false;
250
251
  }
251
- let { event } = p;
252
- if (event.target !== null) {
253
- let index = UT.GetAttr($(event.target), 'data-index');
254
- temp.index = index === null ? false : +index;
255
- temp.start = [...values];
256
- }
252
+ temp.index = index;
253
+ temp.start = [...values];
257
254
  return [0, 0];
258
255
  },
259
256
  move: (p) => {
@@ -273,7 +270,7 @@ const useRange = (PROPS, type) => {
273
270
  }, 100);
274
271
  }, [changeHandle]);
275
272
  function click(mousePosition) {
276
- if (props.disabled === true || temp.index !== false) {
273
+ if (props.disabled === true) {
277
274
  return;
278
275
  }
279
276
  let clickedValue;
@@ -301,17 +298,12 @@ const useRange = (PROPS, type) => {
301
298
  let newValue = getChangedValue(obj);
302
299
  changeValue(newValue);
303
300
  }
304
- function isValueDisabled(value) {
305
- const { rangeOption = {} } = props;
306
- const { disabled: d = () => false } = rangeOption;
307
- return d(value);
308
- }
309
301
  function getChangedValue(obj) {
310
302
  let { dx, dy, deltaCenterAngle, centerAngle } = obj;
311
303
  let startValue = [...temp.start];
312
304
  let index = temp.index;
313
305
  //agar faghat yek point darim har koja mousedown shod farz kon rooye oon point mousedown karde im
314
- if (startValue.length === 1 && index === false) {
306
+ if (startValue.length === 1 && index !== false) {
315
307
  index = 0;
316
308
  }
317
309
  let res;
@@ -325,8 +317,7 @@ const useRange = (PROPS, type) => {
325
317
  else {
326
318
  deltaValue = Math.round(getValueByXP(getXPByX(props.vertical ? dy : dx)) / props.step) * props.step;
327
319
  }
328
- let newValue = moveAll(startValue, deltaValue, true);
329
- res = !isValueValid(newValue) ? values : newValue;
320
+ res = moveAll(startValue, deltaValue, true);
330
321
  }
331
322
  else {
332
323
  let { before, after } = getIndexLimit(index);
@@ -344,9 +335,6 @@ const useRange = (PROPS, type) => {
344
335
  if (newUnitValue < before) {
345
336
  newUnitValue = before;
346
337
  }
347
- if (isValueDisabled(newUnitValue)) {
348
- newUnitValue = values[index];
349
- }
350
338
  startValue[index] = newUnitValue;
351
339
  res = startValue;
352
340
  }
@@ -354,12 +342,7 @@ const useRange = (PROPS, type) => {
354
342
  }
355
343
  function change1Unit(dir) {
356
344
  let newValue = [...values];
357
- let lastValue = JSON.stringify(newValue);
358
345
  newValue = moveAll(newValue, dir * props.step);
359
- while (!isValueValid(newValue) && JSON.stringify(newValue) !== lastValue) {
360
- lastValue = JSON.stringify(newValue);
361
- newValue = moveAll(newValue, dir * props.step);
362
- }
363
346
  changeValue(newValue);
364
347
  }
365
348
  function changeValue(newValue) {
@@ -369,14 +352,6 @@ const useRange = (PROPS, type) => {
369
352
  newValue = getValues(newValue);
370
353
  props.onChange(!!props.multiple ? newValue : newValue[0]);
371
354
  }
372
- function isValueValid(value) {
373
- for (let i = 0; i < value.length; i++) {
374
- if (isValueDisabled(value[i])) {
375
- return false;
376
- }
377
- }
378
- return true;
379
- }
380
355
  function getIndexLimit(index) {
381
356
  let before, after;
382
357
  if (index === 0) {
@@ -400,6 +375,13 @@ const useRange = (PROPS, type) => {
400
375
  }
401
376
  return res;
402
377
  }
378
+ const getFloatLength = (step) => {
379
+ const str = step.toString();
380
+ if (str.indexOf('.') === -1) {
381
+ return 0;
382
+ }
383
+ return str.split('.')[1].length;
384
+ };
403
385
  function getValues(value) {
404
386
  if (!Array.isArray(value)) {
405
387
  value = [value || 0];
@@ -407,8 +389,12 @@ const useRange = (PROPS, type) => {
407
389
  for (let i = 0; i < value.length; i++) {
408
390
  let point = value[i] || 0;
409
391
  point = Math.round((point - start) / step) * step + start;
410
- const { floatLength } = UT.Float(step);
411
- point = +point.toFixed(floatLength);
392
+ try {
393
+ point = +point.toFixed(getFloatLength(step));
394
+ }
395
+ catch (err) {
396
+ console.error(err.message);
397
+ }
412
398
  if (point < min) {
413
399
  point = min;
414
400
  }
@@ -491,7 +477,9 @@ const useRange = (PROPS, type) => {
491
477
  return xp * (end - start) / 100;
492
478
  }
493
479
  function getXPByX(x) {
494
- return x * 100 / $(mainDom.current)[vertical ? 'height' : 'width']();
480
+ const container = mainDom.current;
481
+ const size = vertical ? container.clientHeight : container.clientWidth;
482
+ return (x * 100) / size;
495
483
  }
496
484
  const getArc = (rc) => {
497
485
  let roundCap = rc.roundCap || false, full = rc.full || false, offset = rc.offset, color = rc.color || '#000', className = rc.className;
@@ -502,9 +490,12 @@ const useRange = (PROPS, type) => {
502
490
  return { thickness, radius, color, roundCap, full, className, from, to };
503
491
  };
504
492
  function handleZIndex(index) {
505
- let containers = $(mainDom.current).find(`.ai-range-value-container`);
506
- containers.css({ zIndex: 10 });
507
- containers.eq(index).css({ zIndex: 100 });
493
+ const dom = mainDom.current;
494
+ const containers = dom.querySelectorAll('.ai-range-value-container');
495
+ containers === null || containers === void 0 ? void 0 : containers.forEach((container) => { container.style.zIndex = '10'; });
496
+ if (containers === null || containers === void 0 ? void 0 : containers[index]) {
497
+ containers[index].style.zIndex = '100';
498
+ }
508
499
  }
509
500
  function getOffset(rangeValue) {
510
501
  const { pointOffset = (() => 0) } = props.rangeOption || {};
@@ -518,8 +509,7 @@ const useRange = (PROPS, type) => {
518
509
  }
519
510
  const getRangeValue = (itemValue, index) => {
520
511
  let angle = fixAngle(getAngleByValue(itemValue));
521
- const disabled = isValueDisabled(itemValue);
522
- return { disabled, value: itemValue, index, angle };
512
+ return { value: itemValue, index, angle };
523
513
  };
524
514
  function getRootClassName(props, type) {
525
515
  let c = 'ai-range';
@@ -548,7 +538,7 @@ const useRange = (PROPS, type) => {
548
538
  function getRootProps(props, type) {
549
539
  let { attrs = {} } = props;
550
540
  let rootStyle = getRootStyle(props, type);
551
- return UT.AddToAttrs(attrs, { className: getRootClassName(props, type), style: rootStyle, attrs: { ref: mainDom } });
541
+ return AddToAttrs(attrs, { className: getRootClassName(props, type), style: rootStyle, attrs: { ref: mainDom } });
552
542
  }
553
543
  function getDistance(labelItem, type) {
554
544
  let { offset = 0 } = labelItem;
@@ -573,7 +563,7 @@ const useRange = (PROPS, type) => {
573
563
  background: config.color
574
564
  } }));
575
565
  }
576
- const htmlProps = UT.AddToAttrs({}, { className: ['ai-range-label'], attrs: { draggable: false } });
566
+ const htmlProps = AddToAttrs({}, { className: ['ai-range-label'], attrs: { draggable: false } });
577
567
  const containerStyle = Object.assign({ [getSide()]: getXPByValue(itemValue) + '%' }, getDistance(labelItem, 'slider'));
578
568
  const containerProps = { className: 'ai-range-label-container', style: containerStyle, draggable: false };
579
569
  return (_jsx("div", Object.assign({}, containerProps, { children: _jsx("div", Object.assign({}, htmlProps, { children: html })) })));
@@ -608,13 +598,75 @@ const useRange = (PROPS, type) => {
608
598
  let containerProps = { className: 'ai-range-label-container', style: containerStyle, draggable: false };
609
599
  const distance = getDistance(labelItem, "spinner");
610
600
  let textStyle = Object.assign(Object.assign({}, distance), { transform: labelItem.fixAngle ? `rotate(${-angle}deg)` : undefined });
611
- const htmlProps = UT.AddToAttrs({}, { className: ['ai-range-label'], style: textStyle, attrs: { draggable: false } });
601
+ const htmlProps = AddToAttrs({}, { className: ['ai-range-label'], style: textStyle, attrs: { draggable: false } });
612
602
  return (_jsx("div", Object.assign({}, containerProps, { children: _jsx("div", Object.assign({}, htmlProps, { children: html })) })));
613
603
  }
614
604
  return {
615
- values, getValues, getSide, fixAngle, getValueByAngle, mainDom, isValueDisabled, rootProps: props, getRootProps, getLabelList,
605
+ values, getValues, getSide, fixAngle, getValueByAngle, mainDom, rootProps: props, getRootProps, getLabelList,
616
606
  getArc, getAngleByValue, getRectByStr, sbp, getXPByValue, getValueByXP, getXPByX, handleZIndex, getSpinnerLabel,
617
607
  getSliderPointContainerStyle, getSpinnerPointContainerStyle, getSliderValueProps, getSpinnerValueProps, getRangeValue,
618
608
  getSliderLabel
619
609
  };
620
610
  };
611
+ function svgArcRange(centerX, centerY, radius, angleInDegrees) {
612
+ let angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
613
+ return {
614
+ x: centerX + (radius * Math.cos(angleInRadians)),
615
+ y: centerY + (radius * Math.sin(angleInRadians))
616
+ };
617
+ }
618
+ function svgArc(x, y, radius, startAngle, endAngle) {
619
+ if (startAngle === endAngle || endAngle - startAngle === 360) {
620
+ startAngle = 0;
621
+ endAngle = 360;
622
+ }
623
+ if (startAngle === 360) {
624
+ startAngle = 359.99;
625
+ }
626
+ if (endAngle === 360) {
627
+ endAngle = 359.99;
628
+ }
629
+ let start = svgArcRange(x, y, radius, endAngle);
630
+ let end = svgArcRange(x, y, radius, startAngle);
631
+ let largeArcFlag;
632
+ if (endAngle - startAngle < -180) {
633
+ largeArcFlag = '0';
634
+ }
635
+ else if (endAngle - startAngle < 0) {
636
+ largeArcFlag = '1';
637
+ }
638
+ else if (endAngle - startAngle <= 180) {
639
+ largeArcFlag = '0';
640
+ }
641
+ else if (endAngle - startAngle <= 360) {
642
+ largeArcFlag = '1';
643
+ }
644
+ else {
645
+ largeArcFlag = '0';
646
+ }
647
+ return ["M", start.x, start.y, "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y].join(" ");
648
+ }
649
+ function classListToString(classes) {
650
+ let list = [];
651
+ const getClasses = (cls) => {
652
+ if (Array.isArray(cls)) {
653
+ return cls.filter((o) => !!o && typeof o === 'string');
654
+ }
655
+ else if (!!cls && typeof cls === 'string') {
656
+ return cls.split(' ');
657
+ }
658
+ return [];
659
+ };
660
+ for (let i = 0; i < classes.length; i++) {
661
+ const cls = classes[i];
662
+ list = [...list, ...getClasses(cls)];
663
+ }
664
+ return list.length ? list.join(' ') : '';
665
+ }
666
+ function AddToAttrs(attrs, p) {
667
+ attrs = attrs || {};
668
+ const className = classListToString([attrs.className, p === null || p === void 0 ? void 0 : p.className]);
669
+ let newStyle = Object.assign(Object.assign({}, attrs.style), p === null || p === void 0 ? void 0 : p.style);
670
+ const res = Object.assign(Object.assign(Object.assign({}, attrs), { className, style: newStyle }), p === null || p === void 0 ? void 0 : p.attrs);
671
+ return res;
672
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-range",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
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",