@hi-ui/grid 5.0.0-experimental.0 → 5.0.0-rc.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/CHANGELOG.md +12 -5
- package/lib/cjs/auto-grid/AutoGrid.js +88 -0
- package/lib/cjs/auto-grid/DynamicGrid.js +59 -0
- package/lib/cjs/auto-grid/use-column-count.js +167 -0
- package/lib/cjs/auto-grid/utils.js +48 -0
- package/lib/cjs/index.js +6 -0
- package/lib/cjs/types.js +31 -0
- package/lib/esm/auto-grid/AutoGrid.js +76 -0
- package/lib/esm/auto-grid/DynamicGrid.js +47 -0
- package/lib/esm/auto-grid/use-column-count.js +160 -0
- package/lib/esm/auto-grid/utils.js +35 -0
- package/lib/esm/index.js +3 -0
- package/lib/esm/types.js +25 -0
- package/lib/types/auto-grid/AutoGrid.d.ts +34 -0
- package/lib/types/auto-grid/DynamicGrid.d.ts +9 -0
- package/lib/types/auto-grid/index.d.ts +4 -0
- package/lib/types/auto-grid/use-column-count.d.ts +39 -0
- package/lib/types/auto-grid/utils.d.ts +2 -0
- package/lib/types/context.d.ts +1 -0
- package/lib/types/index.d.ts +2 -0
- package/lib/types/types.d.ts +17 -2
- package/package.json +10 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hi-ui/grid
|
|
2
2
|
|
|
3
|
-
## 5.0.0-
|
|
3
|
+
## 5.0.0-rc.0
|
|
4
4
|
|
|
5
5
|
### Major Changes
|
|
6
6
|
|
|
@@ -13,19 +13,26 @@
|
|
|
13
13
|
### Patch Changes
|
|
14
14
|
|
|
15
15
|
- 9b34d99bc: chore: 将代码中 v4 改为 v5 (5.0)
|
|
16
|
+
- c6197357f: 新增 AutoGrid 及 DynamicGrid 组件
|
|
16
17
|
- 2e56529f7: styles: 主题定制功能完善&&样式变量化写法改造&&兼容 RTL (5.0)
|
|
17
18
|
- 33da3144e: build: 将 package.json 中 exports 配置中的 types 配置放在最上面 (5.0)
|
|
19
|
+
- Updated dependencies [eb17c4697]
|
|
18
20
|
- Updated dependencies [8c0ee78f0]
|
|
19
21
|
- Updated dependencies [8f3aa85e4]
|
|
20
22
|
- Updated dependencies [fd4c20bbd]
|
|
21
23
|
- Updated dependencies [79ea480f3]
|
|
22
24
|
- Updated dependencies [f1ab51725]
|
|
25
|
+
- Updated dependencies [d91a8bb0f]
|
|
23
26
|
- Updated dependencies [33da3144e]
|
|
24
27
|
- Updated dependencies [58ad82e94]
|
|
25
|
-
|
|
26
|
-
- @hi-ui/
|
|
27
|
-
- @hi-ui/
|
|
28
|
-
- @hi-ui/
|
|
28
|
+
- Updated dependencies [95d930354]
|
|
29
|
+
- @hi-ui/core@5.0.0-rc.0
|
|
30
|
+
- @hi-ui/use-latest@5.0.0-rc.0
|
|
31
|
+
- @hi-ui/use-merge-refs@5.0.0-rc.0
|
|
32
|
+
- @hi-ui/classname@5.0.0-rc.0
|
|
33
|
+
- @hi-ui/env@5.0.0-rc.0
|
|
34
|
+
- @hi-ui/func-utils@5.0.0-rc.0
|
|
35
|
+
- @hi-ui/type-assertion@5.0.0-rc.0
|
|
29
36
|
|
|
30
37
|
## 4.0.9
|
|
31
38
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
13
|
+
Object.defineProperty(exports, '__esModule', {
|
|
14
|
+
value: true
|
|
15
|
+
});
|
|
16
|
+
var tslib = require('tslib');
|
|
17
|
+
var React = require('react');
|
|
18
|
+
var Grid = require('../Grid.js');
|
|
19
|
+
var useColumnCount = require('./use-column-count.js');
|
|
20
|
+
var utils = require('./utils.js');
|
|
21
|
+
var env = require('@hi-ui/env');
|
|
22
|
+
function _interopDefaultCompat(e) {
|
|
23
|
+
return e && _typeof(e) === 'object' && 'default' in e ? e : {
|
|
24
|
+
'default': e
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
var React__default = /*#__PURE__*/_interopDefaultCompat(React);
|
|
28
|
+
var DEFAULT_COLUMNS = 24;
|
|
29
|
+
var AutoGrid = /*#__PURE__*/React.forwardRef(function (_a, ref) {
|
|
30
|
+
var _b;
|
|
31
|
+
var nodes = _a.nodes,
|
|
32
|
+
children = _a.children,
|
|
33
|
+
_a$columnCount = _a.columnCount,
|
|
34
|
+
columnCountProp = _a$columnCount === void 0 ? useColumnCount.DEFAULT_COLUMN_COUNT : _a$columnCount,
|
|
35
|
+
_a$columns = _a.columns,
|
|
36
|
+
columns = _a$columns === void 0 ? DEFAULT_COLUMNS : _a$columns,
|
|
37
|
+
_a$gutter = _a.gutter,
|
|
38
|
+
gutter = _a$gutter === void 0 ? true : _a$gutter,
|
|
39
|
+
_a$rowGap = _a.rowGap,
|
|
40
|
+
rowGap = _a$rowGap === void 0 ? 0 : _a$rowGap,
|
|
41
|
+
restRowProps = tslib.__rest(_a, ["nodes", "children", "columnCount", "columns", "gutter", "rowGap"]);
|
|
42
|
+
var rowProps = Object.assign({
|
|
43
|
+
columns: columns,
|
|
44
|
+
gutter: gutter,
|
|
45
|
+
rowGap: rowGap
|
|
46
|
+
}, restRowProps);
|
|
47
|
+
var validColumns = (_b = rowProps.columns) !== null && _b !== void 0 ? _b : DEFAULT_COLUMNS;
|
|
48
|
+
var colSpan = Math.floor(validColumns / columnCountProp);
|
|
49
|
+
// 存在 children 时,自动按照默认的配置增加网格配置
|
|
50
|
+
if (children != null) {
|
|
51
|
+
var validChildren = React__default["default"].Children.toArray(children).filter(function (child) {
|
|
52
|
+
return /*#__PURE__*/React__default["default"].isValidElement(child);
|
|
53
|
+
});
|
|
54
|
+
if (validChildren.length === 0) return null;
|
|
55
|
+
return /*#__PURE__*/React__default["default"].createElement(Grid.Row, Object.assign({
|
|
56
|
+
ref: ref
|
|
57
|
+
}, rowProps), validChildren.map(function (child, index) {
|
|
58
|
+
var _a, _b;
|
|
59
|
+
var span = (_a = utils.getGridElSpan(child)) !== null && _a !== void 0 ? _a : colSpan;
|
|
60
|
+
return /*#__PURE__*/React__default["default"].createElement(Grid.Col, {
|
|
61
|
+
key: (_b = child.key) !== null && _b !== void 0 ? _b : index,
|
|
62
|
+
span: span
|
|
63
|
+
}, child);
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
if (!(nodes === null || nodes === void 0 ? void 0 : nodes.length)) return null;
|
|
67
|
+
return /*#__PURE__*/React__default["default"].createElement(Grid.Row, Object.assign({
|
|
68
|
+
ref: ref
|
|
69
|
+
}, rowProps), nodes.map(function (col, index) {
|
|
70
|
+
var _a;
|
|
71
|
+
var baseColProps = {
|
|
72
|
+
span: colSpan
|
|
73
|
+
};
|
|
74
|
+
var resolvedProps = typeof col.props === 'function' ? col.props({
|
|
75
|
+
columns: validColumns,
|
|
76
|
+
columnCount: columnCountProp
|
|
77
|
+
}) : col.props;
|
|
78
|
+
var colProps = Object.assign(Object.assign({}, baseColProps), resolvedProps);
|
|
79
|
+
if (utils.isInvalidGridEl(col.elem)) return null;
|
|
80
|
+
return /*#__PURE__*/React__default["default"].createElement(Grid.Col, Object.assign({
|
|
81
|
+
key: (_a = col.key) !== null && _a !== void 0 ? _a : index
|
|
82
|
+
}, colProps), col.elem);
|
|
83
|
+
}));
|
|
84
|
+
});
|
|
85
|
+
if (env.__DEV__) {
|
|
86
|
+
AutoGrid.displayName = 'AutoGrid';
|
|
87
|
+
}
|
|
88
|
+
exports.AutoGrid = AutoGrid;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
13
|
+
Object.defineProperty(exports, '__esModule', {
|
|
14
|
+
value: true
|
|
15
|
+
});
|
|
16
|
+
var tslib = require('tslib');
|
|
17
|
+
var React = require('react');
|
|
18
|
+
var useMergeRefs = require('@hi-ui/use-merge-refs');
|
|
19
|
+
var useColumnCount = require('./use-column-count.js');
|
|
20
|
+
var AutoGrid = require('./AutoGrid.js');
|
|
21
|
+
var env = require('@hi-ui/env');
|
|
22
|
+
function _interopDefaultCompat(e) {
|
|
23
|
+
return e && _typeof(e) === 'object' && 'default' in e ? e : {
|
|
24
|
+
'default': e
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
var React__default = /*#__PURE__*/_interopDefaultCompat(React);
|
|
28
|
+
var DynamicGrid = /*#__PURE__*/React.forwardRef(function DynamicGrid(props, ref) {
|
|
29
|
+
var columnCountProp = props.columnCount,
|
|
30
|
+
defaultColumnCount = props.defaultColumnCount,
|
|
31
|
+
wrapperElRefProp = props.wrapperElRef,
|
|
32
|
+
onColumnCountChange = props.onColumnCountChange,
|
|
33
|
+
breakpoints = props.breakpoints,
|
|
34
|
+
sensitive = props.sensitive,
|
|
35
|
+
autoGridProps = tslib.__rest(props, ["columnCount", "defaultColumnCount", "wrapperElRef", "onColumnCountChange", "breakpoints", "sensitive"]);
|
|
36
|
+
var wrapperRef = React.useRef(null);
|
|
37
|
+
var wrapperElRef = wrapperElRefProp !== null && wrapperElRefProp !== void 0 ? wrapperElRefProp : wrapperRef;
|
|
38
|
+
var columnCount = useColumnCount.useColumnCount({
|
|
39
|
+
columnCount: columnCountProp,
|
|
40
|
+
defaultColumnCount: defaultColumnCount,
|
|
41
|
+
wrapperElRef: columnCountProp == null ? wrapperElRef : undefined,
|
|
42
|
+
onColumnCountChange: onColumnCountChange,
|
|
43
|
+
breakpoints: breakpoints,
|
|
44
|
+
sensitive: sensitive
|
|
45
|
+
});
|
|
46
|
+
var mergedRef = useMergeRefs.useMergeRefs(wrapperElRefProp !== null && wrapperElRefProp !== void 0 ? wrapperElRefProp : wrapperRef, ref);
|
|
47
|
+
return /*#__PURE__*/React__default["default"].createElement("div", {
|
|
48
|
+
ref: mergedRef,
|
|
49
|
+
style: {
|
|
50
|
+
width: '100%'
|
|
51
|
+
}
|
|
52
|
+
}, /*#__PURE__*/React__default["default"].createElement(AutoGrid.AutoGrid, Object.assign({}, autoGridProps, {
|
|
53
|
+
columnCount: columnCount
|
|
54
|
+
})));
|
|
55
|
+
});
|
|
56
|
+
if (env.__DEV__) {
|
|
57
|
+
DynamicGrid.displayName = 'DynamicGrid';
|
|
58
|
+
}
|
|
59
|
+
exports.DynamicGrid = DynamicGrid;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
Object.defineProperty(exports, '__esModule', {
|
|
13
|
+
value: true
|
|
14
|
+
});
|
|
15
|
+
var React = require('react');
|
|
16
|
+
var useLatest = require('@hi-ui/use-latest');
|
|
17
|
+
var funcUtils = require('@hi-ui/func-utils');
|
|
18
|
+
var types = require('../types.js');
|
|
19
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
20
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
21
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
22
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
23
|
+
if (it) o = it;
|
|
24
|
+
var i = 0;
|
|
25
|
+
return function () {
|
|
26
|
+
if (i >= o.length) return {
|
|
27
|
+
done: true
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
done: false,
|
|
31
|
+
value: o[i++]
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
36
|
+
}
|
|
37
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
38
|
+
if (!o) return;
|
|
39
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
40
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
41
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
42
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
43
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
44
|
+
}
|
|
45
|
+
function _arrayLikeToArray(arr, len) {
|
|
46
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
47
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) {
|
|
48
|
+
arr2[i] = arr[i];
|
|
49
|
+
}
|
|
50
|
+
return arr2;
|
|
51
|
+
}
|
|
52
|
+
var DEFAULT_COLUMN_COUNT = 3;
|
|
53
|
+
/** 元素未挂载时轮询检查的最大时长(ms) */
|
|
54
|
+
var MAX_EL_READY_POLL_DURATION = 3000;
|
|
55
|
+
/** 轮询检查元素的间隔(ms) */
|
|
56
|
+
var EL_READY_POLL_INTERVAL = 50;
|
|
57
|
+
// 根据容器宽度动态计算列数(与 GridResponsiveSize 断点一致)
|
|
58
|
+
var DEFAULT_BREAKPOINTS = [[types.GRID_VIEWPORT_BREAKPOINTS.sm, 1], [types.GRID_VIEWPORT_BREAKPOINTS.md, 2], [types.GRID_VIEWPORT_BREAKPOINTS.lg, 3], [types.GRID_VIEWPORT_BREAKPOINTS.xl, 4], [Infinity, 4] // xl: 宽度 >= 1200px,4 列
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
function useColumnCount(ctx) {
|
|
62
|
+
var _a, _b;
|
|
63
|
+
var ctxRef = useLatest.useLatestRef(ctx);
|
|
64
|
+
var _useState = React.useState((_b = (_a = ctx.columnCount) !== null && _a !== void 0 ? _a : ctx.defaultColumnCount) !== null && _b !== void 0 ? _b : DEFAULT_COLUMN_COUNT),
|
|
65
|
+
columnCount = _useState[0],
|
|
66
|
+
_setColumnCount = _useState[1];
|
|
67
|
+
var columnCountRef = useLatest.useLatestRef(columnCount);
|
|
68
|
+
var onColumnCountChangeRef = useLatest.useLatestRef(ctx.onColumnCountChange);
|
|
69
|
+
var setColumnCount = React.useMemo(function () {
|
|
70
|
+
return funcUtils.debounce(function (next) {
|
|
71
|
+
var prev = columnCountRef.current;
|
|
72
|
+
_setColumnCount(next);
|
|
73
|
+
// 如果外部有传入,并且实际可视列数变化了,则触发回调
|
|
74
|
+
var fn = onColumnCountChangeRef.current;
|
|
75
|
+
if (fn && prev !== next) fn(next);
|
|
76
|
+
}, 32);
|
|
77
|
+
}, [columnCountRef, onColumnCountChangeRef]);
|
|
78
|
+
var isFirstCalcRef = React.useRef(true);
|
|
79
|
+
var breakpointsRef = useLatest.useLatestRef(ctx.breakpoints);
|
|
80
|
+
var pollTimerRef = React.useRef(null);
|
|
81
|
+
var observerRef = React.useRef(null);
|
|
82
|
+
React.useEffect(function () {
|
|
83
|
+
var _a;
|
|
84
|
+
// 外部有传入,直接使用
|
|
85
|
+
if (ctx.columnCount != null) {
|
|
86
|
+
setColumnCount(ctx.columnCount);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
var setupObserver = function setupObserver(targetEl) {
|
|
90
|
+
// 监听元素宽度变化
|
|
91
|
+
var observer = new ResizeObserver(function (entries) {
|
|
92
|
+
var _a, _b, _c;
|
|
93
|
+
if (((_a = ctxRef.current) === null || _a === void 0 ? void 0 : _a.sensitive) && isFirstCalcRef.current) {
|
|
94
|
+
isFirstCalcRef.current = false;
|
|
95
|
+
setColumnCount((_b = columnCountRef.current) !== null && _b !== void 0 ? _b : DEFAULT_COLUMN_COUNT);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
var entry = entries[0];
|
|
99
|
+
var width = entry.contentRect.width;
|
|
100
|
+
var breakpoints = (_c = breakpointsRef.current) !== null && _c !== void 0 ? _c : DEFAULT_BREAKPOINTS;
|
|
101
|
+
setColumnCount(calcColumnCount(width, breakpoints));
|
|
102
|
+
});
|
|
103
|
+
observer.observe(targetEl);
|
|
104
|
+
observerRef.current = observer;
|
|
105
|
+
};
|
|
106
|
+
var cleanup = function cleanup() {
|
|
107
|
+
if (pollTimerRef.current) {
|
|
108
|
+
clearInterval(pollTimerRef.current);
|
|
109
|
+
pollTimerRef.current = null;
|
|
110
|
+
}
|
|
111
|
+
if (observerRef.current) {
|
|
112
|
+
observerRef.current.disconnect();
|
|
113
|
+
observerRef.current = null;
|
|
114
|
+
}
|
|
115
|
+
setColumnCount.cancel();
|
|
116
|
+
};
|
|
117
|
+
var el = (_a = ctx.wrapperElRef) === null || _a === void 0 ? void 0 : _a.current;
|
|
118
|
+
// 元素已挂载时,直接启动监听
|
|
119
|
+
if (el) {
|
|
120
|
+
setupObserver(el);
|
|
121
|
+
return cleanup;
|
|
122
|
+
}
|
|
123
|
+
// 元素未挂载时,启动定时器轮询检查,超时后使用默认值
|
|
124
|
+
// 元素未挂载时,启动定时器轮询检查
|
|
125
|
+
var startTime = Date.now();
|
|
126
|
+
pollTimerRef.current = setInterval(function () {
|
|
127
|
+
var _a, _b, _c, _d;
|
|
128
|
+
// 1. 检查元素是否已挂载
|
|
129
|
+
var currentEl = (_b = (_a = ctxRef.current) === null || _a === void 0 ? void 0 : _a.wrapperElRef) === null || _b === void 0 ? void 0 : _b.current;
|
|
130
|
+
if (currentEl) {
|
|
131
|
+
// 2. 已挂载:清除定时器,设置 ResizeObserver
|
|
132
|
+
if (pollTimerRef.current) {
|
|
133
|
+
clearInterval(pollTimerRef.current);
|
|
134
|
+
pollTimerRef.current = null;
|
|
135
|
+
}
|
|
136
|
+
setupObserver(currentEl);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
// 3. 未挂载且超时:清除定时器,使用默认列数
|
|
140
|
+
if (Date.now() - startTime >= MAX_EL_READY_POLL_DURATION) {
|
|
141
|
+
if (pollTimerRef.current) {
|
|
142
|
+
clearInterval(pollTimerRef.current);
|
|
143
|
+
pollTimerRef.current = null;
|
|
144
|
+
}
|
|
145
|
+
setColumnCount((_d = (_c = ctxRef.current) === null || _c === void 0 ? void 0 : _c.defaultColumnCount) !== null && _d !== void 0 ? _d : DEFAULT_COLUMN_COUNT);
|
|
146
|
+
}
|
|
147
|
+
}, EL_READY_POLL_INTERVAL);
|
|
148
|
+
return cleanup;
|
|
149
|
+
}, [ctx.columnCount, ctx.wrapperElRef, ctxRef, setColumnCount, breakpointsRef, columnCountRef]);
|
|
150
|
+
return columnCount;
|
|
151
|
+
}
|
|
152
|
+
function calcColumnCount(width, breakpoints) {
|
|
153
|
+
var column = 1; // 默认为1列
|
|
154
|
+
for (var _iterator = _createForOfIteratorHelperLoose(breakpoints), _step; !(_step = _iterator()).done;) {
|
|
155
|
+
var _step$value = _step.value,
|
|
156
|
+
breakpoint = _step$value[0],
|
|
157
|
+
cols = _step$value[1];
|
|
158
|
+
if (width <= breakpoint) {
|
|
159
|
+
column = cols;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return column;
|
|
164
|
+
}
|
|
165
|
+
exports.DEFAULT_COLUMN_COUNT = DEFAULT_COLUMN_COUNT;
|
|
166
|
+
exports.calcColumnCount = calcColumnCount;
|
|
167
|
+
exports.useColumnCount = useColumnCount;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
13
|
+
Object.defineProperty(exports, '__esModule', {
|
|
14
|
+
value: true
|
|
15
|
+
});
|
|
16
|
+
var React = require('react');
|
|
17
|
+
function _interopDefaultCompat(e) {
|
|
18
|
+
return e && _typeof(e) === 'object' && 'default' in e ? e : {
|
|
19
|
+
'default': e
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
var React__default = /*#__PURE__*/_interopDefaultCompat(React);
|
|
23
|
+
function isInvalidGridEl(el) {
|
|
24
|
+
var _a;
|
|
25
|
+
// null 直接返回
|
|
26
|
+
if (el === null) return true;
|
|
27
|
+
var isValidElement = /*#__PURE__*/React__default["default"].isValidElement(el);
|
|
28
|
+
// 无效元素直接返回
|
|
29
|
+
if (!isValidElement) return true;
|
|
30
|
+
// 如果是一个元素,并且被配置为隐藏,则认为是一个无效的元素
|
|
31
|
+
var element = el;
|
|
32
|
+
var isHiddenEl = (_a = element === null || element === void 0 ? void 0 : element.props) === null || _a === void 0 ? void 0 : _a['data-grid-hidden'];
|
|
33
|
+
if (isHiddenEl) return true;
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
function getGridElSpan(el) {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
try {
|
|
39
|
+
var props = (_a = el) === null || _a === void 0 ? void 0 : _a.props;
|
|
40
|
+
var span = (_b = props === null || props === void 0 ? void 0 : props.span) !== null && _b !== void 0 ? _b : props === null || props === void 0 ? void 0 : props['data-span'];
|
|
41
|
+
if (typeof span === 'number' && !isNaN(span) && span >= 0) return span;
|
|
42
|
+
return null;
|
|
43
|
+
} catch (_c) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.getGridElSpan = getGridElSpan;
|
|
48
|
+
exports.isInvalidGridEl = isInvalidGridEl;
|
package/lib/cjs/index.js
CHANGED
|
@@ -14,10 +14,16 @@ Object.defineProperty(exports, '__esModule', {
|
|
|
14
14
|
});
|
|
15
15
|
require('./styles/index.scss.js');
|
|
16
16
|
var Grid$1 = require('./Grid.js');
|
|
17
|
+
var types = require('./types.js');
|
|
18
|
+
var AutoGrid = require('./auto-grid/AutoGrid.js');
|
|
19
|
+
var DynamicGrid = require('./auto-grid/DynamicGrid.js');
|
|
17
20
|
var Grid = {
|
|
18
21
|
Row: Grid$1.Row,
|
|
19
22
|
Col: Grid$1.Col
|
|
20
23
|
};
|
|
21
24
|
exports.Col = Grid$1.Col;
|
|
22
25
|
exports.Row = Grid$1.Row;
|
|
26
|
+
exports.GRID_VIEWPORT_BREAKPOINTS = types.GRID_VIEWPORT_BREAKPOINTS;
|
|
27
|
+
exports.AutoGrid = AutoGrid.AutoGrid;
|
|
28
|
+
exports.DynamicGrid = DynamicGrid.DynamicGrid;
|
|
23
29
|
exports["default"] = Grid;
|
package/lib/cjs/types.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
Object.defineProperty(exports, '__esModule', {
|
|
13
|
+
value: true
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 栅格响应式断点
|
|
18
|
+
* - xs: 0px ~ 575px
|
|
19
|
+
* - sm: 576px ~ 767px
|
|
20
|
+
* - md: 768px ~ 991px
|
|
21
|
+
* - lg: 992px ~ 1199px
|
|
22
|
+
* - xl: 1200px ~ Infinity
|
|
23
|
+
*/
|
|
24
|
+
var GRID_VIEWPORT_BREAKPOINTS = {
|
|
25
|
+
xs: 0,
|
|
26
|
+
sm: 576,
|
|
27
|
+
md: 768,
|
|
28
|
+
lg: 992,
|
|
29
|
+
xl: 1200
|
|
30
|
+
};
|
|
31
|
+
exports.GRID_VIEWPORT_BREAKPOINTS = GRID_VIEWPORT_BREAKPOINTS;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { __rest } from 'tslib';
|
|
11
|
+
import React, { forwardRef } from 'react';
|
|
12
|
+
import { Row, Col } from '../Grid.js';
|
|
13
|
+
import { DEFAULT_COLUMN_COUNT } from './use-column-count.js';
|
|
14
|
+
import { getGridElSpan, isInvalidGridEl } from './utils.js';
|
|
15
|
+
import { __DEV__ } from '@hi-ui/env';
|
|
16
|
+
var DEFAULT_COLUMNS = 24;
|
|
17
|
+
var AutoGrid = /*#__PURE__*/forwardRef(function (_a, ref) {
|
|
18
|
+
var _b;
|
|
19
|
+
var nodes = _a.nodes,
|
|
20
|
+
children = _a.children,
|
|
21
|
+
_a$columnCount = _a.columnCount,
|
|
22
|
+
columnCountProp = _a$columnCount === void 0 ? DEFAULT_COLUMN_COUNT : _a$columnCount,
|
|
23
|
+
_a$columns = _a.columns,
|
|
24
|
+
columns = _a$columns === void 0 ? DEFAULT_COLUMNS : _a$columns,
|
|
25
|
+
_a$gutter = _a.gutter,
|
|
26
|
+
gutter = _a$gutter === void 0 ? true : _a$gutter,
|
|
27
|
+
_a$rowGap = _a.rowGap,
|
|
28
|
+
rowGap = _a$rowGap === void 0 ? 0 : _a$rowGap,
|
|
29
|
+
restRowProps = __rest(_a, ["nodes", "children", "columnCount", "columns", "gutter", "rowGap"]);
|
|
30
|
+
var rowProps = Object.assign({
|
|
31
|
+
columns: columns,
|
|
32
|
+
gutter: gutter,
|
|
33
|
+
rowGap: rowGap
|
|
34
|
+
}, restRowProps);
|
|
35
|
+
var validColumns = (_b = rowProps.columns) !== null && _b !== void 0 ? _b : DEFAULT_COLUMNS;
|
|
36
|
+
var colSpan = Math.floor(validColumns / columnCountProp);
|
|
37
|
+
// 存在 children 时,自动按照默认的配置增加网格配置
|
|
38
|
+
if (children != null) {
|
|
39
|
+
var validChildren = React.Children.toArray(children).filter(function (child) {
|
|
40
|
+
return /*#__PURE__*/React.isValidElement(child);
|
|
41
|
+
});
|
|
42
|
+
if (validChildren.length === 0) return null;
|
|
43
|
+
return /*#__PURE__*/React.createElement(Row, Object.assign({
|
|
44
|
+
ref: ref
|
|
45
|
+
}, rowProps), validChildren.map(function (child, index) {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
var span = (_a = getGridElSpan(child)) !== null && _a !== void 0 ? _a : colSpan;
|
|
48
|
+
return /*#__PURE__*/React.createElement(Col, {
|
|
49
|
+
key: (_b = child.key) !== null && _b !== void 0 ? _b : index,
|
|
50
|
+
span: span
|
|
51
|
+
}, child);
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
if (!(nodes === null || nodes === void 0 ? void 0 : nodes.length)) return null;
|
|
55
|
+
return /*#__PURE__*/React.createElement(Row, Object.assign({
|
|
56
|
+
ref: ref
|
|
57
|
+
}, rowProps), nodes.map(function (col, index) {
|
|
58
|
+
var _a;
|
|
59
|
+
var baseColProps = {
|
|
60
|
+
span: colSpan
|
|
61
|
+
};
|
|
62
|
+
var resolvedProps = typeof col.props === 'function' ? col.props({
|
|
63
|
+
columns: validColumns,
|
|
64
|
+
columnCount: columnCountProp
|
|
65
|
+
}) : col.props;
|
|
66
|
+
var colProps = Object.assign(Object.assign({}, baseColProps), resolvedProps);
|
|
67
|
+
if (isInvalidGridEl(col.elem)) return null;
|
|
68
|
+
return /*#__PURE__*/React.createElement(Col, Object.assign({
|
|
69
|
+
key: (_a = col.key) !== null && _a !== void 0 ? _a : index
|
|
70
|
+
}, colProps), col.elem);
|
|
71
|
+
}));
|
|
72
|
+
});
|
|
73
|
+
if (__DEV__) {
|
|
74
|
+
AutoGrid.displayName = 'AutoGrid';
|
|
75
|
+
}
|
|
76
|
+
export { AutoGrid };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { __rest } from 'tslib';
|
|
11
|
+
import React, { forwardRef, useRef } from 'react';
|
|
12
|
+
import { useMergeRefs } from '@hi-ui/use-merge-refs';
|
|
13
|
+
import { useColumnCount } from './use-column-count.js';
|
|
14
|
+
import { AutoGrid } from './AutoGrid.js';
|
|
15
|
+
import { __DEV__ } from '@hi-ui/env';
|
|
16
|
+
var DynamicGrid = /*#__PURE__*/forwardRef(function DynamicGrid(props, ref) {
|
|
17
|
+
var columnCountProp = props.columnCount,
|
|
18
|
+
defaultColumnCount = props.defaultColumnCount,
|
|
19
|
+
wrapperElRefProp = props.wrapperElRef,
|
|
20
|
+
onColumnCountChange = props.onColumnCountChange,
|
|
21
|
+
breakpoints = props.breakpoints,
|
|
22
|
+
sensitive = props.sensitive,
|
|
23
|
+
autoGridProps = __rest(props, ["columnCount", "defaultColumnCount", "wrapperElRef", "onColumnCountChange", "breakpoints", "sensitive"]);
|
|
24
|
+
var wrapperRef = useRef(null);
|
|
25
|
+
var wrapperElRef = wrapperElRefProp !== null && wrapperElRefProp !== void 0 ? wrapperElRefProp : wrapperRef;
|
|
26
|
+
var columnCount = useColumnCount({
|
|
27
|
+
columnCount: columnCountProp,
|
|
28
|
+
defaultColumnCount: defaultColumnCount,
|
|
29
|
+
wrapperElRef: columnCountProp == null ? wrapperElRef : undefined,
|
|
30
|
+
onColumnCountChange: onColumnCountChange,
|
|
31
|
+
breakpoints: breakpoints,
|
|
32
|
+
sensitive: sensitive
|
|
33
|
+
});
|
|
34
|
+
var mergedRef = useMergeRefs(wrapperElRefProp !== null && wrapperElRefProp !== void 0 ? wrapperElRefProp : wrapperRef, ref);
|
|
35
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
36
|
+
ref: mergedRef,
|
|
37
|
+
style: {
|
|
38
|
+
width: '100%'
|
|
39
|
+
}
|
|
40
|
+
}, /*#__PURE__*/React.createElement(AutoGrid, Object.assign({}, autoGridProps, {
|
|
41
|
+
columnCount: columnCount
|
|
42
|
+
})));
|
|
43
|
+
});
|
|
44
|
+
if (__DEV__) {
|
|
45
|
+
DynamicGrid.displayName = 'DynamicGrid';
|
|
46
|
+
}
|
|
47
|
+
export { DynamicGrid };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { useState, useMemo, useRef, useEffect } from 'react';
|
|
11
|
+
import { useLatestRef } from '@hi-ui/use-latest';
|
|
12
|
+
import { debounce } from '@hi-ui/func-utils';
|
|
13
|
+
import { GRID_VIEWPORT_BREAKPOINTS } from '../types.js';
|
|
14
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
15
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
16
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
17
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
18
|
+
if (it) o = it;
|
|
19
|
+
var i = 0;
|
|
20
|
+
return function () {
|
|
21
|
+
if (i >= o.length) return {
|
|
22
|
+
done: true
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
done: false,
|
|
26
|
+
value: o[i++]
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
31
|
+
}
|
|
32
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
33
|
+
if (!o) return;
|
|
34
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
35
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
36
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
37
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
38
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
39
|
+
}
|
|
40
|
+
function _arrayLikeToArray(arr, len) {
|
|
41
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
42
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) {
|
|
43
|
+
arr2[i] = arr[i];
|
|
44
|
+
}
|
|
45
|
+
return arr2;
|
|
46
|
+
}
|
|
47
|
+
var DEFAULT_COLUMN_COUNT = 3;
|
|
48
|
+
/** 元素未挂载时轮询检查的最大时长(ms) */
|
|
49
|
+
var MAX_EL_READY_POLL_DURATION = 3000;
|
|
50
|
+
/** 轮询检查元素的间隔(ms) */
|
|
51
|
+
var EL_READY_POLL_INTERVAL = 50;
|
|
52
|
+
// 根据容器宽度动态计算列数(与 GridResponsiveSize 断点一致)
|
|
53
|
+
var DEFAULT_BREAKPOINTS = [[GRID_VIEWPORT_BREAKPOINTS.sm, 1], [GRID_VIEWPORT_BREAKPOINTS.md, 2], [GRID_VIEWPORT_BREAKPOINTS.lg, 3], [GRID_VIEWPORT_BREAKPOINTS.xl, 4], [Infinity, 4] // xl: 宽度 >= 1200px,4 列
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
function useColumnCount(ctx) {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
var ctxRef = useLatestRef(ctx);
|
|
59
|
+
var _useState = useState((_b = (_a = ctx.columnCount) !== null && _a !== void 0 ? _a : ctx.defaultColumnCount) !== null && _b !== void 0 ? _b : DEFAULT_COLUMN_COUNT),
|
|
60
|
+
columnCount = _useState[0],
|
|
61
|
+
_setColumnCount = _useState[1];
|
|
62
|
+
var columnCountRef = useLatestRef(columnCount);
|
|
63
|
+
var onColumnCountChangeRef = useLatestRef(ctx.onColumnCountChange);
|
|
64
|
+
var setColumnCount = useMemo(function () {
|
|
65
|
+
return debounce(function (next) {
|
|
66
|
+
var prev = columnCountRef.current;
|
|
67
|
+
_setColumnCount(next);
|
|
68
|
+
// 如果外部有传入,并且实际可视列数变化了,则触发回调
|
|
69
|
+
var fn = onColumnCountChangeRef.current;
|
|
70
|
+
if (fn && prev !== next) fn(next);
|
|
71
|
+
}, 32);
|
|
72
|
+
}, [columnCountRef, onColumnCountChangeRef]);
|
|
73
|
+
var isFirstCalcRef = useRef(true);
|
|
74
|
+
var breakpointsRef = useLatestRef(ctx.breakpoints);
|
|
75
|
+
var pollTimerRef = useRef(null);
|
|
76
|
+
var observerRef = useRef(null);
|
|
77
|
+
useEffect(function () {
|
|
78
|
+
var _a;
|
|
79
|
+
// 外部有传入,直接使用
|
|
80
|
+
if (ctx.columnCount != null) {
|
|
81
|
+
setColumnCount(ctx.columnCount);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
var setupObserver = function setupObserver(targetEl) {
|
|
85
|
+
// 监听元素宽度变化
|
|
86
|
+
var observer = new ResizeObserver(function (entries) {
|
|
87
|
+
var _a, _b, _c;
|
|
88
|
+
if (((_a = ctxRef.current) === null || _a === void 0 ? void 0 : _a.sensitive) && isFirstCalcRef.current) {
|
|
89
|
+
isFirstCalcRef.current = false;
|
|
90
|
+
setColumnCount((_b = columnCountRef.current) !== null && _b !== void 0 ? _b : DEFAULT_COLUMN_COUNT);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
var entry = entries[0];
|
|
94
|
+
var width = entry.contentRect.width;
|
|
95
|
+
var breakpoints = (_c = breakpointsRef.current) !== null && _c !== void 0 ? _c : DEFAULT_BREAKPOINTS;
|
|
96
|
+
setColumnCount(calcColumnCount(width, breakpoints));
|
|
97
|
+
});
|
|
98
|
+
observer.observe(targetEl);
|
|
99
|
+
observerRef.current = observer;
|
|
100
|
+
};
|
|
101
|
+
var cleanup = function cleanup() {
|
|
102
|
+
if (pollTimerRef.current) {
|
|
103
|
+
clearInterval(pollTimerRef.current);
|
|
104
|
+
pollTimerRef.current = null;
|
|
105
|
+
}
|
|
106
|
+
if (observerRef.current) {
|
|
107
|
+
observerRef.current.disconnect();
|
|
108
|
+
observerRef.current = null;
|
|
109
|
+
}
|
|
110
|
+
setColumnCount.cancel();
|
|
111
|
+
};
|
|
112
|
+
var el = (_a = ctx.wrapperElRef) === null || _a === void 0 ? void 0 : _a.current;
|
|
113
|
+
// 元素已挂载时,直接启动监听
|
|
114
|
+
if (el) {
|
|
115
|
+
setupObserver(el);
|
|
116
|
+
return cleanup;
|
|
117
|
+
}
|
|
118
|
+
// 元素未挂载时,启动定时器轮询检查,超时后使用默认值
|
|
119
|
+
// 元素未挂载时,启动定时器轮询检查
|
|
120
|
+
var startTime = Date.now();
|
|
121
|
+
pollTimerRef.current = setInterval(function () {
|
|
122
|
+
var _a, _b, _c, _d;
|
|
123
|
+
// 1. 检查元素是否已挂载
|
|
124
|
+
var currentEl = (_b = (_a = ctxRef.current) === null || _a === void 0 ? void 0 : _a.wrapperElRef) === null || _b === void 0 ? void 0 : _b.current;
|
|
125
|
+
if (currentEl) {
|
|
126
|
+
// 2. 已挂载:清除定时器,设置 ResizeObserver
|
|
127
|
+
if (pollTimerRef.current) {
|
|
128
|
+
clearInterval(pollTimerRef.current);
|
|
129
|
+
pollTimerRef.current = null;
|
|
130
|
+
}
|
|
131
|
+
setupObserver(currentEl);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// 3. 未挂载且超时:清除定时器,使用默认列数
|
|
135
|
+
if (Date.now() - startTime >= MAX_EL_READY_POLL_DURATION) {
|
|
136
|
+
if (pollTimerRef.current) {
|
|
137
|
+
clearInterval(pollTimerRef.current);
|
|
138
|
+
pollTimerRef.current = null;
|
|
139
|
+
}
|
|
140
|
+
setColumnCount((_d = (_c = ctxRef.current) === null || _c === void 0 ? void 0 : _c.defaultColumnCount) !== null && _d !== void 0 ? _d : DEFAULT_COLUMN_COUNT);
|
|
141
|
+
}
|
|
142
|
+
}, EL_READY_POLL_INTERVAL);
|
|
143
|
+
return cleanup;
|
|
144
|
+
}, [ctx.columnCount, ctx.wrapperElRef, ctxRef, setColumnCount, breakpointsRef, columnCountRef]);
|
|
145
|
+
return columnCount;
|
|
146
|
+
}
|
|
147
|
+
function calcColumnCount(width, breakpoints) {
|
|
148
|
+
var column = 1; // 默认为1列
|
|
149
|
+
for (var _iterator = _createForOfIteratorHelperLoose(breakpoints), _step; !(_step = _iterator()).done;) {
|
|
150
|
+
var _step$value = _step.value,
|
|
151
|
+
breakpoint = _step$value[0],
|
|
152
|
+
cols = _step$value[1];
|
|
153
|
+
if (width <= breakpoint) {
|
|
154
|
+
column = cols;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return column;
|
|
159
|
+
}
|
|
160
|
+
export { DEFAULT_COLUMN_COUNT, calcColumnCount, useColumnCount };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import React from 'react';
|
|
11
|
+
function isInvalidGridEl(el) {
|
|
12
|
+
var _a;
|
|
13
|
+
// null 直接返回
|
|
14
|
+
if (el === null) return true;
|
|
15
|
+
var isValidElement = /*#__PURE__*/React.isValidElement(el);
|
|
16
|
+
// 无效元素直接返回
|
|
17
|
+
if (!isValidElement) return true;
|
|
18
|
+
// 如果是一个元素,并且被配置为隐藏,则认为是一个无效的元素
|
|
19
|
+
var element = el;
|
|
20
|
+
var isHiddenEl = (_a = element === null || element === void 0 ? void 0 : element.props) === null || _a === void 0 ? void 0 : _a['data-grid-hidden'];
|
|
21
|
+
if (isHiddenEl) return true;
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
function getGridElSpan(el) {
|
|
25
|
+
var _a, _b;
|
|
26
|
+
try {
|
|
27
|
+
var props = (_a = el) === null || _a === void 0 ? void 0 : _a.props;
|
|
28
|
+
var span = (_b = props === null || props === void 0 ? void 0 : props.span) !== null && _b !== void 0 ? _b : props === null || props === void 0 ? void 0 : props['data-span'];
|
|
29
|
+
if (typeof span === 'number' && !isNaN(span) && span >= 0) return span;
|
|
30
|
+
return null;
|
|
31
|
+
} catch (_c) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export { getGridElSpan, isInvalidGridEl };
|
package/lib/esm/index.js
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import './styles/index.scss.js';
|
|
11
11
|
import { Row, Col } from './Grid.js';
|
|
12
|
+
export { GRID_VIEWPORT_BREAKPOINTS } from './types.js';
|
|
13
|
+
export { AutoGrid } from './auto-grid/AutoGrid.js';
|
|
14
|
+
export { DynamicGrid } from './auto-grid/DynamicGrid.js';
|
|
12
15
|
var Grid = {
|
|
13
16
|
Row: Row,
|
|
14
17
|
Col: Col
|
package/lib/esm/types.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/grid
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/grid#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HiUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* 栅格响应式断点
|
|
12
|
+
* - xs: 0px ~ 575px
|
|
13
|
+
* - sm: 576px ~ 767px
|
|
14
|
+
* - md: 768px ~ 991px
|
|
15
|
+
* - lg: 992px ~ 1199px
|
|
16
|
+
* - xl: 1200px ~ Infinity
|
|
17
|
+
*/
|
|
18
|
+
var GRID_VIEWPORT_BREAKPOINTS = {
|
|
19
|
+
xs: 0,
|
|
20
|
+
sm: 576,
|
|
21
|
+
md: 768,
|
|
22
|
+
lg: 992,
|
|
23
|
+
xl: 1200
|
|
24
|
+
};
|
|
25
|
+
export { GRID_VIEWPORT_BREAKPOINTS };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { RowProps, ColProps } from '../Grid';
|
|
3
|
+
export interface AutoGridNodeType {
|
|
4
|
+
key: React.Key;
|
|
5
|
+
elem: React.ReactElement | null;
|
|
6
|
+
props?: ColProps | ((ctx: {
|
|
7
|
+
columns: number;
|
|
8
|
+
columnCount: number;
|
|
9
|
+
}) => ColProps);
|
|
10
|
+
}
|
|
11
|
+
export declare type AutoGridProps = RowProps & {
|
|
12
|
+
/** 列元素清单 */
|
|
13
|
+
nodes?: AutoGridNodeType[];
|
|
14
|
+
/**
|
|
15
|
+
* 子元素清单
|
|
16
|
+
* - 存在子元素时,会自动按照默认的配置增加网格包裹
|
|
17
|
+
* - 如需控制子元素占据的网格数,请在元素上增加 span 或者 data-span 属性
|
|
18
|
+
*/
|
|
19
|
+
children?: React.ReactNode[];
|
|
20
|
+
/** 列数 */
|
|
21
|
+
columnCount?: number;
|
|
22
|
+
};
|
|
23
|
+
export declare const AutoGrid: React.ForwardRefExoticComponent<RowProps & {
|
|
24
|
+
/** 列元素清单 */
|
|
25
|
+
nodes?: AutoGridNodeType[] | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* 子元素清单
|
|
28
|
+
* - 存在子元素时,会自动按照默认的配置增加网格包裹
|
|
29
|
+
* - 如需控制子元素占据的网格数,请在元素上增加 span 或者 data-span 属性
|
|
30
|
+
*/
|
|
31
|
+
children?: React.ReactNode[] | undefined;
|
|
32
|
+
/** 列数 */
|
|
33
|
+
columnCount?: number | undefined;
|
|
34
|
+
} & React.RefAttributes<HTMLDivElement | null>>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { UseColumnCountCtxType } from './use-column-count';
|
|
3
|
+
import type { AutoGridProps } from './AutoGrid';
|
|
4
|
+
export declare type DynamicGridProps = AutoGridProps & UseColumnCountCtxType;
|
|
5
|
+
export declare const DynamicGrid: React.ForwardRefExoticComponent<import("..").RowProps & {
|
|
6
|
+
nodes?: import("./AutoGrid").AutoGridNodeType[] | undefined;
|
|
7
|
+
children?: React.ReactNode[] | undefined;
|
|
8
|
+
columnCount?: number | undefined;
|
|
9
|
+
} & UseColumnCountCtxType & React.RefAttributes<HTMLDivElement | null>>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export declare type BreakpointType = [width: number, column: number];
|
|
3
|
+
export declare type UseColumnCountCtxType = {
|
|
4
|
+
/**
|
|
5
|
+
* 列数,表示一行包含元素的数量
|
|
6
|
+
* @default 3
|
|
7
|
+
* @desc 传入此值时,列数固定,不因容器宽度变化而变化
|
|
8
|
+
* @desc 不传时,但额外传入 wrapperElRef 时,列数会根据容器宽度变化而变化
|
|
9
|
+
*/
|
|
10
|
+
columnCount?: number;
|
|
11
|
+
/**
|
|
12
|
+
* 默认列数
|
|
13
|
+
* @desc 与 columnCount 的区别是,仅会在初始化时使用
|
|
14
|
+
* @desc 传入后,列数仍会根据容器宽度变化而变化
|
|
15
|
+
*/
|
|
16
|
+
defaultColumnCount?: number;
|
|
17
|
+
/**
|
|
18
|
+
* 组件的外部包裹元素
|
|
19
|
+
* @desc 传入此值,同时不传 columnCount 时,列数会根据容器宽度变化而变化
|
|
20
|
+
*/
|
|
21
|
+
wrapperElRef?: React.RefObject<HTMLElement>;
|
|
22
|
+
/**
|
|
23
|
+
* 实际可视列数变化回调
|
|
24
|
+
*/
|
|
25
|
+
onColumnCountChange?: (columnCount: number) => void;
|
|
26
|
+
/**
|
|
27
|
+
* 列数变化断点
|
|
28
|
+
* @desc 默认使用 GRID_VIEWPORT_BREAKPOINTS: [576, 1], [768, 2], [992, 3], [1200, 4], [Infinity, 4]
|
|
29
|
+
*/
|
|
30
|
+
breakpoints?: BreakpointType[];
|
|
31
|
+
/**
|
|
32
|
+
* 是否对初始布局变化敏感
|
|
33
|
+
* @desc 当设置为 true 时,会等待布局稳定后再渲染内容,避免视觉跳动
|
|
34
|
+
*/
|
|
35
|
+
sensitive?: boolean;
|
|
36
|
+
};
|
|
37
|
+
export declare const DEFAULT_COLUMN_COUNT = 3;
|
|
38
|
+
export declare function useColumnCount(ctx: UseColumnCountCtxType): number;
|
|
39
|
+
export declare function calcColumnCount(width: number, breakpoints: BreakpointType[]): number;
|
package/lib/types/context.d.ts
CHANGED
package/lib/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
1
2
|
import './styles/index.scss';
|
|
2
3
|
declare const Grid: {
|
|
3
4
|
Row: import("react").ForwardRefExoticComponent<import("./Grid").RowProps & import("react").RefAttributes<HTMLDivElement | null>>;
|
|
@@ -6,3 +7,4 @@ declare const Grid: {
|
|
|
6
7
|
export default Grid;
|
|
7
8
|
export * from './Grid';
|
|
8
9
|
export * from './types';
|
|
10
|
+
export * from './auto-grid';
|
package/lib/types/types.d.ts
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 栅格响应式断点
|
|
3
|
+
* - xs: 0px ~ 575px
|
|
4
|
+
* - sm: 576px ~ 767px
|
|
5
|
+
* - md: 768px ~ 991px
|
|
6
|
+
* - lg: 992px ~ 1199px
|
|
7
|
+
* - xl: 1200px ~ Infinity
|
|
8
|
+
*/
|
|
9
|
+
export declare const GRID_VIEWPORT_BREAKPOINTS: {
|
|
10
|
+
readonly xs: 0;
|
|
11
|
+
readonly sm: 576;
|
|
12
|
+
readonly md: 768;
|
|
13
|
+
readonly lg: 992;
|
|
14
|
+
readonly xl: 1200;
|
|
15
|
+
};
|
|
16
|
+
export declare type GridJustifyEnum = 'flex-start' | 'flex-end' | 'center' | 'space-around' | 'space-between';
|
|
17
|
+
export declare type GridAlignEnum = 'flex-start' | 'flex-end' | 'center' | 'stretch';
|
|
3
18
|
export interface GridResponsiveSize<T> {
|
|
4
19
|
/**
|
|
5
20
|
* 设置屏幕 < 576px 时响应式栅格数
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hi-ui/grid",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-rc.0",
|
|
4
4
|
"description": "A sub-package for @hi-ui/hiui.",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "HiUI <mi-hiui@xiaomi.com>",
|
|
@@ -44,18 +44,21 @@
|
|
|
44
44
|
"url": "https://github.com/XiaoMi/hiui/issues"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@hi-ui/classname": "^5.0.0-
|
|
48
|
-
"@hi-ui/env": "^5.0.0-
|
|
49
|
-
"@hi-ui/
|
|
47
|
+
"@hi-ui/classname": "^5.0.0-rc.0",
|
|
48
|
+
"@hi-ui/env": "^5.0.0-rc.0",
|
|
49
|
+
"@hi-ui/func-utils": "^5.0.0-rc.0",
|
|
50
|
+
"@hi-ui/type-assertion": "^5.0.0-rc.0",
|
|
51
|
+
"@hi-ui/use-latest": "^5.0.0-rc.0",
|
|
52
|
+
"@hi-ui/use-merge-refs": "^5.0.0-rc.0"
|
|
50
53
|
},
|
|
51
54
|
"peerDependencies": {
|
|
52
|
-
"@hi-ui/core": ">=5.0.0-
|
|
55
|
+
"@hi-ui/core": ">=5.0.0-rc.0",
|
|
53
56
|
"react": ">=16.8.6",
|
|
54
57
|
"react-dom": ">=16.8.6"
|
|
55
58
|
},
|
|
56
59
|
"devDependencies": {
|
|
57
|
-
"@hi-ui/core": "^5.0.0-
|
|
58
|
-
"@hi-ui/core-css": "^5.0.0-
|
|
60
|
+
"@hi-ui/core": "^5.0.0-rc.0",
|
|
61
|
+
"@hi-ui/core-css": "^5.0.0-rc.0",
|
|
59
62
|
"react": "^17.0.1",
|
|
60
63
|
"react-dom": "^17.0.1"
|
|
61
64
|
}
|