-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: functional component for xstate-router-navigo
- Loading branch information
1 parent
a699f4a
commit de34af9
Showing
4 changed files
with
85 additions
and
57 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/stencil-xstate-router/src/components/xstate-router-navigo/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TContext, TSchema, TEvent>; | ||
options?: RouterInterpreterOptions; | ||
stateRenderer?: StateRenderer<any, any, RouteEvent>; | ||
componentRenderer?: ComponentRenderer<any, any, EventObject>; | ||
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<TContext, TSchema, TEvent>, | ||
children?: JSX.Element[] | ||
) => JSX.Element = (props, children) => ( | ||
<xstate-router-navigo {...props}>{children}</xstate-router-navigo> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/stencil-xstate-router/src/components/xstate-router/utils.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: <T extends Record<string, any>>( | ||
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<any, any, EventObject> = ( | ||
Component, | ||
props | ||
) => <Component {...props} />; | ||
|
||
/** | ||
* Guards a route | ||
* @param context Context | ||
* @param event Event | ||
* @param meta Meta | ||
*/ | ||
export const routeGuard: RouteConditionPredicate<any, RouteEvent> = ( | ||
_, | ||
event, | ||
{ cond } | ||
) => event.path === cond.path; |
2 changes: 1 addition & 1 deletion
2
packages/stencil-xstate-router/src/components/xstate-router/xstate-router.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters