Skip to content

Commit

Permalink
feat: functional component wrapper for xstate-route
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkaron committed Jun 18, 2019
1 parent 35c4dc2 commit a699f4a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
tag: 'xstate-router-navigo',
shadow: false
})
export class XStateRouterNavigo implements ComponentInterface {
export class XstateRouterNavigo implements ComponentInterface {
@State() router: Navigo;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import { EventObject } from 'xstate';
import { EventObject, StateSchema } from 'xstate';
import {
ComponentRenderer,
RouteConditionPredicate,
RouteEvent
RouteEvent,
RouterProps
} from './types';

export * from './types';

/**
* Functional component wrapper for XstateRouter
* @param props Component props
* @param children Component children
*/
export const Router: <
TContext,
TSchema extends StateSchema,
TEvent extends EventObject
>(
props: RouterProps<TContext, TSchema, TEvent>,
children?: JSX.Element[]
) => JSX.Element = (
{ machine, options, route, navigate, stateRenderer, componentRenderer },
children
) => (
<xstate-router
machine={machine}
options={options}
route={route}
navigate={navigate}
stateRenderer={stateRenderer}
componentRenderer={componentRenderer}
>
{children}
</xstate-router>
);

/**
* Merges meta objects
* @param source XState.State meta object
Expand All @@ -27,7 +58,7 @@ export const mergeMeta: <T extends Record<string, any>>(
export const renderComponent: ComponentRenderer<any, any, EventObject> = (
Component,
props
) => ({ tagName: Component, ...props });
) => <Component {...props} />;

/**
* Guards a route
Expand All @@ -40,3 +71,5 @@ export const routeGuard: RouteConditionPredicate<any, RouteEvent> = (
event,
{ cond }
) => event.path === cond.path;

export default Router;
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
EventObject,
GuardPredicate,
Interpreter,
InterpreterOptions,
OmniEventObject,
State as RouterState,
StateMachine,
StateMeta,
StateSchema,
TransitionConfig,
InterpreterOptions,
TransitionConfig
} from 'xstate';

export { RouterState };
Expand Down Expand Up @@ -83,6 +84,19 @@ export type NavigationEvent = EventObject & {
url?: string;
};

export type RouterProps<
TContext,
TSchema extends StateSchema,
TEvent extends EventObject
> = {
machine: StateMachine<TContext, TSchema, TEvent>;
options?: RouterInterpreterOptions;
stateRenderer?: StateRenderer<any, any, RouteEvent>;
componentRenderer?: ComponentRenderer<any, any, EventObject>;
route?: RouteHandler<any, any, RouteEvent>;
navigate?: NavigationHandler;
};

export type ComponentProps<
TContext,
TSchema extends StateSchema,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, ComponentInterface, Prop, State } from '@stencil/core';
import { EventObject, interpret, Interpreter, StateMachine } from 'xstate';
import { mergeMeta, renderComponent, routeGuard } from './utils';
import { mergeMeta, renderComponent, routeGuard } from '.';
import {
ComponentRenderer,
NavigationEvent,
Expand All @@ -17,7 +17,7 @@ import {
tag: 'xstate-router',
shadow: false
})
export class XStateRouter implements ComponentInterface {
export class XstateRouter implements ComponentInterface {
@State() private subscriptions: Set<VoidFunction> = new Set();
@State() private service: Interpreter<any, any, EventObject>;
@State() private rendered: {
Expand Down
3 changes: 1 addition & 2 deletions packages/stencil-xstate-router/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { Components } from './components';

export * from './components/xstate-router/utils';
export * from './components/xstate-router/types';
export * from './components/xstate-router';

0 comments on commit a699f4a

Please sign in to comment.