Skip to content

Commit 5ca49b6

Browse files
authored
Merge pull request #18 from Mawla/feature/indexes
Adding the ability to add rows and columns at indexes
2 parents 32849f1 + 2742ac5 commit 5ca49b6

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/TableComponent.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ const TableComponent: FunctionComponent<RootProps> = (props) => {
7070
return updateValue(newValue);
7171
};
7272

73+
const addRowAt = (index: number = 0) => {
74+
const newValue = { ...value };
75+
// Calculate the column count from the first row
76+
const columnCount = value.rows[0].cells.length;
77+
78+
newValue.rows.splice(index, 0, {
79+
_type: config.rowType,
80+
_key: uuid(),
81+
cells: Array(columnCount).fill(''),
82+
})
83+
84+
return updateValue(newValue);
85+
};
86+
7387
const removeRow = (index: number) => {
7488
const newValue = deepClone(value);
7589
newValue.rows.splice(index, 1);
@@ -98,6 +112,16 @@ const TableComponent: FunctionComponent<RootProps> = (props) => {
98112
return updateValue(newValue);
99113
};
100114

115+
const addColumnAt = (index: number) => {
116+
const newValue = deepClone(value);
117+
118+
newValue.rows.forEach((_, i) => {
119+
newValue.rows[i].cells.splice(index, 0, '')
120+
});
121+
122+
return updateValue(newValue);
123+
};
124+
101125
const removeColumn = (index) => {
102126
const newValue = deepClone(value);
103127
newValue.rows.forEach((row) => {
@@ -152,7 +176,9 @@ const TableComponent: FunctionComponent<RootProps> = (props) => {
152176
{value?.rows?.length && (
153177
<TableMenu
154178
addColumns={addColumns}
179+
addColumnAt={addColumnAt}
155180
addRows={addRows}
181+
addRowAt={addRowAt}
156182
remove={confirmRemoveTable}
157183
placement="left"
158184
/>

src/components/TableMenu.tsx

+24-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import { AddIcon, WarningOutlineIcon, ControlsIcon } from '@sanity/icons';
1515

1616
const TableMenu: FunctionComponent<{
1717
addColumns: (count: number) => any;
18+
addColumnAt: (index: number) => any;
1819
addRows: (count: number) => any;
20+
addRowAt: (index: number) => any;
1921
remove: () => any;
2022
placement: 'top' | 'bottom' | 'left' | 'right' | 'auto';
2123
}> = (props) => {
@@ -34,16 +36,25 @@ const TableMenu: FunctionComponent<{
3436
setDialog({ type: 'rows', callback: (count) => props.addRows(count) });
3537
};
3638

39+
const addRowAt = () => {
40+
setDialog({ type: 'rows', callback: (index) => props.addRowAt(index) });
41+
};
42+
3743
const addColumns = () => {
3844
setDialog({
3945
type: 'columns',
4046
callback: (count) => props.addColumns(count),
4147
});
4248
};
4349

50+
const addColumnsAt = () => {
51+
setDialog({ type: 'columns', callback: (index) => props.addColumnAt(index) });
52+
};
53+
4454
const onConfirm = () => {
4555
const parsedCount = parseInt(count, 10);
46-
if (parsedCount && parsedCount < 100) {
56+
57+
if (parsedCount < 100) {
4758
setDialog(null);
4859
dialog.callback(parsedCount);
4960
setCount(undefined);
@@ -94,12 +105,24 @@ const TableMenu: FunctionComponent<{
94105
text="Add Row(s)"
95106
onClick={addRows}
96107
/>
108+
<MenuItem
109+
icon={AddIcon}
110+
fontSize={1}
111+
text="Add Row At Index"
112+
onClick={addRowAt}
113+
/>
97114
<MenuItem
98115
icon={AddIcon}
99116
fontSize={1}
100117
text="Add Column(s)"
101118
onClick={addColumns}
102119
/>
120+
<MenuItem
121+
icon={AddIcon}
122+
fontSize={1}
123+
text="Add Column At Index"
124+
onClick={addColumnsAt}
125+
/>
103126
<MenuDivider />
104127
<MenuItem
105128
icon={WarningOutlineIcon}

0 commit comments

Comments
 (0)