Skip to content

Commit

Permalink
feat(Table): Implement 'Table' composed component
Browse files Browse the repository at this point in the history
Composes StateParamsControlledTable and ResourceTable into a single component
  • Loading branch information
wms committed Sep 19, 2017
1 parent e7fd0d4 commit d48039c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/components/Table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ResourceTable, Props as ResourceTableProps } from './ResourceTable';
import { stateParamsControlledTable, Props as SPCTProps } from '../hoc/stateParamsControlledTable';

export const Table: React.ComponentClass<SPCTProps> = stateParamsControlledTable<ResourceTableProps>(ResourceTable);
16 changes: 6 additions & 10 deletions src/hoc/stateParamsControlledTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ORDER_TO_SORT = {
desc: 'descend'
};

export interface Props<T> extends TableProps<T> {
export interface Props {
defaultPageSize: number;
}

Expand Down Expand Up @@ -49,10 +49,6 @@ export interface Context {
router: UIRouterReact
}

export type StateParamsControlledTable =
<T>(Component: React.ComponentType<TableProps<T>>) =>
React.ComponentClass<Props<T>>;

export const filtersToWhereParam = (columns: ColumnProps<any>[] | undefined, filters: { [key: string]: string | string[] }) => {
if (!columns) {
return;
Expand Down Expand Up @@ -86,9 +82,9 @@ export const sorterToOrderParam = (sorter: Sorter) => {
}
}

export const stateParamsControlledTable: StateParamsControlledTable =
(Component) => {
class StateParamsControlledTable extends React.Component<Props<any> & StateParamsProps> {
export const stateParamsControlledTable =
<P extends TableProps<any>>(Component: React.ComponentType<P>): React.ComponentClass<P & Props> => {
class StateParamsControlledTable extends React.Component<TableProps<any> & Props & StateParamsProps> {
static contextTypes = {
router: PropTypes.object
}
Expand Down Expand Up @@ -124,7 +120,7 @@ export const stateParamsControlledTable: StateParamsControlledTable =
);
}

componentWillReceiveProps(nextProps: Props<any> & StateParamsProps) {
componentWillReceiveProps(nextProps: Props & StateParamsProps) {
if (!nextProps.stateParams || !nextProps.stateParams.page) {
return;
}
Expand Down Expand Up @@ -216,7 +212,7 @@ export const stateParamsControlledTable: StateParamsControlledTable =
}

return stateParamsObserver(
StateParamsControlledTable,
StateParamsControlledTable as any,
(stateParams, props: any) => ({
...props,
stateParams
Expand Down
24 changes: 22 additions & 2 deletions stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import { UIRouter, UISref, Transition } from '@uirouter/react';
import { Resource } from '@optics/hal-client';
import { Input, Icon, Table, LocaleProvider, Card, Button } from 'antd';
import { Input, Icon, Table as AntTable, LocaleProvider, Card, Button } from 'antd';
import { TableProps } from 'antd/lib/table/Table';
import enUS from 'antd/lib/locale-provider/en_US';

import { buildRouter } from '../src/ui-router';
import { CredentialStore } from '../src/CredentialStore';
import { Login } from '../src/containers/Login';
import { SchemaField } from '../src/components/SchemaField';
import { ResourceTable } from '../src/components/ResourceTable';
import { Table } from '../src/components/Table';
import { stateParamsObserver } from '../src/hoc/stateParamsObserver';
import { stateParamsControlledTable } from '../src/hoc/stateParamsControlledTable';

Expand Down Expand Up @@ -146,6 +148,10 @@ storiesOf('Login', module)
});

const router = buildRouter();
router.transitionService.onStart({}, transition => {
action('transitionService.onStart')(transition.params('to'))
});

router.stateRegistry.register({
name: 'main',
url: '/iframe.html',
Expand Down Expand Up @@ -178,7 +184,7 @@ const StateObservingStateParams = stateParamsObserver(DisplayStateParams);

const StateObservingPaginator = stateParamsObserver(Paginator);

const StateParamsControlledTable = stateParamsControlledTable(Table as any);
const StateParamsControlledTable = stateParamsControlledTable(AntTable as any as React.ComponentClass<TableProps<any>>);

const COLUMNS = [{
title: 'Forename',
Expand Down Expand Up @@ -260,3 +266,17 @@ storiesOf('ResourceTable', module)
/>
</LocaleProvider>
));

storiesOf('Table', module)
.add('Default', () => (
<UIRouter router={router}>
<LocaleProvider locale={enUS}>
<Table
defaultPageSize={15}
resource={mockApiResource}
rel="customers"
columns={COLUMNS}
/>
</LocaleProvider>
</UIRouter>
));

0 comments on commit d48039c

Please sign in to comment.