Skip to content

Commit

Permalink
feat(ResourceTable): Implement customTitle function prop
Browse files Browse the repository at this point in the history
Allow `ResourceTable` to accept a `customTitle` prop, which functions in the same way as the `title`
prop on the Ant Design Table component, except that the currently fetched resource is also supplied
as a second parameter. Allows table titles to be constructed using meta-properties on the top-level
resource, for example.
  • Loading branch information
wms committed Oct 3, 2017
1 parent cd70d73 commit 7581950
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/components/ResourceTable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,52 @@ describe('when `name` is specified', () => {
expect(fetch).toHaveBeenCalledWith({ rel: 'widgets', name: 'collection' }, {});
});
});

describe('when `customTitle` prop is specified', () => {
let root;
let titleFn;
let widgets;
let result;

beforeEach(() => {
widgets = [{
properties: {
name: 'Widget #1',
}
}, {
properties: {
name: 'Widget #2'
}
}];

result = {
hasEmbedded: () => true,
embedded: jest.fn((name: string) => widgets),
properties: {
count: widgets.length
}
};

titleFn = jest.fn();

root = mount((
<ResourceTable
resource={api}
rel="widgets"
name="collection"
customTitle={titleFn}
/>
));
});

it('is called with current page data and parent resource', async () => {
const promise = root.instance().fetch();
_deferred.resolve(result);
await promise;

const tableTitleFn = root.find('Table').prop('title');
tableTitleFn(widgets);

expect(titleFn).toHaveBeenCalledWith(widgets, result);
});
});
15 changes: 15 additions & 0 deletions src/components/ResourceTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export interface Props extends TableProps<any> {
name?: string;
embedded?: string;
params?: {};
customTitle?: (currentPageData: Resource[], resource?: Resource) => React.ReactNode;
}

export interface State {
loading: boolean;
total: number;
resource?: Resource;
rows: Resource[];
params: {};
}
Expand Down Expand Up @@ -96,6 +98,7 @@ export class ResourceTable extends React.PureComponent<Props, State> {
rel,
name,
embedded,
customTitle,
...tableProps
} = this.props;

Expand All @@ -105,6 +108,7 @@ export class ResourceTable extends React.PureComponent<Props, State> {
<Table
rowKey={this._rowKey}
{...tableProps}
title={customTitle ? this._customTitle : undefined}
pagination={this.pagination()}
loading={loading}
dataSource={rows}
Expand Down Expand Up @@ -166,6 +170,7 @@ export class ResourceTable extends React.PureComponent<Props, State> {
[];

this.setState({
resource: result,
total: result.properties.count,
rows
})
Expand Down Expand Up @@ -218,4 +223,14 @@ export class ResourceTable extends React.PureComponent<Props, State> {

return 'unknown';
}

protected _customTitle = (currentPageData: object[]): React.ReactNode => {
const { customTitle } = this.props;

if (!customTitle) {
return;
}

return customTitle(currentPageData as Resource[], this.state.resource);
}
}

0 comments on commit 7581950

Please sign in to comment.