Skip to content

Commit

Permalink
fix: typescript fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkaron committed Jul 4, 2019
1 parent 007c0f0 commit 88b2b58
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 52 deletions.
29 changes: 17 additions & 12 deletions packages/demo/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ import {
RouterState,
Send,
} from 'stencil-xstate-router/dist/types';
import {
DemoContext,
DemoEvent,
DemoSchema,
} from './components/types';

export namespace Components {
interface IsAnonymous {
'send': Send<any, any, any>;
'state': RouterState<any, any>;
'send': Send<DemoContext, DemoSchema, DemoEvent>;
'state': RouterState<DemoContext, DemoEvent>;
}
interface IsAuthenticated {
'send': Send<any, any, any>;
'state': RouterState<any, any>;
'send': Send<DemoContext, DemoSchema, DemoEvent>;
'state': RouterState<DemoContext, DemoEvent>;
}
interface IsTest {
'send': Send<any, any, any>;
'state': RouterState<any, any>;
'send': Send<DemoContext, DemoSchema, DemoEvent>;
'state': RouterState<DemoContext, DemoEvent>;
'testId': string;
}
interface XstateRouterTest {}
Expand Down Expand Up @@ -64,16 +69,16 @@ declare global {

declare namespace LocalJSX {
interface IsAnonymous extends JSXBase.HTMLAttributes<HTMLIsAnonymousElement> {
'send': Send<any, any, any>;
'state': RouterState<any, any>;
'send': Send<DemoContext, DemoSchema, DemoEvent>;
'state': RouterState<DemoContext, DemoEvent>;
}
interface IsAuthenticated extends JSXBase.HTMLAttributes<HTMLIsAuthenticatedElement> {
'send': Send<any, any, any>;
'state': RouterState<any, any>;
'send': Send<DemoContext, DemoSchema, DemoEvent>;
'state': RouterState<DemoContext, DemoEvent>;
}
interface IsTest extends JSXBase.HTMLAttributes<HTMLIsTestElement> {
'send': Send<any, any, any>;
'state': RouterState<any, any>;
'send': Send<DemoContext, DemoSchema, DemoEvent>;
'state': RouterState<DemoContext, DemoEvent>;
'testId'?: string;
}
interface XstateRouterTest extends JSXBase.HTMLAttributes<HTMLXstateRouterTestElement> {}
Expand Down
5 changes: 3 additions & 2 deletions packages/demo/src/components/is-anonymous/is-anonymous.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { h, Component, Prop, ComponentInterface } from '@stencil/core';
import { Send, RouterState } from 'stencil-xstate-router/dist/types';
import { DemoContext, DemoSchema, DemoEvent } from '../types';

@Component({
tag: 'is-anonymous',
shadow: true
})
export class IsAnonymous implements ComponentInterface {
@Prop() send!: Send<any, any, any>;
@Prop() send!: Send<DemoContext, DemoSchema, DemoEvent>;

@Prop() state!: RouterState<any, any>;
@Prop() state!: RouterState<DemoContext, DemoEvent>;

render() {
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { h, Component, Prop, ComponentInterface } from '@stencil/core';
import { Send, RouterState } from 'stencil-xstate-router/dist/types';
import { DemoContext, DemoSchema, DemoEvent } from '../types';

@Component({
tag: 'is-authenticated',
shadow: true
})
export class IsAuthenticated implements ComponentInterface {
@Prop() send!: Send<any, any, any>;
@Prop() send!: Send<DemoContext, DemoSchema, DemoEvent>;

@Prop() state!: RouterState<any, any>;
@Prop() state!: RouterState<DemoContext, DemoEvent>;

componentWillLoad() {
console.log(`will load: ${JSON.stringify(this.state.value)}`);
Expand Down
5 changes: 3 additions & 2 deletions packages/demo/src/components/is-test/is-test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { h, Component, Prop } from '@stencil/core';
import { Send, RouterState } from 'stencil-xstate-router/dist/types';
import { DemoContext, DemoSchema, DemoEvent } from '../types';

@Component({
tag: 'is-test',
shadow: true
})
export class IsTest {
@Prop() send!: Send<any, any, any>;
@Prop() send!: Send<DemoContext, DemoSchema, DemoEvent>;

@Prop() state!: RouterState<any, any>;
@Prop() state!: RouterState<DemoContext, DemoEvent>;

@Prop() testId: string;

Expand Down
35 changes: 35 additions & 0 deletions packages/demo/src/components/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { StateSchema } from "xstate";

export type DemoContext = {
authenticated?: boolean;
params?: { [key: string]: string };
};

export interface DemoSchema extends StateSchema {
states: {
anonymous: {};
authenticated: {
states: {
home: {};
woot: {};
account: {};
test: {
states: {
overview: {};
details: {};
};
};
current: {};
};
};
};
}

export type DemoEvent = { params?: Record<string, any> } & (
| { type: 'LOGIN' }
| { type: 'LOGOUT' }
| { type: 'HOME' }
| { type: 'ACCOUNT' }
| { type: 'TEST' }
| { type: 'DETAILS' }
| { type: 'WOOT' });
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { h, Component, State } from '@stencil/core';
import { Machine, assign } from 'xstate';
import 'stencil-xstate-router';
import { DemoContext, DemoEvent, DemoSchema } from '../types';

type Context = {
authenticated?: boolean;
params?: { [key: string]: string };
};

@Component({
tag: 'xstate-router-test',
shadow: false
})
export class XStateRouterTest {
@State() machine = Machine<Context>(
@State() machine = Machine<DemoContext, DemoSchema, DemoEvent>(
{
id: 'app',
initial: 'anonymous',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { h } from '@stencil/core';
import { EventObject, StateMachine, StateSchema } from 'xstate';
import {
ComponentRenderer,
RouteEvent,
RouterInterpreterOptions,
StateRenderer
} from '../xstate-router/types';
Expand All @@ -14,10 +13,11 @@ export interface RouterNavigoProps<
> {
machine: StateMachine<TContext, TSchema, TEvent>;
options?: RouterInterpreterOptions;
stateRenderer?: StateRenderer<any, any, RouteEvent>;
componentRenderer?: ComponentRenderer<any, any, EventObject>;
stateRenderer?: StateRenderer<TContext, TSchema, TEvent>;
componentRenderer?: ComponentRenderer<TContext, TSchema, TEvent>;
capture?: boolean;
root?: string;
routes?: Record<string, string>;
useHash?: boolean;
hash?: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
InterpreterOptions,
State as RouterState,
StateMachine,
StateSchema,
StateSchema
} from 'xstate';

export { RouterState };
Expand All @@ -15,7 +15,10 @@ export type Send<
TEvent extends EventObject
> = Interpreter<TContext, TSchema, TEvent>['send'];

export type NavigationHandler = (path: string, params?: Record<string, any>) => void;
export type NavigationHandler = (
path: string,
params?: Record<string, any>
) => void;

export type RouteHandler = (routes: Route[]) => VoidFunction;

Expand Down Expand Up @@ -44,8 +47,15 @@ export type ComponentRenderer<
props?: ComponentProps<TContext, TSchema, TEvent>
) => Element[] | Element;

export type RenderEvent = {
type: 'RENDER',
export interface RouterEvent extends EventObject {
/**
* Parameters
*/
params?: Record<string, any>;
}

export type RenderEvent = RouterEvent & {
type: 'RENDER';
/**
* Component to render
*/
Expand All @@ -54,25 +64,14 @@ export type RenderEvent = {
* Component slot
*/
slot?: string;
/**
* Component params
*/
params?: Record<string, any>;
};

export interface RouteEvent extends EventObject {
export type NavigationEvent = RouterEvent & {
type: 'NAVIGATE';
/**
* Path routed to
*/
path: string;
/**
* Route parameters
*/
params?: Record<string, any>;
};

export type NavigationEvent = RouteEvent & {
type: 'NAVIGATE'
};

export interface RouterProps<
Expand All @@ -82,11 +81,12 @@ export interface RouterProps<
> {
machine: StateMachine<TContext, TSchema, TEvent>;
options?: RouterInterpreterOptions;
stateRenderer?: StateRenderer<any, any, RouteEvent>;
componentRenderer?: ComponentRenderer<any, any, EventObject>;
stateRenderer?: StateRenderer<TContext, TSchema, TEvent>;
componentRenderer?: ComponentRenderer<TContext, TSchema, TEvent>;
route?: RouteHandler;
routes?: Record<string, string>;
navigate?: NavigationHandler;
};
}

export interface ComponentProps<
TContext,
Expand All @@ -105,7 +105,7 @@ export interface ComponentProps<
* Current service
*/
service: Interpreter<TContext, TSchema, TEvent>;
};
}

/**
* Router interpreter options
Expand Down
1 change: 1 addition & 0 deletions packages/stencil-xstate-router/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Components } from './components';

export * from './components/xstate-router';
export * from './components/xstate-router-navigo'
6 changes: 1 addition & 5 deletions packages/stencil-xstate-router/stencil.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ export const config: Config = {
type: 'dist',
},
{
'type': 'docs-readme'
},
{
type: 'www',
serviceWorker: null // disable service workers,
type: 'docs-readme'
}
]
};

0 comments on commit 88b2b58

Please sign in to comment.