From de34af96ec1010c9508f906a3a17cab0f69b8d51 Mon Sep 17 00:00:00 2001 From: Mikael Karon Date: Tue, 18 Jun 2019 06:30:54 +0200 Subject: [PATCH] feat: functional component for xstate-router-navigo --- .../components/xstate-router-navigo/index.tsx | 38 ++++++++++++ .../src/components/xstate-router/index.tsx | 60 ++----------------- .../src/components/xstate-router/utils.tsx | 42 +++++++++++++ .../xstate-router/xstate-router.tsx | 2 +- 4 files changed, 85 insertions(+), 57 deletions(-) create mode 100644 packages/stencil-xstate-router/src/components/xstate-router-navigo/index.tsx create mode 100644 packages/stencil-xstate-router/src/components/xstate-router/utils.tsx diff --git a/packages/stencil-xstate-router/src/components/xstate-router-navigo/index.tsx b/packages/stencil-xstate-router/src/components/xstate-router-navigo/index.tsx new file mode 100644 index 0000000..9db5d9b --- /dev/null +++ b/packages/stencil-xstate-router/src/components/xstate-router-navigo/index.tsx @@ -0,0 +1,38 @@ +import { EventObject, StateMachine, StateSchema } from 'xstate'; +import { + ComponentRenderer, + RouteEvent, + RouterInterpreterOptions, + StateRenderer +} from '../xstate-router/types'; + +export type RouterNavigoProps< + TContext, + TSchema extends StateSchema, + TEvent extends EventObject +> = { + machine: StateMachine; + options?: RouterInterpreterOptions; + stateRenderer?: StateRenderer; + componentRenderer?: ComponentRenderer; + capture?: boolean; + root?: string; + useHash?: boolean; + hash?: string; +}; + +/** + * Functional component wrapper around XstateRouterNavigo + * @param props Component props + * @param children Component children + */ +export const RouterNavigo: < + TContext, + TSchema extends StateSchema, + TEvent extends EventObject +>( + props: RouterNavigoProps, + children?: JSX.Element[] +) => JSX.Element = (props, children) => ( + {children} +); diff --git a/packages/stencil-xstate-router/src/components/xstate-router/index.tsx b/packages/stencil-xstate-router/src/components/xstate-router/index.tsx index d1759c5..3c17cee 100644 --- a/packages/stencil-xstate-router/src/components/xstate-router/index.tsx +++ b/packages/stencil-xstate-router/src/components/xstate-router/index.tsx @@ -1,12 +1,8 @@ import { EventObject, StateSchema } from 'xstate'; -import { - ComponentRenderer, - RouteConditionPredicate, - RouteEvent, - RouterProps -} from './types'; +import { RouterProps } from './types'; export * from './types'; +export * from './utils'; /** * Functional component wrapper for XstateRouter @@ -20,56 +16,8 @@ export const Router: < >( props: RouterProps, children?: JSX.Element[] -) => JSX.Element = ( - { machine, options, route, navigate, stateRenderer, componentRenderer }, - children -) => ( - - {children} - +) => JSX.Element = (props, children) => ( + {children} ); -/** - * Merges meta objects - * @param source XState.State meta object - * @param target Target object - */ -export const mergeMeta: >( - source: T, - target?: T | {} -) => T = (source, target = {}) => - Object.keys(source).reduce( - (result, key) => Object.assign(result, source[key]), - target - ); - -/** - * Renders a component - * @param Component Component tag - * @param props Component props - */ -export const renderComponent: ComponentRenderer = ( - Component, - props -) => ; - -/** - * Guards a route - * @param context Context - * @param event Event - * @param meta Meta - */ -export const routeGuard: RouteConditionPredicate = ( - _, - event, - { cond } -) => event.path === cond.path; - export default Router; diff --git a/packages/stencil-xstate-router/src/components/xstate-router/utils.tsx b/packages/stencil-xstate-router/src/components/xstate-router/utils.tsx new file mode 100644 index 0000000..605fb0b --- /dev/null +++ b/packages/stencil-xstate-router/src/components/xstate-router/utils.tsx @@ -0,0 +1,42 @@ +import { EventObject } from 'xstate'; +import { + ComponentRenderer, + RouteConditionPredicate, + RouteEvent +} from './types'; + +/** + * Merges meta objects + * @param source XState.State meta object + * @param target Target object + */ +export const mergeMeta: >( + source: T, + target?: T | {} +) => T = (source, target = {}) => + Object.keys(source).reduce( + (result, key) => Object.assign(result, source[key]), + target + ); + +/** + * Renders a component + * @param Component Component tag + * @param props Component props + */ +export const renderComponent: ComponentRenderer = ( + Component, + props +) => ; + +/** + * Guards a route + * @param context Context + * @param event Event + * @param meta Meta + */ +export const routeGuard: RouteConditionPredicate = ( + _, + event, + { cond } +) => event.path === cond.path; diff --git a/packages/stencil-xstate-router/src/components/xstate-router/xstate-router.tsx b/packages/stencil-xstate-router/src/components/xstate-router/xstate-router.tsx index a8bd83b..82420ae 100644 --- a/packages/stencil-xstate-router/src/components/xstate-router/xstate-router.tsx +++ b/packages/stencil-xstate-router/src/components/xstate-router/xstate-router.tsx @@ -1,6 +1,6 @@ import { Component, ComponentInterface, Prop, State } from '@stencil/core'; import { EventObject, interpret, Interpreter, StateMachine } from 'xstate'; -import { mergeMeta, renderComponent, routeGuard } from '.'; +import { mergeMeta, renderComponent, routeGuard } from './utils'; import { ComponentRenderer, NavigationEvent,