@rescui/tab-list 0.13.4 → 0.13.6
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/lib/_virtual/index.css.js +7 -7
- package/lib/_virtual/left.js +8 -9
- package/lib/_virtual/right.js +8 -9
- package/lib/index.css +81 -187
- package/lib/parts/deep-map.js +4 -7
- package/lib/parts/separator.p.module.css.js +3 -3
- package/lib/parts/tab-list-context.js +3 -5
- package/lib/parts/tab-list.p.module.css.js +15 -15
- package/lib/parts/tab-separator.js +6 -4
- package/lib/parts/tab.js +36 -50
- package/lib/parts/use-indicator.js +18 -39
- package/lib/parts/use-scrollable-list.js +93 -121
- package/lib/parts/with-scroll-arrows.js +37 -55
- package/lib/parts/with-scroll-arrows.p.module.css.js +11 -11
- package/lib/tab-list.js +24 -26
- package/package.json +9 -10
package/lib/parts/deep-map.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
|
|
2
1
|
import React, { isValidElement, cloneElement } from 'react';
|
|
3
2
|
|
|
4
3
|
function deepMap(children, fn) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var i = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
9
|
-
var returnChild = child;
|
|
4
|
+
const processChild = function (child) {
|
|
5
|
+
let i = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
6
|
+
let returnChild = child;
|
|
10
7
|
|
|
11
8
|
if (! /*#__PURE__*/isValidElement(returnChild)) {
|
|
12
9
|
return returnChild;
|
|
@@ -26,7 +23,7 @@ function deepMap(children, fn) {
|
|
|
26
23
|
return processChild(children);
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
return
|
|
26
|
+
return React.Children.map(children, processChild);
|
|
30
27
|
}
|
|
31
28
|
|
|
32
29
|
export { deepMap };
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { createContext, useContext } from 'react';
|
|
2
|
-
|
|
2
|
+
const TabListContext = /*#__PURE__*/createContext({
|
|
3
3
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
4
|
-
onChange:
|
|
4
|
+
onChange: () => {},
|
|
5
5
|
selectedId: 0
|
|
6
6
|
});
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
return useContext(TabListContext);
|
|
10
|
-
};
|
|
8
|
+
const useTabListContext = () => useContext(TabListContext);
|
|
11
9
|
|
|
12
10
|
export { TabListContext, useTabListContext };
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
var styles = {
|
|
2
|
-
"light": "
|
|
3
|
-
"dark": "
|
|
4
|
-
"sizeM": "
|
|
5
|
-
"sizeL": "
|
|
6
|
-
"classic": "
|
|
7
|
-
"rock": "
|
|
8
|
-
"sparse": "
|
|
9
|
-
"short": "
|
|
10
|
-
"tabsContainer": "
|
|
11
|
-
"indicator": "
|
|
12
|
-
"iconLeft": "
|
|
13
|
-
"iconRight": "
|
|
14
|
-
"tab": "
|
|
15
|
-
"selected": "
|
|
16
|
-
"icon": "
|
|
2
|
+
"light": "_light_7obrsz_7",
|
|
3
|
+
"dark": "_dark_7obrsz_10",
|
|
4
|
+
"sizeM": "_sizeM_7obrsz_14",
|
|
5
|
+
"sizeL": "_sizeL_7obrsz_17",
|
|
6
|
+
"classic": "_classic_7obrsz_21",
|
|
7
|
+
"rock": "_rock_7obrsz_24",
|
|
8
|
+
"sparse": "_sparse_7obrsz_28",
|
|
9
|
+
"short": "_short_7obrsz_32",
|
|
10
|
+
"tabsContainer": "_tabsContainer_7obrsz_36",
|
|
11
|
+
"indicator": "_indicator_7obrsz_46",
|
|
12
|
+
"iconLeft": "_iconLeft_7obrsz_61",
|
|
13
|
+
"iconRight": "_iconRight_7obrsz_64",
|
|
14
|
+
"tab": "_tab_7obrsz_36",
|
|
15
|
+
"selected": "_selected_7obrsz_154",
|
|
16
|
+
"icon": "_icon_7obrsz_61"
|
|
17
17
|
};
|
|
18
18
|
export { styles as default };
|
|
@@ -3,10 +3,12 @@ import cn from 'classnames';
|
|
|
3
3
|
import { useThemeWithUndefined } from '@rescui/ui-contexts';
|
|
4
4
|
import styles from './separator.p.module.css.js';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const TabSeparator = _ref => {
|
|
7
|
+
let {
|
|
8
|
+
className,
|
|
9
|
+
theme: themeFromProps
|
|
10
|
+
} = _ref;
|
|
11
|
+
const theme = useThemeWithUndefined(themeFromProps);
|
|
10
12
|
return /*#__PURE__*/React.createElement("div", {
|
|
11
13
|
className: cn(styles.separator, styles[theme], className)
|
|
12
14
|
});
|
package/lib/parts/tab.js
CHANGED
|
@@ -1,81 +1,67 @@
|
|
|
1
|
-
import "core-js/modules/es.object.to-string.js";
|
|
2
|
-
import "core-js/modules/web.dom-collections.for-each.js";
|
|
3
|
-
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
|
4
|
-
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
|
5
|
-
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
|
6
|
-
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
|
7
|
-
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
|
8
|
-
import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
|
|
9
|
-
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
|
10
|
-
var _excluded = ["value", "className", "children", "icon", "iconPosition", "itemId", "onClick", "onKeyDown", "data-e2e"];
|
|
11
|
-
|
|
12
|
-
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
-
|
|
14
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
15
|
-
|
|
16
1
|
import React, { forwardRef, cloneElement } from 'react';
|
|
17
2
|
import PropTypes from 'prop-types';
|
|
18
3
|
import cn from 'classnames';
|
|
19
4
|
import styles from './tab-list.p.module.css.js';
|
|
20
5
|
import { useTabListContext } from './tab-list-context.js';
|
|
21
|
-
|
|
6
|
+
const TabTag = /*#__PURE__*/forwardRef((props, ref) => {
|
|
22
7
|
if (typeof props.href !== 'undefined') {
|
|
23
|
-
return /*#__PURE__*/React.createElement("a",
|
|
24
|
-
ref: ref
|
|
25
|
-
|
|
8
|
+
return /*#__PURE__*/React.createElement("a", {
|
|
9
|
+
ref: ref,
|
|
10
|
+
...props
|
|
11
|
+
}, props.children);
|
|
26
12
|
}
|
|
27
13
|
|
|
28
|
-
return /*#__PURE__*/React.createElement("button",
|
|
29
|
-
ref: ref
|
|
30
|
-
|
|
14
|
+
return /*#__PURE__*/React.createElement("button", {
|
|
15
|
+
ref: ref,
|
|
16
|
+
...props,
|
|
31
17
|
type: "button"
|
|
32
|
-
}
|
|
18
|
+
}, props.children);
|
|
33
19
|
});
|
|
34
20
|
TabTag.displayName = 'TabTag';
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return /*#__PURE__*/React.createElement(TabTag,
|
|
21
|
+
const Tab = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
22
|
+
let {
|
|
23
|
+
value,
|
|
24
|
+
className,
|
|
25
|
+
children,
|
|
26
|
+
icon,
|
|
27
|
+
iconPosition = 'left',
|
|
28
|
+
itemId,
|
|
29
|
+
onClick,
|
|
30
|
+
onKeyDown,
|
|
31
|
+
['data-e2e']: e2eId,
|
|
32
|
+
...restProps
|
|
33
|
+
} = _ref;
|
|
34
|
+
const {
|
|
35
|
+
onChange,
|
|
36
|
+
selectedId
|
|
37
|
+
} = useTabListContext();
|
|
38
|
+
const selected = itemId === selectedId;
|
|
39
|
+
return /*#__PURE__*/React.createElement(TabTag, { ...restProps,
|
|
54
40
|
className: cn(styles.tab, selected && styles.selected, iconPosition === 'right' ? styles.iconRight : styles.iconLeft, className),
|
|
55
|
-
onClick:
|
|
56
|
-
if (
|
|
57
|
-
|
|
41
|
+
onClick: e => {
|
|
42
|
+
if (onClick) {
|
|
43
|
+
onClick(e);
|
|
58
44
|
}
|
|
59
45
|
|
|
60
46
|
onChange(value);
|
|
61
47
|
},
|
|
62
|
-
onKeyDown:
|
|
48
|
+
onKeyDown: e => {
|
|
63
49
|
if (e.key === ' ') {
|
|
64
50
|
e.target.click();
|
|
65
51
|
e.preventDefault();
|
|
66
52
|
}
|
|
67
53
|
|
|
68
|
-
if (
|
|
69
|
-
|
|
54
|
+
if (onKeyDown) {
|
|
55
|
+
onKeyDown(e);
|
|
70
56
|
}
|
|
71
57
|
},
|
|
72
58
|
role: "tab",
|
|
73
59
|
tabIndex: 0,
|
|
74
60
|
ref: ref,
|
|
75
61
|
"data-e2e": e2eId,
|
|
76
|
-
"data-test":
|
|
62
|
+
"data-test": `tab${selected ? ' tab-selected' : ''}`,
|
|
77
63
|
"data-rs-internal": 'tab-list__tab'
|
|
78
|
-
}
|
|
64
|
+
}, icon && /*#__PURE__*/cloneElement(icon, {
|
|
79
65
|
className: cn(icon.props.className, styles.icon)
|
|
80
66
|
}), /*#__PURE__*/React.createElement("div", null, children));
|
|
81
67
|
});
|
|
@@ -1,60 +1,39 @@
|
|
|
1
|
-
import "core-js/modules/es.object.to-string.js";
|
|
2
|
-
import "core-js/modules/web.dom-collections.for-each.js";
|
|
3
|
-
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
|
4
|
-
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
|
5
|
-
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
|
6
|
-
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
|
7
|
-
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
|
8
|
-
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
|
9
|
-
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
|
10
|
-
|
|
11
|
-
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
12
|
-
|
|
13
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
14
|
-
|
|
15
1
|
import { useState, useEffect } from 'react';
|
|
16
2
|
import { useResizeObserver } from '@rescui/use-resize-observer';
|
|
17
3
|
|
|
18
4
|
function calculateIndicator(activeNodeRef, parentNode) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
5
|
+
const childPos = activeNodeRef.getBoundingClientRect();
|
|
6
|
+
const parentPos = parentNode.getBoundingClientRect();
|
|
7
|
+
const left = `${childPos.left - parentPos.left + parentNode.scrollLeft}px`;
|
|
8
|
+
const width = `${childPos.width}px`;
|
|
23
9
|
return {
|
|
24
|
-
left
|
|
25
|
-
width
|
|
10
|
+
left,
|
|
11
|
+
width
|
|
26
12
|
};
|
|
27
13
|
}
|
|
28
14
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
15
|
+
const useIndicator = _ref => {
|
|
16
|
+
let {
|
|
17
|
+
activeNodeRef,
|
|
18
|
+
containerRef,
|
|
19
|
+
triggers
|
|
20
|
+
} = _ref;
|
|
21
|
+
const [indicator, setIndicator] = useState({
|
|
35
22
|
left: 0,
|
|
36
23
|
width: 0
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
indicator = _useState2[0],
|
|
40
|
-
setIndicator = _useState2[1];
|
|
41
|
-
|
|
42
|
-
var _useState3 = useState(true),
|
|
43
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
44
|
-
isSSR = _useState4[0],
|
|
45
|
-
setIsSSR = _useState4[1]; // Catch the resize of the container which is happening when the fonts downloads for example
|
|
46
|
-
|
|
24
|
+
});
|
|
25
|
+
const [isSSR, setIsSSR] = useState(true); // Catch the resize of the container which is happening when the fonts downloads for example
|
|
47
26
|
|
|
48
|
-
|
|
27
|
+
const onResize = () => {
|
|
49
28
|
if (!activeNodeRef.current || !containerRef.current) {
|
|
50
29
|
return;
|
|
51
30
|
}
|
|
52
31
|
|
|
53
32
|
if (isSSR) {
|
|
54
33
|
setIsSSR(false);
|
|
55
|
-
setIndicator(
|
|
34
|
+
setIndicator({ ...calculateIndicator(activeNodeRef.current, containerRef.current),
|
|
56
35
|
transitionDuration: '0ms'
|
|
57
|
-
})
|
|
36
|
+
});
|
|
58
37
|
return;
|
|
59
38
|
}
|
|
60
39
|
|
|
@@ -1,53 +1,37 @@
|
|
|
1
|
-
import "core-js/modules/web.dom-collections.for-each.js";
|
|
2
|
-
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
|
3
|
-
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
|
4
|
-
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
|
5
|
-
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
|
6
|
-
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
|
7
|
-
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
|
|
8
|
-
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
|
9
|
-
import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
|
|
10
|
-
var _excluded = ["child", "itemId", "childValue", "ref"],
|
|
11
|
-
_excluded2 = ["value", "onFocus"];
|
|
12
|
-
import "core-js/modules/es.object.to-string.js";
|
|
13
|
-
import "core-js/modules/es.promise.js";
|
|
14
|
-
|
|
15
|
-
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
16
|
-
|
|
17
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
18
|
-
|
|
19
1
|
import React, { useRef, useState, useCallback, useMemo, useEffect, cloneElement } from 'react';
|
|
20
2
|
import { useResizeObserver } from '@rescui/use-resize-observer';
|
|
21
3
|
import { deepMap } from './deep-map.js'; // arrow (32) + shadow (10) + shadow blur (~8) + item focus outline (4)
|
|
22
4
|
|
|
23
|
-
|
|
5
|
+
const ARROW_AREA_WIDTH_IN_PX = 54;
|
|
24
6
|
|
|
25
7
|
function patchItemIfNeeded(_ref) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
8
|
+
let {
|
|
9
|
+
child,
|
|
10
|
+
itemId,
|
|
11
|
+
childValue,
|
|
12
|
+
ref,
|
|
13
|
+
...restProps
|
|
14
|
+
} = _ref;
|
|
15
|
+
return /*#__PURE__*/cloneElement(child, {
|
|
16
|
+
itemId,
|
|
34
17
|
value: childValue,
|
|
35
|
-
ref
|
|
36
|
-
|
|
18
|
+
ref,
|
|
19
|
+
...restProps
|
|
20
|
+
});
|
|
37
21
|
}
|
|
38
22
|
|
|
39
23
|
function calculateArrowsVisibility(scrollContainerEl) {
|
|
40
24
|
// doing this because scrollContainerEl.scrollWidth returns a rounded value
|
|
41
|
-
|
|
42
|
-
|
|
25
|
+
const scrollLeft = Math.round(scrollContainerEl.scrollLeft);
|
|
26
|
+
const shouldShowAnyArrow = scrollContainerEl.offsetWidth < scrollContainerEl.scrollWidth; // in some browsers, fully scrolled element has `scrollLeft + offsetWidth === scrollWidth - 1`
|
|
43
27
|
// so we take this measurement error into account
|
|
44
28
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
29
|
+
const delta = 1;
|
|
30
|
+
const showLeftArrow = shouldShowAnyArrow && scrollLeft > 0;
|
|
31
|
+
const showRightArrow = shouldShowAnyArrow && scrollLeft + scrollContainerEl.offsetWidth < scrollContainerEl.scrollWidth - delta;
|
|
48
32
|
return {
|
|
49
|
-
showLeftArrow
|
|
50
|
-
showRightArrow
|
|
33
|
+
showLeftArrow,
|
|
34
|
+
showRightArrow
|
|
51
35
|
};
|
|
52
36
|
}
|
|
53
37
|
|
|
@@ -76,30 +60,30 @@ function scrollElementLeft(element, to) {
|
|
|
76
60
|
}
|
|
77
61
|
|
|
78
62
|
function scrollIntoView(activeNode, parentNode) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
63
|
+
const childWidth = activeNode.offsetWidth;
|
|
64
|
+
const parentWidth = parentNode.offsetWidth;
|
|
65
|
+
const parentStyles = getComputedStyle(parentNode);
|
|
66
|
+
const parentMarginsOffset = parseInt(parentStyles.marginLeft, 10) + parseInt(parentStyles.marginRight, 10);
|
|
67
|
+
const parentScrollLeft = parentNode.scrollLeft;
|
|
68
|
+
const parentScrollWidth = parentNode.scrollWidth; // position of the left border in the container
|
|
85
69
|
|
|
86
|
-
|
|
70
|
+
const activeNodeLeft = parentNode.offsetLeft + activeNode.offsetLeft; // position of the right border in the container
|
|
87
71
|
|
|
88
|
-
|
|
72
|
+
const activeNodeRight = activeNodeLeft + childWidth; // position of the left border in relation to the viewport
|
|
89
73
|
|
|
90
|
-
|
|
74
|
+
const activeNodeLeftOffset = activeNodeLeft - parentScrollLeft; // position of the right border in relation to the viewport
|
|
91
75
|
|
|
92
|
-
|
|
76
|
+
const activeNodeRightOffset = parentWidth - (activeNodeLeftOffset + childWidth); // by default, don't scroll at all
|
|
93
77
|
|
|
94
|
-
|
|
78
|
+
let scrollTo = -1; // if before the element there is a space larger than arrow area
|
|
95
79
|
|
|
96
|
-
|
|
80
|
+
const isElHasWorkSpaceLeft = activeNodeLeft > ARROW_AREA_WIDTH_IN_PX; // if after the element there is a space larger than arrow area
|
|
97
81
|
|
|
98
|
-
|
|
82
|
+
const isElHasWorkSpaceRight = activeNodeRight < parentScrollWidth - ARROW_AREA_WIDTH_IN_PX;
|
|
99
83
|
|
|
100
84
|
if (activeNodeRightOffset <= ARROW_AREA_WIDTH_IN_PX) {
|
|
101
85
|
if (isElHasWorkSpaceRight) {
|
|
102
|
-
|
|
86
|
+
const sideOffset = parentWidth - childWidth + parentMarginsOffset;
|
|
103
87
|
scrollTo = activeNodeLeft + ARROW_AREA_WIDTH_IN_PX - sideOffset;
|
|
104
88
|
} else {
|
|
105
89
|
scrollTo = parentScrollWidth - parentWidth; // right edge
|
|
@@ -120,67 +104,60 @@ function scrollIntoView(activeNode, parentNode) {
|
|
|
120
104
|
}
|
|
121
105
|
}
|
|
122
106
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
107
|
+
const useScrollableList = _ref2 => {
|
|
108
|
+
let {
|
|
109
|
+
children,
|
|
110
|
+
compareValues,
|
|
111
|
+
value,
|
|
112
|
+
CompareItem
|
|
113
|
+
} = _ref2;
|
|
114
|
+
const activeNodeRef = useRef(null);
|
|
115
|
+
const scrollableRef = useRef(null);
|
|
116
|
+
const containerRef = useRef(null); // reset targeted element on scrolling by arrows
|
|
131
117
|
// so on window resize there wouldn't scroll to it
|
|
132
118
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
var _useState3 = useState({
|
|
119
|
+
const [targetedEl, setTargetedEl] = useState(null);
|
|
120
|
+
const [{
|
|
121
|
+
showLeftArrow,
|
|
122
|
+
showRightArrow
|
|
123
|
+
}, setShowArrows] = useState({
|
|
139
124
|
showLeftArrow: false,
|
|
140
125
|
showRightArrow: false
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
_useState4$ = _useState4[0],
|
|
144
|
-
showLeftArrow = _useState4$.showLeftArrow,
|
|
145
|
-
showRightArrow = _useState4$.showRightArrow,
|
|
146
|
-
setShowArrows = _useState4[1];
|
|
147
|
-
|
|
148
|
-
var _onFocus = useCallback(function (e) {
|
|
126
|
+
});
|
|
127
|
+
const onFocus = useCallback(e => {
|
|
149
128
|
setTargetedEl(e.target);
|
|
150
129
|
}, []);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
var items = deepMap(children, function (child) {
|
|
130
|
+
const [childrenWithValues, selectedId] = useMemo(() => {
|
|
131
|
+
let counter = 0;
|
|
132
|
+
let currentSelectedItemId = null;
|
|
133
|
+
const items = deepMap(children, child => {
|
|
156
134
|
if (child.type === CompareItem) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
var isSelectedItem = hasAlreadySelectedItem && compareValues(value, childValue);
|
|
135
|
+
const {
|
|
136
|
+
value: childValue = counter,
|
|
137
|
+
onFocus: onFocusFromProps,
|
|
138
|
+
...restProps
|
|
139
|
+
} = child.props;
|
|
140
|
+
const hasAlreadySelectedItem = currentSelectedItemId === null;
|
|
141
|
+
const isSelectedItem = hasAlreadySelectedItem && compareValues(value, childValue);
|
|
165
142
|
|
|
166
143
|
if (isSelectedItem) {
|
|
167
144
|
currentSelectedItemId = counter;
|
|
168
145
|
}
|
|
169
146
|
|
|
170
|
-
|
|
171
|
-
child
|
|
147
|
+
const patchedItem = patchItemIfNeeded({
|
|
148
|
+
child,
|
|
172
149
|
itemId: counter,
|
|
173
|
-
childValue
|
|
174
|
-
|
|
175
|
-
onFocus:
|
|
150
|
+
childValue,
|
|
151
|
+
...restProps,
|
|
152
|
+
onFocus: e => {
|
|
176
153
|
if (onFocusFromProps) {
|
|
177
154
|
onFocusFromProps(e);
|
|
178
155
|
}
|
|
179
156
|
|
|
180
|
-
|
|
157
|
+
onFocus(e);
|
|
181
158
|
},
|
|
182
159
|
ref: isSelectedItem ? activeNodeRef : null
|
|
183
|
-
})
|
|
160
|
+
});
|
|
184
161
|
counter += 1;
|
|
185
162
|
return patchedItem;
|
|
186
163
|
} else {
|
|
@@ -188,28 +165,25 @@ var useScrollableList = function useScrollableList(_ref2) {
|
|
|
188
165
|
}
|
|
189
166
|
});
|
|
190
167
|
return [React.Children.toArray(items), currentSelectedItemId];
|
|
191
|
-
}, [children, value, compareValues,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
setTargetedEl(null);
|
|
201
|
-
scrollElementBy(current, left);
|
|
202
|
-
};
|
|
168
|
+
}, [children, value, compareValues, onFocus, CompareItem]);
|
|
169
|
+
|
|
170
|
+
const prepareDirectionScroller = direction => () => {
|
|
171
|
+
const {
|
|
172
|
+
current
|
|
173
|
+
} = scrollableRef;
|
|
174
|
+
const left = Math.max(current.clientWidth - 2 * ARROW_AREA_WIDTH_IN_PX, 30) * (direction === 'right' ? 1 : -1);
|
|
175
|
+
setTargetedEl(null);
|
|
176
|
+
scrollElementBy(current, left);
|
|
203
177
|
};
|
|
204
178
|
|
|
205
|
-
useEffect(
|
|
179
|
+
useEffect(() => {
|
|
206
180
|
if (!activeNodeRef.current) {
|
|
207
181
|
return;
|
|
208
182
|
}
|
|
209
183
|
|
|
210
184
|
setTargetedEl(activeNodeRef.current);
|
|
211
185
|
}, [value, childrenWithValues]);
|
|
212
|
-
|
|
186
|
+
const setArrowsVisibility = useCallback(() => {
|
|
213
187
|
if (!scrollableRef.current) {
|
|
214
188
|
return;
|
|
215
189
|
}
|
|
@@ -217,8 +191,8 @@ var useScrollableList = function useScrollableList(_ref2) {
|
|
|
217
191
|
setShowArrows(calculateArrowsVisibility(scrollableRef.current));
|
|
218
192
|
}, []); // update arrows while smooth scrolling
|
|
219
193
|
|
|
220
|
-
useEffect(
|
|
221
|
-
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
const scrollContainer = scrollableRef.current;
|
|
222
196
|
|
|
223
197
|
if (!scrollContainer) {
|
|
224
198
|
return null;
|
|
@@ -227,13 +201,11 @@ var useScrollableList = function useScrollableList(_ref2) {
|
|
|
227
201
|
scrollContainer.addEventListener('scroll', setArrowsVisibility, {
|
|
228
202
|
passive: true
|
|
229
203
|
});
|
|
230
|
-
return
|
|
231
|
-
return scrollContainer.removeEventListener('scroll', setArrowsVisibility);
|
|
232
|
-
};
|
|
204
|
+
return () => scrollContainer.removeEventListener('scroll', setArrowsVisibility);
|
|
233
205
|
}, [setArrowsVisibility]); // scrollIntoView active element when it changes or the component resizes
|
|
234
206
|
|
|
235
|
-
|
|
236
|
-
|
|
207
|
+
const updateScrollState = () => {
|
|
208
|
+
const scrollableEl = scrollableRef.current;
|
|
237
209
|
|
|
238
210
|
if (!scrollableEl) {
|
|
239
211
|
return;
|
|
@@ -255,14 +227,14 @@ var useScrollableList = function useScrollableList(_ref2) {
|
|
|
255
227
|
});
|
|
256
228
|
useEffect(updateScrollState, [targetedEl, setArrowsVisibility]);
|
|
257
229
|
return {
|
|
258
|
-
selectedId
|
|
259
|
-
prepareDirectionScroller
|
|
260
|
-
activeNodeRef
|
|
261
|
-
scrollableRef
|
|
262
|
-
containerRef
|
|
263
|
-
childrenWithValues
|
|
264
|
-
showLeftArrow
|
|
265
|
-
showRightArrow
|
|
230
|
+
selectedId,
|
|
231
|
+
prepareDirectionScroller,
|
|
232
|
+
activeNodeRef,
|
|
233
|
+
scrollableRef,
|
|
234
|
+
containerRef,
|
|
235
|
+
childrenWithValues,
|
|
236
|
+
showLeftArrow,
|
|
237
|
+
showRightArrow
|
|
266
238
|
};
|
|
267
239
|
};
|
|
268
240
|
|