brew-js-react 0.1.1 → 0.1.2

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.
@@ -1,27 +1,33 @@
1
1
  import { definePrototype } from "../include/zeta-dom/util.js";
2
2
  import { setClass } from "../include/zeta-dom/domUtil.js";
3
3
  import dom from "../include/zeta-dom/dom.js";
4
- import ClassNameMixin from "./ClassNameMixin.js";
4
+ import StatefulMixin from "./StatefulMixin.js";
5
5
 
6
- const LoadingStateMixinSuper = ClassNameMixin.prototype;
6
+ const LoadingStateMixinSuper = StatefulMixin.prototype;
7
7
 
8
8
  export default function LoadingStateMixin() {
9
- ClassNameMixin.call(this, ['loading']);
9
+ StatefulMixin.call(this);
10
10
  }
11
11
 
12
- definePrototype(LoadingStateMixin, ClassNameMixin, {
12
+ definePrototype(LoadingStateMixin, StatefulMixin, {
13
13
  initElement: function (element, state) {
14
14
  LoadingStateMixinSuper.initElement.call(this, element, state);
15
15
  dom.on(element, {
16
16
  asyncStart: function () {
17
+ state.loading = true;
17
18
  setClass(element, 'loading', true);
18
19
  },
19
20
  asyncEnd: function () {
21
+ state.loading = false;
20
22
  setClass(element, 'loading', false);
21
23
  },
22
24
  cancelled: function () {
25
+ state.loading = false;
23
26
  setClass(element, 'loading', false);
24
27
  }
25
28
  });
29
+ },
30
+ getClassNames: function () {
31
+ return [{ loading: !!this.state.loading }];
26
32
  }
27
33
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brew-js-react",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/view.d.ts CHANGED
@@ -9,6 +9,13 @@ export type ViewComponent<P> = React.FC<P>;
9
9
  */
10
10
  export function registerView<P>(factory: () => Promise<{ default: React.ComponentType<P> }>, params: Zeta.Dictionary<string>): ViewComponent<P>;
11
11
 
12
+ /**
13
+ * Determines whether a registered view component matches current route state.
14
+ * However it does not imply if the view is in fact being rendered.
15
+ * @param view A view component returned from {@link registerView}.
16
+ */
17
+ export function isViewMatched(view: ViewComponent<any>): boolean;
18
+
12
19
  /**
13
20
  * Renders view by matching current route state against registered route parameters of each supplied views.
14
21
  * @param args A list of view components created by {@link registerView}.
@@ -31,3 +38,19 @@ export function renderView(props: ViewComponentRootProps, ...args: ViewComponent
31
38
  * @param params Extra route parameters that supplements or overrides current route parameters.
32
39
  */
33
40
  export function linkTo(view: ViewComponent<any>, params?: Zeta.Dictionary<string>): string;
41
+
42
+ /**
43
+ * Navigates to path that will render the specified view.
44
+ * @param view A view component created by {@link registerView}.
45
+ * @param params Extra route parameters that supplements or overrides current route parameters.
46
+ * @see {@link linkTo}.
47
+ */
48
+ export function navigateTo(view: ViewComponent<any>, params?: Zeta.Dictionary<string>): Promise<Brew.NavigateResult>;
49
+
50
+ /**
51
+ * Navigates to path that will render the specified view, replacing current state in browser history.
52
+ * @param view A view component created by {@link registerView}.
53
+ * @param params Extra route parameters that supplements or overrides current route parameters.
54
+ * @see {@link linkTo}.
55
+ */
56
+ export function redirectTo(view: ViewComponent<any>, params?: Zeta.Dictionary<string>): Promise<Brew.NavigateResult>;
package/view.js CHANGED
@@ -53,14 +53,15 @@ definePrototype(ViewContainer, React.Component, {
53
53
  },
54
54
  getViewComponent: function () {
55
55
  var views = this.props.views;
56
- var V = any(views, function (V) {
57
- var params = routeMap.get(V);
58
- return params && equal(params, pick(app.route, keys(params)));
59
- });
60
- return V || void app.navigate(linkTo(views[0]), true);
56
+ return any(views, isViewMatched) || void redirectTo(views[0]);
61
57
  }
62
58
  });
63
59
 
60
+ export function isViewMatched(view) {
61
+ var params = routeMap.get(view);
62
+ return !!params && equal(params, pick(app.route, keys(params)));
63
+ }
64
+
64
65
  export function registerView(factory, routeParams) {
65
66
  var Component = function (props) {
66
67
  var childProps = exclude(props, ['rootProps', 'onComponentLoaded']);
@@ -90,3 +91,11 @@ export function renderView() {
90
91
  export function linkTo(view, params) {
91
92
  return app.route.getPath(extend({}, app.route, params, routeMap.get(view)));
92
93
  }
94
+
95
+ export function navigateTo(view, params) {
96
+ return app.navigate(linkTo(view, params));
97
+ }
98
+
99
+ export function redirectTo(view, params) {
100
+ return app.navigate(linkTo(view, params), true);
101
+ }