Skip to content

Commit

Permalink
feat: 🎸 appHistory and <Link /> can both take state (#477)
Browse files Browse the repository at this point in the history
* feat: 🎸 `appHistory` and `<Link />` can both take state

✅ Closes: #478
  • Loading branch information
maoxiaoke authored Dec 8, 2021
1 parent 73d8aa3 commit 2f42a89
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 25 deletions.
5 changes: 3 additions & 2 deletions packages/icestark-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Changelog

## 1.4.3
## 1.5.0

- [feat] `appHistory` and `<Link />` can both take state. ([#478](https://github.com/ice-lab/icestark/pull/428))
- [fix] add missing props for `registerAppEnter` & `registerAppLeave`.

## 1.4.2

- [fix] Bind history to window when using `AppLink`. ([#428](https://github.com/ice-lab/icestark/pull/428))
- [fix] bind history to window when using `AppLink`. ([#428](https://github.com/ice-lab/icestark/pull/428))

## 1.4.1

Expand Down
3 changes: 1 addition & 2 deletions packages/icestark-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/stark-app",
"version": "1.4.3",
"version": "1.5.0",
"description": "icestark-app is a JavaScript library for icestark, used by sub-application.",
"scripts": {
"build": "rm -rf lib && tsc",
Expand Down Expand Up @@ -42,7 +42,6 @@
"@types/jest": "^24.0.12",
"@types/node": "^12.0.0",
"codecov": "^3.4.0",
"eslint": "^5.16.0",
"husky": "^2.2.0",
"jest": "^24.7.1",
"stylelint": "^10.1.0",
Expand Down
27 changes: 23 additions & 4 deletions packages/icestark-app/src/AppLink.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import * as React from 'react';
import formatUrl from './util/formatUrl';

interface To {
/**
* A string representing the path link to
*/
pathname: string;
/**
* A string representing the url search to
*/
search?: string;
/**
* A string representing the url state to
*/
state?: object;
}

export type AppLinkProps = {
to: string;
to: string | To;
hashType?: boolean;
replace?: boolean;
message?: string;
Expand All @@ -11,12 +26,16 @@ export type AppLinkProps = {

const AppLink = (props: AppLinkProps) => {
const { to, hashType, replace, message, children, ...rest } = props;
const linkTo = formatUrl(to, hashType);

const _to = typeof to === 'object' ? (to.pathname + to.search) : to;
const _state = typeof to === 'object' ? to.state : {};

const linkTo = formatUrl(_to, hashType);
return (
<a
{...rest}
href={linkTo}
onClick={e => {
onClick={(e) => {
e.preventDefault();
// eslint-disable-next-line no-alert
if (message && window.confirm(message) === false) {
Expand All @@ -28,7 +47,7 @@ const AppLink = (props: AppLinkProps) => {
*/
const changeState = window.history[replace ? 'replaceState' : 'pushState'].bind(window);

changeState({}, null, linkTo);
changeState(_state ?? {}, null, linkTo);
}}
>
{children}
Expand Down
15 changes: 9 additions & 6 deletions packages/icestark-app/src/appHistory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import formatUrl from './util/formatUrl';
import normalizeArgs from './util/normalizeArgs';

const appHistory = {
push: (url: string, hashType?: boolean) => {
push: (url: string, state?: object | boolean, hashType?: boolean) => {
const [_state, _hashType] = normalizeArgs(state, hashType);
window.history.pushState(
{},
_state ?? {},
null,
formatUrl(url, hashType),
formatUrl(url, _hashType),
);
},
replace: (url: string, hashType?: boolean) => {
replace: (url: string, state?: object | boolean, hashType?: boolean) => {
const [_state, _hashType] = normalizeArgs(state, hashType);
window.history.replaceState(
{},
_state ?? {},
null,
formatUrl(url, hashType),
formatUrl(url, _hashType),
);
},
};
Expand Down
16 changes: 16 additions & 0 deletions packages/icestark-app/src/util/normalizeArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// `hashType' was relocated to the third argument.
const isDev = process.env.NODE_ENV === 'development';

const normalizeArgs = (state?: object | boolean, hashType?: boolean): [object, boolean] => {
if (typeof state === 'boolean') {
isDev && console.warn('[icestark]: hashType was relocated to the third argument.');
return [{}, hashType ?? state];
}
if (typeof state === 'object') {
return [state, hashType];
}

return [{}, hashType];
};

export default normalizeArgs;
17 changes: 16 additions & 1 deletion packages/icestark-app/tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../src/index';
import { setCache, getCache } from '../src/cache';
import formatUrl from '../src/util/formatUrl';
import normalizeArgs from '../src/util/normalizeArgs';

const namespace = 'ICESTARK';

Expand Down Expand Up @@ -146,4 +147,18 @@ describe('formatUrl', () => {

expect(formatUrl('/seller', true)).toBe('#/seller');
})
})
});

describe('normalizeArgs', () => {
test('normalizeArgs', () => {
expect(normalizeArgs(true)).toEqual([{}, true]);
expect(normalizeArgs(false, true)).toEqual([{}, true]);

expect(normalizeArgs({ framework: 'icestark' })).toEqual([{ framework: 'icestark' }, undefined]);
expect(normalizeArgs({ framework: 'icestark' }, true)).toEqual([{ framework: 'icestark' }, true]);
expect(normalizeArgs({ framework: 'icestark' }, false)).toEqual([{ framework: 'icestark' }, false]);

expect(normalizeArgs()).toEqual([{}, undefined]);
expect(normalizeArgs(null)).toEqual([null, undefined]);
})
});
1 change: 0 additions & 1 deletion packages/icestark-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@types/jest": "^24.0.12",
"@types/node": "^12.0.0",
"codecov": "^3.4.0",
"eslint": "^5.16.0",
"husky": "^2.2.0",
"jest": "^24.7.1",
"stylelint": "^10.1.0",
Expand Down
61 changes: 52 additions & 9 deletions website/docs/api/ice-stark-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ import isInIcestark from '@ice/stark-app/lib/isInIcestark';

#### appHistory.push

- 类型:`function`
- 代码示例:
- 类型定义:` (url: string, state?: object, hashType?: boolean) => void`

代码示例一:跳转 url 页面

```js
import React from 'react';
Expand All @@ -62,7 +63,7 @@ export default class SelfLink extends React.Component {
return (
<span
onClick={() => {
appHistory.push('/home');
appHistory.push('/home?name=ice');
}}
>
selfLink
Expand All @@ -72,20 +73,33 @@ export default class SelfLink extends React.Component {
}
```

代码示例二:传递 `state`

```js
appHistory.push('/home?name=ice', { framework: 'icestark' });
```

代码示例三:设置为 hash 路由模式

```js
appHistory.push('/home?name=ice', {}, true);
}
```

#### appHistory.replace

- 类型:`function`
- 函数类型定义:- 类型定义:` (url: string, state?: object, hashType?: boolean) => void`
- 代码示例参考 `appHistory.push`

## AppLink

提供声明式的,可访问的导航,表示本次跳转需要重新加载静态资源。微应用内部跳转仍然使用 `Link`
提供声明式的,可访问的导航,表示本次跳转需要重新加载静态资源。微应用内部跳转仍然使用 `Link`

#### to

目标路径,同 `Link` 中的 `to` 保持一致 ,必填

- 类型:`string`
- 类型:`string | object`
- 默认值:`-`

#### replace
Expand All @@ -109,26 +123,55 @@ export default class SelfLink extends React.Component {
- 类型:`boolean`
- 默认值:`false`

代码示例:
代码示例一:`to` 为字符串,传递 query 参数

```js
import React from 'react';
import { Link } from 'react-router-dom';
import { AppLink } from '@ice/stark';

export default class SelfLink extends React.Component {
render() {
return (
<div>
// 应用间路由跳转,并携带 query 查询参数
<AppLink to="/waiter/list?name=ice">使用 AppLink 跳转到小二平台的列表页</AppLink>
<Link to="/detail">跳转到商家平台详情页</Link>
</div>
);
}
}
```

代码示例二:`to` 为简单对象,传递 state

```js
import React from 'react';
import { Link } from 'react-router-dom';
import { AppLink } from '@ice/stark';

export default class SelfLink extends React.Component {
// 商家平台代码
render() {
return (
<div>
<AppLink to="/waiter/list">使用 AppLink 跳转到小二平台的列表页</AppLink>
// 应用间路由跳转,并传递 state、query
<AppLink
to={{
pathname: '/waiter/list',
search: '?name=ice',
state: {
framework: 'icestark'
}
}}
>使用 AppLink 跳转到小二平台的列表页</AppLink>
<Link to="/detail">跳转到商家平台详情页</Link>
</div>
);
}
}
```


## registerAppEnter

提供快速注册当前应用加载前的回调事件
Expand Down

0 comments on commit 2f42a89

Please sign in to comment.