Skip to content

Commit

Permalink
Adding the ability to add at indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Danm72 committed Sep 21, 2021
1 parent 32849f1 commit 76d49b9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/TableComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ const TableComponent: FunctionComponent<RootProps> = (props) => {
return updateValue(newValue);
};

const addRowAt = (index: number = 0) => {
const newValue = { ...value };
// Calculate the column count from the first row
const columnCount = value.rows[0].cells.length;

// Add as many cells as we have columns
newValue.rows.splice(index, 0, {
_type: config.rowType,
_key: uuid(),
cells: Array(columnCount).fill(''),
})

return updateValue(newValue);
};

const removeRow = (index: number) => {
const newValue = deepClone(value);
newValue.rows.splice(index, 1);
Expand Down Expand Up @@ -98,6 +113,19 @@ const TableComponent: FunctionComponent<RootProps> = (props) => {
return updateValue(newValue);
};

const addColumnAt = (index: number) => {
const newValue = deepClone(value);
// Add a cell to each of the rows
newValue.rows.forEach((_, i) => {
// for (let j = 0; j < 1; j++) {
// newValue.rows[i].cells[j].push('');
newValue.rows[i].cells.splice(index, 0, '')
// }
});

return updateValue(newValue);
};

const removeColumn = (index) => {
const newValue = deepClone(value);
newValue.rows.forEach((row) => {
Expand Down Expand Up @@ -152,7 +180,9 @@ const TableComponent: FunctionComponent<RootProps> = (props) => {
{value?.rows?.length && (
<TableMenu
addColumns={addColumns}
addColumnAt={addColumnAt}
addRows={addRows}
addRowAt={addRowAt}
remove={confirmRemoveTable}
placement="left"
/>
Expand Down
25 changes: 24 additions & 1 deletion src/components/TableMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { AddIcon, WarningOutlineIcon, ControlsIcon } from '@sanity/icons';

const TableMenu: FunctionComponent<{
addColumns: (count: number) => any;
addColumnAt: (index: number) => any;
addRows: (count: number) => any;
addRowAt: (index: number) => any;
remove: () => any;
placement: 'top' | 'bottom' | 'left' | 'right' | 'auto';
}> = (props) => {
Expand All @@ -34,16 +36,25 @@ const TableMenu: FunctionComponent<{
setDialog({ type: 'rows', callback: (count) => props.addRows(count) });
};

const addRowAt = () => {
setDialog({ type: 'rows', callback: (index) => props.addRowAt(index) });
};

const addColumns = () => {
setDialog({
type: 'columns',
callback: (count) => props.addColumns(count),
});
};

const addColumnsAt = () => {
setDialog({ type: 'columns', callback: (index) => props.addColumnAt(index) });
};

const onConfirm = () => {
const parsedCount = parseInt(count, 10);
if (parsedCount && parsedCount < 100) {
console.log(parsedCount +"" + count)
if (parsedCount < 100) {
setDialog(null);
dialog.callback(parsedCount);
setCount(undefined);
Expand Down Expand Up @@ -94,12 +105,24 @@ const TableMenu: FunctionComponent<{
text="Add Row(s)"
onClick={addRows}
/>
<MenuItem
icon={AddIcon}
fontSize={1}
text="Add Row At Index"
onClick={addRowAt}
/>
<MenuItem
icon={AddIcon}
fontSize={1}
text="Add Column(s)"
onClick={addColumns}
/>
<MenuItem
icon={AddIcon}
fontSize={1}
text="Add Column At Index"
onClick={addColumnsAt}
/>
<MenuDivider />
<MenuItem
icon={WarningOutlineIcon}
Expand Down

0 comments on commit 76d49b9

Please sign in to comment.