react-ui-basics 0.0.236 → 0.0.238

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-ui-basics",
3
- "version": "0.0.236",
3
+ "version": "0.0.238",
4
4
  "description": "",
5
5
  "module": "index.js",
6
6
  "_main": "index.cjs.js",
@@ -1,2 +1,9 @@
1
- export declare const pushLocation: (path: string) => any;
2
- export declare const replaceLocation: (path: string) => any;
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 {};
@@ -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
- original.apply(history, arguments);
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
  });
@@ -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;AAE/B,MAAM,MAAM,GAAG,CAAC,WAAW,EAAE,cAAc,CAAC,CAAC;AAC7C,MAAM,CAAC,OAAO,CAAC,CAAC,IAAG;AACf,IAAA,IAAI,QAAQ,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1B,OAAO,CAAC,CAAC,CAAC,GAAG,YAAA;AACT,QAAA,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QACnC,SAAS,CAAC,CAAC,CAAC,CAAC;AACjB,KAAC,CAAA;AACL,CAAC,CAAC,CAAC;AAEH,MAAM,SAAS,GAAG,CAAC,IAAI,KAAI;AACvB,IAAA,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;AACnE,IAAA,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC,CAAC;AAEK,MAAM,YAAY,GAAG,CAAC,IAAY,KAAK,IAAI,KAAK,MAAM,CAAC,QAAQ,CAAC,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;MAE3G,eAAe,GAAG,CAAC,IAAY,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;AAEtF,IAAI,MAAM,CAAC,4BAA4B,CAAC,EAAE;;;IAGtC,MAAM,CAAC,eAAe,CAAC,GAAG;QACtB,eAAe;KAClB,CAAA;AACJ;;;;"}
1
+ {"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,EAAE,IAAI,EAAE,IAAI,EAAE;AAUtF,IAAI,MAAM,CAAC,4BAA4B,CAAC,EAAE;;;IAGtC,MAAM,CAAC,eAAe,CAAC,GAAG;QACtB,eAAe;KAClB,CAAA;AACJ;;;;"}
@@ -147,7 +147,7 @@ class AutocompleteSelect extends PureComponent {
147
147
  this.initSelected(__props(this), __state(this))
148
148
  if (__state(this).isActive && !prevState.isActive)
149
149
  this.updateListPosition()
150
- if (__state(this).isActive !== !prevState.isActive)
150
+ if (__state(this).isActive !== prevState.isActive)
151
151
  orNoop(__props(this).onIsActiveChange)(__state(this).isActive)
152
152
  }
153
153
 
@@ -2,11 +2,22 @@ import {WINDOW} from "../Tools";
2
2
 
3
3
  const history = WINDOW.history;
4
4
 
5
- const events = ['pushState', 'replaceState'];
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
- original.apply(history, arguments);
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,6 +31,14 @@ 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
@@ -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
  });