Skip to content

Commit 6c793f8

Browse files
committed
v0.3.0
1 parent 24c8eee commit 6c793f8

6 files changed

Lines changed: 33 additions & 38 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,19 @@ The `Table` component is the main entry point, and has two generic type paramete
8585
| `activeRange` | `ActiveRange` | The current selection state. |
8686
| `setActiveRange` | `(range: ActiveRange) => void` | Callback to update the selection state. |
8787
| `columnsResizeable` | `boolean` | Enables column resizing. |
88-
| `columnsEditable` | `boolean` | Enables column operations (insert/delete/rename). |
89-
| `rowsEditable` | `boolean` | Enables row operations (insert/delete). |
88+
| `columnsRenameable` | `boolean` | Enables column renaming. |
89+
| `addColumnButton` | `boolean` | Whether to render an "add column" button. |
90+
| `addRowButton` | `boolean` | Whether to render an "add row" button. |
9091
| `cellHeight` | `number` | Height of cells in pixels (default: 29). |
91-
| `cellContent` | `(column: Column) => Component<CellContentProps<Value>>` | Component used to render the content of cells for a particular column. |
92+
| `getCellContent` | `(column: Column) => Component<CellContentProps<Value>>` | Component used to render the content of cells for a particular column. |
9293
| `getColumnIcon` | `(column: Column) => Component` | Gets the icon for a particular column. |
9394
| `getColumnSize` | `(column: Column) => number` | Get column width. |
9495
| `setColumnSize` | `(column: Column, width: number) => void` | Set column width. |
9596
| `resetColumnSize` | `(column: Column) => void` | Reset column width. |
9697
| `getColumnName` | `(column: Column) => string` | Get column name. |
9798
| `setColumnName` | `(column: Column, name: string) => void` | Set column name. |
98-
| `insertColumns` | `(index: number, count: number) => void` | Insert columns. |
99-
| `insertRows` | `(index: number, count: number) => void` | Insert rows. |
100-
| `removeColumns` | `(index: number, count: number) => void` | Remove columns. |
101-
| `removeRows` | `(index: number, count: number) => void` | Remove rows. |
99+
| `onInsertColumns` | `(index: number, count: number) => void` | Insert columns. |
100+
| `onInsertRows` | `(index: number, count: number) => void` | Insert rows. |
102101
| `onViewportChanged` | `(start: number, end: number) => void` | Called whenever the range of rows visible in the viewport changes. |
103102
| `onCellContextMenu` | `(ev: MouseEvent, row: number, column: Column) => void` | Fired when a cell context menu is opened. |
104103
| `onCopy` | `(min: CellIndex, max: CellIndex) => void` | Called when cells are copied. |

TODO

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- Generalise `setCellValue` to range of cells
1+
- Events
2+
- Generalise `setCellValue` to range of cells
23
- Extract out context menu
3-
- Lift cell component to top level?
44
- Column auto-sizing

src/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Table from 'src/table/Table'
2-
import type { ActiveRange } from 'src/table/types'
3-
import { createTextContent, createCheckboxContent } from './components/CellContent'
4-
5-
export { Table, ActiveRange, createTextContent, createCheckboxContent }
1+
export { Table } from 'src/table/Table'
2+
export type { ActiveRange } from 'src/table/types'
3+
export { createTextContent, createCheckboxContent } from './components/CellContent'

src/stories/Table.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const meta = {
4848
<Table
4949
columns={columns()}
5050
numRows={numRows()}
51-
cellContent={props.cellContent}
51+
getCellContent={props.getCellContent}
5252
getCellValue={(row, col) => data()[row]![col]!}
5353
setCellValue={(row, col, value) => {
5454
const newData = data().slice()
@@ -71,7 +71,7 @@ const meta = {
7171
url?: string
7272
accentColor: string
7373
setCellValue: (row: number, column: string, value: unknown) => void
74-
cellContent?: (column: string) => Component<CellContentProps>
74+
getCellContent?: (column: string) => Component<CellContentProps>
7575
}>
7676

7777
export default meta

src/table/ColumnHeader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import './ColumnHeader.css'
88
export interface ColumnHeaderProps<Column, Value> {
99
column: PositionedColumn<Column, Value>
1010
height: number
11-
columnsEditable?: boolean
11+
columnsRenameable?: boolean
1212
columnsResizeable?: boolean
1313
setColumnName?: (column: Column, name: string) => void
1414
setColumnSize?: (column: Column, width: number) => void
@@ -33,7 +33,7 @@ export function ColumnHeader<K, T>(props: ColumnHeaderProps<K, T>) {
3333
class="solid-tabular/renameable"
3434
value={props.column.name}
3535
setValue={name => props.setColumnName?.(props.column.column, name)}
36-
disabled={!props.columnsEditable}
36+
disabled={!props.columnsRenameable}
3737
/>
3838
<div style={{ flex: '1' }} />
3939
<Dynamic component={props.column.icon} />

src/table/Table.tsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ export interface TableProps<Column, Value = unknown> {
2929
columns: Column[]
3030
/** The number of rows. */
3131
numRows: number
32-
/** Whether columns can be inserted, removed, modified and re-ordered. */
33-
columnsEditable?: boolean
34-
/** Whether rows can be inserted, removed and re-ordered. */
35-
rowsEditable?: boolean
3632
/** Whether columns can be resized. */
3733
columnsResizeable?: boolean
34+
/** Whether columns can be renamed. */
35+
columnsRenameable?: boolean
36+
/** Whether to render an "add column" button. */
37+
addColumnButton?: boolean
38+
/** Whether to render an "add column" button. */
39+
addRowButton?: boolean
3840
/** Height of cells in pixels. */
3941
cellHeight?: number
4042
/** Component used to render the content of cells for a particular column. */
41-
cellContent?: (column: Column) => Component<CellContentProps<Value>> | undefined
43+
getCellContent?: (column: Column) => Component<CellContentProps<Value>> | undefined
4244
/** Gets the icon for a particular column. */
4345
getColumnIcon?: (column: Column) => Component | undefined
4446
/** The state of the selected cell or cells in the table. */
@@ -60,13 +62,9 @@ export interface TableProps<Column, Value = unknown> {
6062
/** Sets the name of the given column. */
6163
setColumnName?: (column: Column, name: string) => void
6264
/** Inserts a numer of columns into the table. */
63-
insertColumns?: (index: number, count: number) => void
65+
onInsertColumns?: (index: number, count: number) => void
6466
/** Inserts a number of rows into the table. */
65-
insertRows?: (index: number, count: number) => void
66-
/** Removes a number of columns from the table. */
67-
removeColumns?: (index: number, count: number) => void
68-
/** Removes a number of rows from the table. */
69-
removeRows?: (index: number, count: number) => void
67+
onInsertRows?: (index: number, count: number) => void
7068
/** Called whenever the range of rows visible in the viewport changes. */
7169
onViewportChanged?: (start: number, end: number) => void
7270
/** Fired when a cell context menu is opened. */
@@ -83,7 +81,7 @@ export interface TableProps<Column, Value = unknown> {
8381
onScrollPositionChange?: (scrollLeft: number, scrollTop: number) => void
8482
}
8583

86-
export default function Table<Column, Value = unknown>(props: TableProps<Column, Value>) {
84+
export function Table<Column, Value = unknown>(props: TableProps<Column, Value>) {
8785
// The root DOM element of the table
8886
let tableEl: HTMLDivElement | undefined
8987

@@ -237,7 +235,7 @@ export default function Table<Column, Value = unknown>(props: TableProps<Column,
237235
return props.getColumnName?.(column) ?? String(column)
238236
},
239237
get component() {
240-
return props.cellContent?.(column) ?? DEFAULT_CELL_CONTENT
238+
return props.getCellContent?.(column) ?? DEFAULT_CELL_CONTENT
241239
},
242240
get icon() {
243241
return props.getColumnIcon?.(column)
@@ -538,8 +536,8 @@ export default function Table<Column, Value = unknown>(props: TableProps<Column,
538536

539537
// Append row button
540538
const appendRow = () => {
541-
if (!props.insertRows) return
542-
props.insertRows(numRows(), 1)
539+
if (!props.onInsertRows) return
540+
props.onInsertRows(numRows(), 1)
543541

544542
// Scroll to the last row
545543
scrollToCell(props.numRows, undefined)
@@ -612,7 +610,7 @@ export default function Table<Column, Value = unknown>(props: TableProps<Column,
612610
<ColumnHeader
613611
column={column}
614612
height={colHeaderHeight()}
615-
columnsEditable={props.columnsEditable}
613+
columnsRenameable={props.columnsRenameable}
616614
columnsResizeable={props.columnsResizeable}
617615
setColumnName={props.setColumnName}
618616
setColumnSize={props.setColumnSize}
@@ -622,12 +620,12 @@ export default function Table<Column, Value = unknown>(props: TableProps<Column,
622620
)}
623621
</For>
624622

625-
<Show when={props.columnsEditable}>
623+
<Show when={props.addColumnButton}>
626624
<AddColumnButton
627625
tableWidth={tableWidth()}
628626
width={DEFAULT_COLUMN_SIZE}
629627
height={colHeaderHeight()}
630-
onPointerDown={() => props.insertColumns?.(numCols(), 1)}
628+
onPointerDown={() => props.onInsertColumns?.(numCols(), 1)}
631629
/>
632630
</Show>
633631

@@ -672,7 +670,7 @@ export default function Table<Column, Value = unknown>(props: TableProps<Column,
672670
<Show when={activeCellData()} keyed>
673671
{({ row, column }) => (
674672
<CellInputContainer
675-
component={props.cellContent?.(column) ?? DEFAULT_CELL_CONTENT}
673+
component={props.getCellContent?.(column) ?? DEFAULT_CELL_CONTENT}
676674
rect={activeCellOutline()!}
677675
value={props.getCellValue(row, column)}
678676
setValue={value => props.setCellValue?.(row, column, value)}
@@ -686,7 +684,7 @@ export default function Table<Column, Value = unknown>(props: TableProps<Column,
686684
</div>
687685

688686
{/* Add row button */}
689-
<Show when={props.rowsEditable}>
687+
<Show when={props.addRowButton}>
690688
<AddRowButton
691689
tableWidth={tableWidth()}
692690
cellHeight={cellHeight()}

0 commit comments

Comments
 (0)