Skip to content

Commit

Permalink
Merge pull request #18 from Mawla/feature/indexes
Browse files Browse the repository at this point in the history
Adding the ability to add rows and columns at indexes
  • Loading branch information
rdunk authored Sep 21, 2021
2 parents 32849f1 + 2742ac5 commit 5ca49b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/TableComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ 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;

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 +112,16 @@ const TableComponent: FunctionComponent<RootProps> = (props) => {
return updateValue(newValue);
};

const addColumnAt = (index: number) => {
const newValue = deepClone(value);

newValue.rows.forEach((_, i) => {
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 +176,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) {

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 5ca49b6

Please sign in to comment.