@arcblock/ux 2.4.10 → 2.4.13

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.
@@ -3,14 +3,19 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.NavLink = exports.Link = void 0;
7
- exports.withExternalLink = withExternalLink;
6
+ exports.Link = Link;
7
+ exports.NavLink = NavLink;
8
+
9
+ var _propTypes = _interopRequireDefault(require("prop-types"));
8
10
 
9
11
  var _reactRouterDom = require("react-router-dom");
10
12
 
11
13
  var _jsxRuntime = require("react/jsx-runtime");
12
14
 
13
- const _excluded = ["to", "external"];
15
+ const _excluded = ["children", "routerLinkComponent", "to", "external"],
16
+ _excluded2 = ["className"];
17
+
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
19
 
15
20
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
16
21
 
@@ -22,35 +27,64 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return
22
27
 
23
28
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
24
29
 
25
- // 包裹 router Link/NavLink 组件, 支持 external link (external 为 true 时通过 click 事件跳转)
26
- function withExternalLink(RouterLinkComponent) {
27
- // eslint-disable-next-line react/prop-types
28
- return function WithExternalLink(_ref) {
29
- let {
30
- to,
31
- external
32
- } = _ref,
33
- rest = _objectWithoutProperties(_ref, _excluded);
34
-
35
- const handleClick = e => {
36
- e.preventDefault();
37
- window.location.href = to;
38
- };
39
-
40
- if (external) {
41
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(RouterLinkComponent, _objectSpread({
42
- onClick: handleClick,
43
- to: to
44
- }, rest));
45
- }
46
-
47
- return /*#__PURE__*/(0, _jsxRuntime.jsx)(RouterLinkComponent, _objectSpread({
48
- to: to
49
- }, rest));
50
- };
30
+ // 包裹 router Link/NavLink 组件, 支持 external link (external 为 true 时渲染为 a 标签)
31
+ function ExternalLink(_ref) {
32
+ let {
33
+ children,
34
+ routerLinkComponent: RouterLinkComponent,
35
+ to,
36
+ external
37
+ } = _ref,
38
+ rest = _objectWithoutProperties(_ref, _excluded);
39
+
40
+ if (external) {
41
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("a", _objectSpread(_objectSpread({
42
+ href: to
43
+ }, rest), {}, {
44
+ children: children
45
+ }));
46
+ }
47
+
48
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(RouterLinkComponent, _objectSpread(_objectSpread({
49
+ to: to
50
+ }, rest), {}, {
51
+ children: children
52
+ }));
51
53
  }
52
54
 
53
- const Link = withExternalLink(_reactRouterDom.Link);
54
- exports.Link = Link;
55
- const NavLink = withExternalLink(_reactRouterDom.NavLink);
56
- exports.NavLink = NavLink;
55
+ ExternalLink.propTypes = {
56
+ children: _propTypes.default.any.isRequired,
57
+ routerLinkComponent: _propTypes.default.elementType.isRequired,
58
+ external: _propTypes.default.bool,
59
+ to: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]).isRequired
60
+ };
61
+ ExternalLink.defaultProps = {
62
+ external: false
63
+ };
64
+
65
+ function Link(props) {
66
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(ExternalLink, _objectSpread({
67
+ routerLinkComponent: _reactRouterDom.Link
68
+ }, props));
69
+ }
70
+
71
+ function NavLink(_ref2) {
72
+ let {
73
+ className
74
+ } = _ref2,
75
+ rest = _objectWithoutProperties(_ref2, _excluded2);
76
+
77
+ // NavLink#className 支持 function
78
+ const classes = rest.external && typeof className === 'function' ? className({}) : className;
79
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(ExternalLink, _objectSpread({
80
+ routerLinkComponent: _reactRouterDom.NavLink,
81
+ className: classes
82
+ }, rest));
83
+ }
84
+
85
+ NavLink.propTypes = {
86
+ className: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func])
87
+ };
88
+ NavLink.defaultProps = {
89
+ className: ''
90
+ };
@@ -31,10 +31,10 @@ var _Footer = _interopRequireDefault(require("../../Footer"));
31
31
 
32
32
  var _sidebar = _interopRequireDefault(require("./sidebar"));
33
33
 
34
- var _withExternalLink = require("./with-external-link");
35
-
36
34
  var _Theme = require("../../Theme");
37
35
 
36
+ var _externalLink = require("./external-link");
37
+
38
38
  var _jsxRuntime = require("react/jsx-runtime");
39
39
 
40
40
  var _templateObject, _templateObject2;
@@ -80,12 +80,13 @@ function formatLinks(links, location) {
80
80
  }
81
81
 
82
82
  return _objectSpread(_objectSpread({}, link), {}, {
83
- label: /*#__PURE__*/(0, _jsxRuntime.jsx)(_withExternalLink.Link, {
84
- external: link.external,
83
+ label: /*#__PURE__*/(0, _jsxRuntime.jsx)(_externalLink.Link, {
85
84
  to: link.url,
85
+ external: link.external,
86
86
  children: link.title
87
87
  }),
88
- active: !!(0, _reactRouterDom.matchPath)({
88
+ // external = true 时 active 状态由传入 links 的调用方决定 (适用于 blocklet ui dashboard 的情况)
89
+ active: link.external ? link.active : !!(0, _reactRouterDom.matchPath)({
89
90
  path: link.url,
90
91
  end: false
91
92
  }, location.pathname)
@@ -13,10 +13,10 @@ var _colors = require("@mui/material/colors");
13
13
 
14
14
  var _clsx = _interopRequireDefault(require("clsx"));
15
15
 
16
- var _withExternalLink = require("./with-external-link");
17
-
18
16
  var _Theme = require("../../Theme");
19
17
 
18
+ var _externalLink = require("./external-link");
19
+
20
20
  var _jsxRuntime = require("react/jsx-runtime");
21
21
 
22
22
  var _templateObject;
@@ -64,17 +64,21 @@ function renderLinksItem(item) {
64
64
  icon,
65
65
  title,
66
66
  showBadge,
67
- external
68
- } = item;
67
+ external,
68
+ active
69
+ } = item; // external = true 时 link active 状态由传入 links 的调用方决定 (适用于 blocklet ui dashboard 的情况)
70
+
69
71
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
70
- children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_withExternalLink.NavLink, {
71
- to: url,
72
+ children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_externalLink.NavLink, {
72
73
  external: external,
74
+ to: url,
73
75
  className: _ref => {
74
76
  let {
75
77
  isActive
76
78
  } = _ref;
77
- return isActive ? 'layout-sidebar-link layout-sidebar-link--active' : 'layout-sidebar-link';
79
+ return (0, _clsx.default)('layout-sidebar-link', {
80
+ 'layout-sidebar-link--active': external ? active : isActive
81
+ });
78
82
  },
79
83
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
80
84
  className: "layout-sidebar-icon ".concat(showBadge ? 'layout-sidebar-badge' : ''),
@@ -56,11 +56,26 @@ const getLocaleFromSearchParams = function getLocaleFromSearchParams() {
56
56
  return null;
57
57
  };
58
58
 
59
+ const setLocaleParam = function setLocaleParam(locale) {
60
+ let url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href;
61
+ const urlObj = new URL(url);
62
+ const hasLocaleParam = urlObj.searchParams.has('locale');
63
+
64
+ if (hasLocaleParam) {
65
+ urlObj.searchParams.set('locale', locale);
66
+ window.history.replaceState({}, '', urlObj.href);
67
+ }
68
+ };
69
+
59
70
  const getLocale = locale => locale || getLocaleFromSearchParams() || _jsCookie.default.get(cookieName) || (0, _browserLang.default)(langParams);
60
71
 
61
72
  exports.getLocale = getLocale;
62
73
 
63
- const setLocale = locale => _jsCookie.default.set(cookieName, locale, (0, _Util.getCookieOptions)());
74
+ const setLocale = locale => {
75
+ _jsCookie.default.set(cookieName, locale, (0, _Util.getCookieOptions)());
76
+
77
+ setLocaleParam(locale);
78
+ };
64
79
 
65
80
  exports.setLocale = setLocale;
66
81
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ux",
3
- "version": "2.4.10",
3
+ "version": "2.4.13",
4
4
  "description": "Common used react components for arcblock products",
5
5
  "keywords": [
6
6
  "react",
@@ -47,10 +47,10 @@
47
47
  "react": ">=18.1.0",
48
48
  "react-ga": "^2.7.0"
49
49
  },
50
- "gitHead": "9780479afff4ed710a926cb930fb36bac5e46f8c",
50
+ "gitHead": "f374463572e397b959e9c1ce70cdbc9f53f99076",
51
51
  "dependencies": {
52
- "@arcblock/icons": "^2.4.10",
53
- "@arcblock/react-hooks": "^2.4.10",
52
+ "@arcblock/icons": "^2.4.13",
53
+ "@arcblock/react-hooks": "^2.4.13",
54
54
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
55
55
  "@emotion/react": "^11.10.0",
56
56
  "@emotion/styled": "^11.10.0",
@@ -0,0 +1,47 @@
1
+ import PropTypes from 'prop-types';
2
+ import { Link as RouterLink, NavLink as RouterNavLink } from 'react-router-dom';
3
+
4
+ // 包裹 router Link/NavLink 组件, 支持 external link (external 为 true 时渲染为 a 标签)
5
+ function ExternalLink({ children, routerLinkComponent: RouterLinkComponent, to, external, ...rest }) {
6
+ if (external) {
7
+ return (
8
+ <a href={to} {...rest}>
9
+ {children}
10
+ </a>
11
+ );
12
+ }
13
+ return (
14
+ <RouterLinkComponent to={to} {...rest}>
15
+ {children}
16
+ </RouterLinkComponent>
17
+ );
18
+ }
19
+
20
+ ExternalLink.propTypes = {
21
+ children: PropTypes.any.isRequired,
22
+ routerLinkComponent: PropTypes.elementType.isRequired,
23
+ external: PropTypes.bool,
24
+ to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
25
+ };
26
+
27
+ ExternalLink.defaultProps = {
28
+ external: false,
29
+ };
30
+
31
+ export function Link(props) {
32
+ return <ExternalLink routerLinkComponent={RouterLink} {...props} />;
33
+ }
34
+
35
+ export function NavLink({ className, ...rest }) {
36
+ // NavLink#className 支持 function
37
+ const classes = rest.external && typeof className === 'function' ? className({}) : className;
38
+ return <ExternalLink routerLinkComponent={RouterNavLink} className={classes} {...rest} />;
39
+ }
40
+
41
+ NavLink.propTypes = {
42
+ className: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
43
+ };
44
+
45
+ NavLink.defaultProps = {
46
+ className: '',
47
+ };
@@ -11,8 +11,8 @@ import { ResponsiveHeader } from '../../Header';
11
11
  import NavMenu from '../../NavMenu';
12
12
  import Footer from '../../Footer';
13
13
  import Sidebar from './sidebar';
14
- import { Link } from './with-external-link';
15
14
  import { styled, useTheme } from '../../Theme';
15
+ import { Link } from './external-link';
16
16
 
17
17
  // 监听 location 变化并关闭 header 中的 menu (确保 drawer - disablePortal:false, keepMounted:true)
18
18
  // 直接监听 menu 中 link 点击来触发 closeMenu 会有问题 (Suspense & lazy)
@@ -34,11 +34,12 @@ function formatLinks(links, location) {
34
34
  return {
35
35
  ...link,
36
36
  label: (
37
- <Link external={link.external} to={link.url}>
37
+ <Link to={link.url} external={link.external}>
38
38
  {link.title}
39
39
  </Link>
40
40
  ),
41
- active: !!matchPath({ path: link.url, end: false }, location.pathname),
41
+ // external = true 时 active 状态由传入 links 的调用方决定 (适用于 blocklet ui dashboard 的情况)
42
+ active: link.external ? link.active : !!matchPath({ path: link.url, end: false }, location.pathname),
42
43
  };
43
44
  });
44
45
  }
@@ -3,9 +3,8 @@ import PropTypes from 'prop-types';
3
3
  import Typography from '@mui/material/Typography';
4
4
  import { teal } from '@mui/material/colors';
5
5
  import clsx from 'clsx';
6
-
7
- import { NavLink } from './with-external-link';
8
6
  import { styled } from '../../Theme';
7
+ import { NavLink } from './external-link';
9
8
 
10
9
  // 渲染 links, 为 group links 添加分隔线, 返回展平后的、包含分隔线元素的 links 数组
11
10
  function renderLinks(links) {
@@ -25,14 +24,15 @@ function renderLinksItem(item) {
25
24
  if (Array.isArray(item)) {
26
25
  return item.map(renderLinksItem);
27
26
  }
28
- const { url, icon, title, showBadge, external } = item;
27
+ const { url, icon, title, showBadge, external, active } = item;
28
+ // external = true 时 link active 状态由传入 links 的调用方决定 (适用于 blocklet ui dashboard 的情况)
29
29
  return (
30
30
  <li key={url}>
31
31
  <NavLink
32
- to={url}
33
32
  external={external}
33
+ to={url}
34
34
  className={({ isActive }) =>
35
- isActive ? 'layout-sidebar-link layout-sidebar-link--active' : 'layout-sidebar-link'
35
+ clsx('layout-sidebar-link', { 'layout-sidebar-link--active': external ? active : isActive })
36
36
  }>
37
37
  <span className={`layout-sidebar-icon ${showBadge ? 'layout-sidebar-badge' : ''}`}>{icon}</span>
38
38
  <Typography component="span" className="layout-sidebar-link-text">
@@ -25,9 +25,21 @@ const getLocaleFromSearchParams = (url = window.location.href) => {
25
25
  return null;
26
26
  };
27
27
 
28
+ const setLocaleParam = (locale, url = window.location.href) => {
29
+ const urlObj = new URL(url);
30
+ const hasLocaleParam = urlObj.searchParams.has('locale');
31
+ if (hasLocaleParam) {
32
+ urlObj.searchParams.set('locale', locale);
33
+ window.history.replaceState({}, '', urlObj.href);
34
+ }
35
+ };
36
+
28
37
  const getLocale = (locale) =>
29
38
  locale || getLocaleFromSearchParams() || Cookie.get(cookieName) || browserLang(langParams);
30
- const setLocale = (locale) => Cookie.set(cookieName, locale, getCookieOptions());
39
+ const setLocale = (locale) => {
40
+ Cookie.set(cookieName, locale, getCookieOptions());
41
+ setLocaleParam(locale);
42
+ };
31
43
 
32
44
  const replace = (template, data) =>
33
45
  template.replace(/{(\w*)}/g, (m, key) => (data.hasOwnProperty(key) ? data[key] : ''));
@@ -1,20 +0,0 @@
1
- import { Link as RouterLink, NavLink as RouterNavLink } from 'react-router-dom';
2
-
3
- // 包裹 router Link/NavLink 组件, 支持 external link (external 为 true 时通过 click 事件跳转)
4
- export function withExternalLink(RouterLinkComponent) {
5
- // eslint-disable-next-line react/prop-types
6
- return function WithExternalLink({ to, external, ...rest }) {
7
- const handleClick = (e) => {
8
- e.preventDefault();
9
- window.location.href = to;
10
- };
11
- if (external) {
12
- return <RouterLinkComponent onClick={handleClick} to={to} {...rest} />;
13
- }
14
- return <RouterLinkComponent to={to} {...rest} />;
15
- };
16
- }
17
-
18
- export const Link = withExternalLink(RouterLink);
19
-
20
- export const NavLink = withExternalLink(RouterNavLink);