|
1 | 1 | import * as React from "react"; |
| 2 | +import classNames from "classnames"; |
2 | 3 | import { columnIndexToLabel } from "hot-formula-parser"; |
3 | 4 | import * as Types from "./types"; |
| 5 | +import * as Actions from "./actions"; |
| 6 | +import * as Selection from "./selection"; |
| 7 | +import useDispatch from "./use-dispatch"; |
| 8 | +import useSelector from "./use-selector"; |
4 | 9 |
|
5 | | -const ColumnIndicator: Types.ColumnIndicatorComponent = ({ column, label }) => ( |
6 | | - <th className="Spreadsheet__header"> |
7 | | - {label !== undefined ? label : columnIndexToLabel(String(column))} |
8 | | - </th> |
9 | | -); |
| 10 | +const ColumnIndicator: Types.ColumnIndicatorComponent = ({ |
| 11 | + column, |
| 12 | + label, |
| 13 | + selected, |
| 14 | + onSelect, |
| 15 | +}) => { |
| 16 | + const handleClick = React.useCallback( |
| 17 | + (event: React.MouseEvent) => { |
| 18 | + onSelect(column, event.shiftKey); |
| 19 | + }, |
| 20 | + [onSelect, column] |
| 21 | + ); |
| 22 | + return ( |
| 23 | + <th |
| 24 | + className={classNames("Spreadsheet__header", { |
| 25 | + "Spreadsheet__header--selected": selected, |
| 26 | + })} |
| 27 | + onClick={handleClick} |
| 28 | + tabIndex={0} |
| 29 | + > |
| 30 | + {label !== undefined ? label : columnIndexToLabel(String(column))} |
| 31 | + </th> |
| 32 | + ); |
| 33 | +}; |
10 | 34 |
|
11 | 35 | export default ColumnIndicator; |
| 36 | + |
| 37 | +export const enhance = ( |
| 38 | + ColumnIndicatorComponent: Types.ColumnIndicatorComponent |
| 39 | +): React.FC<Omit<Types.ColumnIndicatorProps, "selected" | "onSelect">> => { |
| 40 | + return function ColumnIndicatorWrapper(props) { |
| 41 | + const dispatch = useDispatch(); |
| 42 | + const selectEntireColumn = React.useCallback( |
| 43 | + (column: number, extend: boolean) => |
| 44 | + dispatch(Actions.selectEntireColumn(column, extend)), |
| 45 | + [dispatch] |
| 46 | + ); |
| 47 | + const selected = useSelector( |
| 48 | + (state) => |
| 49 | + Selection.hasEntireColumn(state.selected, props.column) || |
| 50 | + Selection.isEntireTable(state.selected) |
| 51 | + ); |
| 52 | + return ( |
| 53 | + <ColumnIndicatorComponent |
| 54 | + {...props} |
| 55 | + selected={selected} |
| 56 | + onSelect={selectEntireColumn} |
| 57 | + /> |
| 58 | + ); |
| 59 | + }; |
| 60 | +}; |
0 commit comments