DefinitelyTyped/types/react-bootstrap-table2-toolkit/react-bootstrap-table2-toolkit-tests.tsx
Wlad Meixner dec598b7cf
[react-bootstrap-table-next] Add typing for react-bootstrap-table-next (#43155)
* react-bootstrap-table-next: add initial typings

* [name react-bootstrap-table2] add modules

* [react-bootstrap-table-next] add filter example

* [react-bootstrap-table2-filter] add filter example

* [react-bootstrap-table-next] update types

- loosen table typing after testing with real life projects
- remove enums in favour of constant declarations and aggregation types

* [react-bootstrap-table-next] fix cell align type

* [react-bootstrap-table-next] update tests

* [react-bootstrap-table2-toolkit] narrow types

* [react-bootstrap-table-next] fix test column

* [react-bootstrap-table2-filter] update comments

* [react-bootstrap-table-next] fix documentation

* [react-bootstrap-table2-filter] fix typo in header

* [react-bootstrap-table-next] fix issues

* [react-bootstrap-table-next] add definitions

- expand toolkit definitions
- add standalone pagination component types

* [react-bootstrap-table-next] loosen column type

* update typescript version

* fix: typescript version mixup

* fix: remove optional chaining, change TS version
2020-04-06 09:42:41 -07:00

105 lines
2.6 KiB
TypeScript

import * as React from 'react';
import BootstrapTable, {
CellAlignment,
ColumnDescription,
HeaderFormatter,
ColumnFormatter,
} from 'react-bootstrap-table-next';
import paginationFactory from 'react-bootstrap-table2-paginator';
import { render } from 'react-dom';
import ToolkitProvider, { InjectedSearchProps } from 'react-bootstrap-table2-toolkit';
interface Product {
id: number;
name: string;
price?: number;
quality?: number;
inStockStatus?: number;
sales?: number;
}
const products: Product[] = [
{
id: 1,
name: 'Item name 1',
price: 100,
},
{
id: 2,
name: 'Item name 2',
price: 100,
},
];
const priceHeaderFormatter: HeaderFormatter<Product> = (column, colIndex, components) => {
return (
<div>
{column.text}
{components.sortElement}
{components.filterElement}
</div>
);
};
const priceFormatter: ColumnFormatter<Product, { indexSquare: number }> = (cell, row, rowIndex) => {
return (
<span>
{rowIndex} - {cell}
</span>
);
};
const productColumns: Array<ColumnDescription<Product>> = [
{ dataField: 'id', align: 'center', sort: true, text: 'Product ID' },
{ dataField: 'name', align: 'center', sort: true, text: 'Product Name' },
{
isDummyField: true,
dataField: '',
sort: true,
text: 'Product Name',
},
{
dataField: 'price',
sort: true,
formatter: priceFormatter,
text: 'Product Price',
headerFormatter: priceHeaderFormatter,
},
/**
* test optional dataField for dummyFields
*/
{
isDummyField: true,
dataField: '',
sort: true,
formatter: priceFormatter,
text: 'Product Price',
headerFormatter: priceHeaderFormatter,
},
];
/**
* Toolkit with custom search test test
*/
const CustomSearch = (props: InjectedSearchProps) => {
return (
<span>
<input value={props.searchText} onChange={e => props.onSearch(e.currentTarget.value)} />
<button onClick={props.onClear}>Clear Search</button>
</span>
);
};
render(
<ToolkitProvider data={products} keyField="id" columns={productColumns}>
{({ baseProps, searchProps }) => (
<>
<CustomSearch {...searchProps} />
<BootstrapTable {...baseProps} pagination={paginationFactory({ sizePerPage: 10, page: 1 })} />
</>
)}
</ToolkitProvider>,
document.getElementById('app'),
);