react-ui-basics 0.0.237 → 0.0.239
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/bundle.css +634 -634
- package/package.json +1 -1
- package/router/HistoryTools.d.ts +9 -2
- package/router/HistoryTools.js +19 -3
- package/router/HistoryTools.js.map +1 -1
- package/src/router/HistoryTools.ts +24 -3
- package/src/router/Route.test.js +25 -1
package/package.json
CHANGED
package/router/HistoryTools.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
declare const history: History;
|
|
2
|
+
declare type HistoryAction = 'pushState' | 'replaceState';
|
|
3
|
+
declare type HistoryActionArgs = Parameters<typeof history[HistoryAction]>;
|
|
4
|
+
declare type HistoryMiddleware = (action: HistoryAction, args: HistoryActionArgs) => HistoryActionArgs;
|
|
5
|
+
export declare const pushLocation: (path: string) => void;
|
|
6
|
+
export declare const replaceLocation: (path: string) => void;
|
|
7
|
+
export declare const addMiddleware: (middleware: HistoryMiddleware) => void;
|
|
8
|
+
export declare const removeMiddleware: (middleware: HistoryMiddleware) => void;
|
|
9
|
+
export {};
|
package/router/HistoryTools.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import ReactConstants from "./../ReactConstants"; import {clientHeight as __clientHeight, clientWidth as __clientWidth, createAccessor as __createAccessor, data as __data, height as __height, left as __left, length as __length, pageX as __pageX, pageY as __pageY, props as __props, scrollLeft as __scrollLeft, scrollTop as __scrollTop, state as __state, style as __style, target as __target, top as __top, value as __value, width as __width} from "./../CommonFields"; import {toConsumableArray as __toConsumableArray} from "./../_virtual/_rollupPluginBabelHelpers"; import {extends as __extends} from "./../_virtual/_rollupPluginBabelHelpers"; import {inherits as __inherits} from "./../_virtual/_rollupPluginBabelHelpers"; import { WINDOW } from '../Tools.js';
|
|
2
2
|
|
|
3
3
|
const history = WINDOW.history;
|
|
4
|
+
const middlewares = [];
|
|
4
5
|
const events = ['pushState', 'replaceState'];
|
|
5
6
|
events.forEach(e => {
|
|
6
7
|
var original = history[e];
|
|
7
8
|
history[e] = function () {
|
|
8
|
-
|
|
9
|
+
let args = arguments;
|
|
10
|
+
for (const middleware of middlewares) {
|
|
11
|
+
args = middleware(e, args);
|
|
12
|
+
if (!args)
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
args && original.apply(history, args);
|
|
9
16
|
fireEvent(e);
|
|
10
17
|
};
|
|
11
18
|
});
|
|
@@ -15,13 +22,22 @@ const fireEvent = (name) => {
|
|
|
15
22
|
};
|
|
16
23
|
const pushLocation = (path) => path !== WINDOW.location.pathname && history[events[0]](null, null, path);
|
|
17
24
|
const replaceLocation = (path) => history[events[1]](null, null, path);
|
|
25
|
+
const addMiddleware = (middleware) => {
|
|
26
|
+
middlewares.push(middleware);
|
|
27
|
+
};
|
|
28
|
+
const removeMiddleware = (middleware) => {
|
|
29
|
+
let index = middlewares.indexOf(middleware);
|
|
30
|
+
index !== -1 && middlewares.splice(index, 1);
|
|
31
|
+
};
|
|
18
32
|
if (process.env.NODE_ENV !== 'production') {
|
|
19
33
|
// will be removed in production build
|
|
20
34
|
// needed only to prevent bundler to remove 'unused' functions
|
|
21
35
|
window['history-tools'] = {
|
|
22
|
-
replaceLocation
|
|
36
|
+
replaceLocation,
|
|
37
|
+
addMiddleware,
|
|
38
|
+
removeMiddleware,
|
|
23
39
|
};
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
export { pushLocation, replaceLocation };
|
|
42
|
+
export { addMiddleware, pushLocation, removeMiddleware, replaceLocation };
|
|
27
43
|
//# sourceMappingURL=HistoryTools.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HistoryTools.js","sources":["../src/router/HistoryTools.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"HistoryTools.js","sources":["../src/router/HistoryTools.ts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A,aAAa,GAAG,CAAC,UAA6B,KAAI;AAC3D,IAAA,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;AACjC,EAAE;AACW,MAAA,gBAAgB,GAAG,CAAC,UAA6B,KAAI;IAC9D,IAAI,KAAK,GAAG,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AAC5C,IAAA,KAAK,KAAK,CAAC,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;AACjD,EAAE;AAEF,IAAI,MAAM,CAAC,4BAA4B,CAAC,EAAE;;;IAGtC,MAAM,CAAC,eAAe,CAAC,GAAG;QACtB,eAAe;QACf,aAAa;QACb,gBAAgB;KACnB,CAAA;AACJ;;;;"}
|
|
@@ -2,11 +2,22 @@ import {WINDOW} from "../Tools";
|
|
|
2
2
|
|
|
3
3
|
const history = WINDOW.history;
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
type HistoryAction = 'pushState' | 'replaceState';
|
|
6
|
+
type HistoryActionArgs = Parameters<typeof history[HistoryAction]>;
|
|
7
|
+
type HistoryMiddleware = (action: HistoryAction, args: HistoryActionArgs) => HistoryActionArgs;
|
|
8
|
+
|
|
9
|
+
const middlewares: HistoryMiddleware[] = []
|
|
10
|
+
|
|
11
|
+
const events: HistoryAction[] = ['pushState', 'replaceState'];
|
|
6
12
|
events.forEach(e => {
|
|
7
13
|
var original = history[e];
|
|
8
14
|
history[e] = function () {
|
|
9
|
-
|
|
15
|
+
let args = arguments as any as HistoryActionArgs;
|
|
16
|
+
for (const middleware of middlewares) {
|
|
17
|
+
args = middleware(e, args)
|
|
18
|
+
if (!args) break
|
|
19
|
+
}
|
|
20
|
+
args && original.apply(history, args);
|
|
10
21
|
fireEvent(e);
|
|
11
22
|
}
|
|
12
23
|
});
|
|
@@ -20,10 +31,20 @@ export const pushLocation = (path: string) => path !== WINDOW.location.pathname
|
|
|
20
31
|
|
|
21
32
|
export const replaceLocation = (path: string) => history[events[1]](null, null, path);
|
|
22
33
|
|
|
34
|
+
export const addMiddleware = (middleware: HistoryMiddleware) => {
|
|
35
|
+
middlewares.push(middleware);
|
|
36
|
+
};
|
|
37
|
+
export const removeMiddleware = (middleware: HistoryMiddleware) => {
|
|
38
|
+
let index = middlewares.indexOf(middleware);
|
|
39
|
+
index !== -1 && middlewares.splice(index, 1);
|
|
40
|
+
};
|
|
41
|
+
|
|
23
42
|
if (window['isNotProductionEnvironment']) {
|
|
24
43
|
// will be removed in production build
|
|
25
44
|
// needed only to prevent bundler to remove 'unused' functions
|
|
26
45
|
window['history-tools'] = {
|
|
27
|
-
replaceLocation
|
|
46
|
+
replaceLocation,
|
|
47
|
+
addMiddleware,
|
|
48
|
+
removeMiddleware,
|
|
28
49
|
}
|
|
29
50
|
}
|
package/src/router/Route.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ReactConstants from "./ReactConstants"; import {clientHeight as __clientHeight, clientWidth as __clientWidth, createAccessor as __createAccessor, data as __data, height as __height, left as __left, length as __length, pageX as __pageX, pageY as __pageY, props as __props, scrollLeft as __scrollLeft, scrollTop as __scrollTop, state as __state, style as __style, target as __target, top as __top, value as __value, width as __width} from "./CommonFields"; import {toConsumableArray as __toConsumableArray} from "./_virtual/_rollupPluginBabelHelpers"; import {extends as __extends} from "./_virtual/_rollupPluginBabelHelpers"; import {inherits as __inherits} from "./_virtual/_rollupPluginBabelHelpers"; import React from 'react';
|
|
2
2
|
import Route from "./Route";
|
|
3
3
|
import {mount} from 'enzyme';
|
|
4
|
-
import {pushLocation} from './HistoryTools'
|
|
4
|
+
import {pushLocation, addMiddleware, removeMiddleware} from './HistoryTools'
|
|
5
5
|
|
|
6
6
|
it('renders exact path', () => {
|
|
7
7
|
pushLocation('/test');
|
|
@@ -131,4 +131,28 @@ it('do not render if regex does not match ', () => {
|
|
|
131
131
|
<WithVariable/>
|
|
132
132
|
</Route>
|
|
133
133
|
</div>)).toHaveHTML('<div></div>');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('middleware test', () => {
|
|
137
|
+
let middleware = (action, args) => {
|
|
138
|
+
expect(action).toBe('pushState');
|
|
139
|
+
expect(__length(args)).toBe(3);
|
|
140
|
+
expect(args[0]).toBe(null);
|
|
141
|
+
expect(args[1]).toBe(null);
|
|
142
|
+
expect(args[2]).toBe('/to-rewrite');
|
|
143
|
+
return [null, null, '/']
|
|
144
|
+
};
|
|
145
|
+
addMiddleware(middleware)
|
|
146
|
+
pushLocation('/to-rewrite');
|
|
147
|
+
removeMiddleware(middleware)
|
|
148
|
+
|
|
149
|
+
expect(mount(<Route path="/">
|
|
150
|
+
<div>test</div>
|
|
151
|
+
</Route>)).toContainReact(<div>test</div>);
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
pushLocation('/to-rewrite');
|
|
155
|
+
expect(mount(<Route path="/to-rewrite">
|
|
156
|
+
<div>to-rewrite</div>
|
|
157
|
+
</Route>)).toContainReact(<div>to-rewrite</div>);
|
|
134
158
|
});
|