Handling Date Objects
InfiniteTable can handle dates just like any other data type - make sure you specify type="date" for date columns.
If your date column does not specify a custom formatter or renderer, by default the date will be formatted using the toLocaleDateString
method of the date object.
Note
For date columns, make sure you specify column.type="date"
.
This will ensure that the column is sorted correctly (as per the available sortTypes
) and that the default date formatting is applied.
In this example, the birthDate
column contains dates and we customized the way they are displayed.
const renderValue = ({ value }: { value: Date }) => {
return <b>{value.toISOString().split('T')[0]}</b>;
};
If no custom renderValue
was specified, the dates would have been formatted using the Date.toLocaleDateString()
import '@infinite-table/infinite-react/index.css'; import { InfiniteTable, DataSource, type InfiniteTablePropColumns, } from '@infinite-table/infinite-react'; import * as React from 'react'; type Developer = { birthDate: Date; id: number; firstName: string; country: string; city: string; currency: string; email: string; preferredLanguage: string; hobby: string; salary: number; }; const columns: InfiniteTablePropColumns<Developer> = { firstName: { field: 'firstName', header: 'First Name' }, birthDate: { field: 'birthDate', header: 'Birth Date', // we need to specify the type of the column as "date" type: 'date', renderValue: ({ value }: { value: Date }) => { return <b>{value.toISOString().split('T')[0]}</b>; }, }, salary: { field: 'salary', header: 'Salary', type: 'number', }, country: { field: 'country', header: 'Country' }, preferredLanguage: { field: 'preferredLanguage' }, id: { field: 'id' }, hobby: { field: 'hobby' }, city: { field: 'city' }, currency: { field: 'currency' }, }; export default function LocalUncontrolledSingleSortingExample() { return ( <> <DataSource<Developer> primaryKey="id" data={dataSource}> <InfiniteTable<Developer> columns={columns} columnDefaultWidth={120} /> </DataSource> </> ); } const dataSource: Developer[] = [ { id: 0, firstName: 'Nya', country: 'India', city: 'Unnao', birthDate: new Date(1997, 0, 1), currency: 'JPY', preferredLanguage: 'TypeScript', salary: 60000, hobby: 'sports', email: 'Nya44@gmail.com', }, { id: 1, firstName: 'Axel', country: 'Mexico', city: 'Cuitlahuac', birthDate: new Date(1993, 3, 10), currency: 'USD', preferredLanguage: 'TypeScript', salary: 100000, hobby: 'sports', email: 'Axel93@hotmail.com', }, { id: 2, firstName: 'Gonzalo', country: 'United Arab Emirates', city: 'Fujairah', birthDate: new Date(1997, 10, 30), currency: 'JPY', preferredLanguage: 'Go', salary: 120000, hobby: 'photography', email: 'Gonzalo_McGlynn34@gmail.com', }, { id: 3, firstName: 'Sherwood', country: 'Mexico', city: 'Tlacolula de Matamoros', birthDate: new Date(1990, 5, 20), currency: 'CHF', preferredLanguage: 'Rust', salary: 99000, hobby: 'cooking', email: 'Sherwood_McLaughlin65@hotmail.com', }, { id: 4, firstName: 'Alexandre', country: 'France', city: 'Persan', birthDate: new Date(1990, 3, 20), currency: 'EUR', preferredLanguage: 'Go', salary: 97000, hobby: 'reading', email: 'Alexandre_Harber@hotmail.com', }, { id: 5, firstName: 'Mariane', country: 'United States', city: 'Hays', birthDate: new Date(2002, 3, 20), currency: 'EUR', preferredLanguage: 'TypeScript', salary: 58000, hobby: 'cooking', email: 'Mariane0@hotmail.com', }, { id: 6, firstName: 'Rosalind', country: 'Mexico', city: 'Nuevo Casas Grandes', birthDate: new Date(1992, 11, 12), currency: 'AUD', preferredLanguage: 'JavaScript', salary: 198000, hobby: 'dancing', email: 'Rosalind69@gmail.com', }, { id: 7, firstName: 'Lolita', country: 'Sweden', city: 'Delsbo', birthDate: new Date(1990, 9, 5), currency: 'JPY', preferredLanguage: 'TypeScript', salary: 200000, hobby: 'cooking', email: 'Lolita.Hayes@hotmail.com', }, { id: 8, firstName: 'Tre', country: 'Germany', city: 'Bad Camberg', birthDate: new Date(1990, 9, 15), currency: 'GBP', preferredLanguage: 'TypeScript', salary: 200000, hobby: 'sports', email: 'Tre28@gmail.com', }, { id
Using date strings
Note
If your dates are not instanceof Date
but strings or numbers (timestamps) then it's better not to use the column.type="date"
but rather to specify a custom column.type
along with sortTypes
.
For the case when your dates are not actually dates, but date strings (the same applies to timestamps), you have to define your sorting function.
const sortTypes = {
mydatestring: (a: string, b: string) => {
// use your preferred date parsing library
// to turn a string into date and then compare the two values
return new Date(a).getTime() - new Date(b).getTime();
},
};
When then pass the sortTypes
to the <DataSource />
component and configure our date column to be of type "mydatestring"
(it should match the key you specified in your sortTypes
definition).
In this example, the birthDate
column contains dates as strings, so we have to define a custom column.type and sort type.
import '@infinite-table/infinite-react/index.css'; import { InfiniteTable, DataSource, DataSourceSortInfo, type InfiniteTablePropColumns, } from '@infinite-table/infinite-react'; import * as React from 'react'; type Developer = { birthDate: string; id: number; firstName: string; country: string; city: string; currency: string; email: string; preferredLanguage: string; hobby: string; salary: number; }; const columns: InfiniteTablePropColumns<Developer> = { firstName: { field: 'firstName', header: 'First Name' }, birthDate: { field: 'birthDate', header: 'Birth Date', type: 'datestring', defaultWidth: 150, }, salary: { field: 'salary', header: 'Salary', type: 'number', }, country: { field: 'country', header: 'Country' }, preferredLanguage: { field: 'preferredLanguage' }, id: { field: 'id' }, hobby: { field: 'hobby' }, city: { field: 'city' }, currency: { field: 'currency' }, }; const sortTypes = { datestring: (a: string, b: string) => { return new Date(a).getTime() - new Date(b).getTime(); }, }; const defaultSortInfo: DataSourceSortInfo<Developer> = [ { field: 'birthDate', dir: -1, }, ]; export default function LocalUncontrolledSingleSortingExample() { return ( <> <DataSource<Developer> primaryKey="id" data={dataSource} sortTypes={sortTypes} defaultSortInfo={defaultSortInfo} > <InfiniteTable<Developer> columns={columns} columnDefaultWidth={120} /> </DataSource> </> ); } const dataSource: Developer[] = [ { id: 0, firstName: 'Nya', country: 'India', city: 'Unnao', birthDate: '1997-01-01', currency: 'JPY', preferredLanguage: 'TypeScript', salary: 60000, hobby: 'sports', email: 'Nya44@gmail.com', }, { id: 1, firstName: 'Axel', country: 'Mexico', city: 'Cuitlahuac', birthDate: '1993-04-10', currency: 'USD', preferredLanguage: 'TypeScript', salary: 100000, hobby: 'sports', email: 'Axel93@hotmail.com', }, { id: 2, firstName: 'Gonzalo', country: 'United Arab Emirates', city: 'Fujairah', birthDate: '1997-11-30', currency: 'JPY', preferredLanguage: 'Go', salary: 120000, hobby: 'photography', email: 'Gonzalo_McGlynn34@gmail.com', }, { id: 3, firstName: 'Sherwood', country: 'Mexico', city: 'Tlacolula de Matamoros', birthDate: '1990-06-20', currency: 'CHF', preferredLanguage: 'Rust', salary: 99000, hobby: 'cooking', email: 'Sherwood_McLaughlin65@hotmail.com', }, { id: 4, firstName: 'Alexandre', country: 'France', city: 'Persan', // birthDate: new Date(1990, 3, 20), birthDate: '1990-04-20', currency: 'EUR', preferredLanguage: 'Go', salary: 97000, hobby: 'reading', email: 'Alexandre_Harber@hotmail.com', }, { id: 5, firstName: 'Mariane', country: 'United States', city: 'Hays', birthDate: '2002-04-20', currency: 'EUR', preferredLanguage: 'TypeScript', salary: 58000, hobby: 'cooking', email: 'Mariane0@hotmail.com', }, { id: 6, firstName: 'Rosalind', country: 'Mexico', city: 'Nuevo Casas Grandes', birthDate: '1992-12-12', currency: 'AUD', preferredLanguage: 'JavaScript', salary: 198000, hobby: 'dancing', email: 'Rosalind69@gmail.com', }, { id: 7, firstName: 'Lolita', country: 'Sweden', city: 'Delsbo', birthDate: '1990-10-05', currency: 'JPY', preferredLanguage: 'TypeScript', salary: 200000, hobby: 'cooking', email: 'Lolita.Hayes@hotmail.com', }, { id: 8, firstName: 'Tre', country: 'Germany', city: 'Bad Camberg', birthDate: '1990-10-15', currency: 'GBP', preferredLanguage: 'TypeScript', salary: 200000, hobby: 'sports', email: 'Tre28@gmail.com', }, { id