|
| 1 | +import classNames from "classnames"; |
| 2 | +import { HTMLAttributes, KeyboardEvent, ReactElement, ReactNode, useMemo } from "react"; |
| 3 | +import { FaArrowsAltV } from "./icons/FaArrowsAltV"; |
| 4 | +import { FaLongArrowAltDown } from "./icons/FaLongArrowAltDown"; |
| 5 | +import { FaLongArrowAltUp } from "./icons/FaLongArrowAltUp"; |
| 6 | + |
| 7 | +import ColumnHeader from "./ColumnHeader"; |
| 8 | + |
| 9 | +import { useColumn, useColumnsStore, useDatagridConfig, useHeaderDragDrop } from "../model/hooks/injection-hooks"; |
| 10 | +import { GridColumn } from "../typings/GridColumn"; |
| 11 | +import { ColumnResizerProps } from "./ColumnResizer"; |
| 12 | +import { ColumnHeaderViewModel } from "../features/column/ColumnHeader.viewModel"; |
| 13 | +import { observer } from "mobx-react-lite"; |
| 14 | + |
| 15 | +export interface ColumnContainerProps { |
| 16 | + isLast?: boolean; |
| 17 | + resizer: ReactElement<ColumnResizerProps>; |
| 18 | +} |
| 19 | + |
| 20 | +export const ColumnContainer = observer(function ColumnContainer(props: ColumnContainerProps): ReactElement { |
| 21 | + const { columnsFilterable, id: gridId } = useDatagridConfig(); |
| 22 | + const columnsStore = useColumnsStore(); |
| 23 | + const column = useColumn(); |
| 24 | + const { canDrag, canSort } = column; |
| 25 | + |
| 26 | + const headerDragDropStore = useHeaderDragDrop(); |
| 27 | + const columnHeaderVM = useMemo( |
| 28 | + () => |
| 29 | + new ColumnHeaderViewModel({ |
| 30 | + dndStore: headerDragDropStore, |
| 31 | + columnsStore, |
| 32 | + columnsDraggable: canDrag |
| 33 | + }), |
| 34 | + [headerDragDropStore, columnsStore, canDrag] |
| 35 | + ); |
| 36 | + const draggableProps = columnHeaderVM.draggableProps; |
| 37 | + const dropTarget = columnHeaderVM.dropTarget; |
| 38 | + const isDragging = columnHeaderVM.dragging; |
| 39 | + |
| 40 | + const sortProps = canSort ? getSortProps(column) : null; |
| 41 | + const caption = column.header.trim(); |
| 42 | + |
| 43 | + return ( |
| 44 | + <div |
| 45 | + aria-sort={getAriaSort(canSort, column)} |
| 46 | + className={classNames("th", { |
| 47 | + [`drop-${dropTarget?.[1]}`]: column.columnId === dropTarget?.[0], |
| 48 | + dragging: column.columnId === isDragging?.[1], |
| 49 | + "dragging-over-self": column.columnId === isDragging?.[1] && !dropTarget |
| 50 | + })} |
| 51 | + role="columnheader" |
| 52 | + style={!canSort ? { cursor: "unset" } : undefined} |
| 53 | + title={caption} |
| 54 | + ref={ref => column.setHeaderElementRef(ref)} |
| 55 | + data-column-id={column.columnId} |
| 56 | + onDrop={draggableProps.onDrop} |
| 57 | + onDragEnter={draggableProps.onDragEnter} |
| 58 | + onDragOver={draggableProps.onDragOver} |
| 59 | + > |
| 60 | + <div |
| 61 | + className={classNames("column-container")} |
| 62 | + id={`${gridId}-column${column.columnId}`} |
| 63 | + draggable={draggableProps.draggable} |
| 64 | + onDragStart={draggableProps.onDragStart} |
| 65 | + onDragEnd={draggableProps.onDragEnd} |
| 66 | + > |
| 67 | + <ColumnHeader |
| 68 | + sortProps={sortProps} |
| 69 | + canSort={canSort} |
| 70 | + caption={caption} |
| 71 | + isDragging={isDragging} |
| 72 | + columnAlignment={column.alignment} |
| 73 | + > |
| 74 | + {canSort ? <SortIcon /> : null} |
| 75 | + </ColumnHeader> |
| 76 | + {columnsFilterable && ( |
| 77 | + <div className="filter" style={{ pointerEvents: isDragging ? "none" : undefined }}> |
| 78 | + {columnsStore.columnFilters[column.columnIndex]?.renderFilterWidgets()} |
| 79 | + </div> |
| 80 | + )} |
| 81 | + </div> |
| 82 | + {column.canResize ? props.resizer : null} |
| 83 | + </div> |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | +function SortIcon(): ReactNode { |
| 88 | + const column = useColumn(); |
| 89 | + switch (column.sortDir) { |
| 90 | + case "asc": |
| 91 | + return <FaLongArrowAltUp />; |
| 92 | + case "desc": |
| 93 | + return <FaLongArrowAltDown />; |
| 94 | + default: |
| 95 | + return <FaArrowsAltV />; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +function getAriaSort(canSort: boolean, column: GridColumn): "ascending" | "descending" | "none" | undefined { |
| 100 | + if (!canSort) { |
| 101 | + return undefined; |
| 102 | + } |
| 103 | + |
| 104 | + switch (column.sortDir) { |
| 105 | + case "asc": |
| 106 | + return "ascending"; |
| 107 | + case "desc": |
| 108 | + return "descending"; |
| 109 | + default: |
| 110 | + return "none"; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function getSortProps(column: GridColumn): HTMLAttributes<HTMLDivElement> { |
| 115 | + return { |
| 116 | + onClick: () => { |
| 117 | + column.toggleSort(); |
| 118 | + }, |
| 119 | + onKeyDown: (e: KeyboardEvent<HTMLDivElement>) => { |
| 120 | + if (e.key === "Enter" || e.key === " ") { |
| 121 | + e.preventDefault(); |
| 122 | + column.toggleSort(); |
| 123 | + } |
| 124 | + }, |
| 125 | + role: "button", |
| 126 | + tabIndex: 0 |
| 127 | + }; |
| 128 | +} |
0 commit comments