import * as React from 'react';
import { createListItems, IExampleItem } from '@fluentui/example-data';
import { Link } from '@fluentui/react/lib/Link';
import { Image, ImageFit } from '@fluentui/react/lib/Image';
import { DetailsList, buildColumns, IColumn } from '@fluentui/react/lib/DetailsList';
import { mergeStyles } from '@fluentui/react/lib/Styling';
export interface IDetailsListCustomColumnsExampleState {
sortedItems: IExampleItem[];
columns: IColumn[];
}
export class DetailsListCustomColumnsExample extends React.Component<{}, IDetailsListCustomColumnsExampleState> {
constructor(props: {}) {
super(props);
const items = createListItems(500);
this.state = {
sortedItems: items,
columns: _buildColumns(items),
};
}
public render() {
const { sortedItems, columns } = this.state;
return (
);
}
private _onColumnClick = (event: React.MouseEvent, column: IColumn): void => {
const { columns } = this.state;
let { sortedItems } = this.state;
let isSortedDescending = column.isSortedDescending;
// If we've sorted this column, flip it.
if (column.isSorted) {
isSortedDescending = !isSortedDescending;
}
// Sort the items.
sortedItems = _copyAndSort(sortedItems, column.fieldName!, isSortedDescending);
// Reset the items and columns to match the state.
this.setState({
sortedItems: sortedItems,
columns: columns.map(col => {
col.isSorted = col.key === column.key;
if (col.isSorted) {
col.isSortedDescending = isSortedDescending;
}
return col;
}),
});
};
private _onColumnHeaderContextMenu(column: IColumn | undefined, ev: React.MouseEvent | undefined): void {
console.log(`column ${column!.key} contextmenu opened.`);
}
private _onItemInvoked(item: any, index: number | undefined): void {
alert(`Item ${item.name} at index ${index} has been invoked.`);
}
}
function _buildColumns(items: IExampleItem[]): IColumn[] {
const columns = buildColumns(items);
const thumbnailColumn = columns.filter(column => column.name === 'thumbnail')[0];
// Special case one column's definition.
thumbnailColumn.name = '';
thumbnailColumn.maxWidth = 50;
thumbnailColumn.ariaLabel = 'Thumbnail';
return columns;
}
function _renderItemColumn(item: IExampleItem, index: number, column: IColumn) {
const fieldContent = item[column.fieldName as keyof IExampleItem] as string;
switch (column.key) {
case 'thumbnail':
return ;
case 'name':
return {fieldContent};
case 'color':
return (
{fieldContent}
);
default:
return {fieldContent};
}
}
function _copyAndSort(items: T[], columnKey: string, isSortedDescending?: boolean): T[] {
const key = columnKey as keyof T;
return items.slice(0).sort((a: T, b: T) => ((isSortedDescending ? a[key] < b[key] : a[key] > b[key]) ? 1 : -1));
}