Skip to content

Commit

Permalink
fix: pass row type to table component
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunk committed Jan 17, 2024
1 parent b295dc1 commit 35d5457
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/components/TableComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { Box, Button, Card, Dialog, Flex, Inline, Text } from '@sanity/ui';

import { AddIcon } from '@sanity/icons';

const ROW_TYPE = 'tableRow';

const deepClone: <T>(data: T) => T =
globalThis.structuredClone ?? (data => JSON.parse(JSON.stringify(data)));

Expand All @@ -29,8 +27,8 @@ export type TableRow = {
// TODO refactor deeplone stuff to use proper patches

Check warning on line 27 in src/components/TableComponent.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: 'TODO refactor deeplone stuff to use...'

Check warning on line 27 in src/components/TableComponent.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: 'TODO refactor deeplone stuff to use...'
// TODO use callback all the things

Check warning on line 28 in src/components/TableComponent.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: 'TODO use callback all the things'

Check warning on line 28 in src/components/TableComponent.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: 'TODO use callback all the things'

const TableComponent = (props: TableProps) => {
const { value, onChange } = props;
const TableComponent = (props: TableProps & { rowType?: string }) => {
const { rowType = 'tableRow', value, onChange } = props;
const [dialog, setDialog] = useState<{
type: string;
callback: () => any;
Expand All @@ -48,12 +46,12 @@ const TableComponent = (props: TableProps) => {
const newValue: Omit<TableValue, '_type'> = {
rows: [
{
_type: ROW_TYPE,
_type: rowType,
_key: uuid(),
cells: ['', ''],
},
{
_type: ROW_TYPE,
_type: rowType,
_key: uuid(),
cells: ['', ''],
},
Expand Down Expand Up @@ -81,7 +79,7 @@ const TableComponent = (props: TableProps) => {
for (let i = 0; i < count; i++) {
// Add as many cells as we have columns
newValue.rows.push({
_type: ROW_TYPE,
_type: rowType,
_key: uuid(),
cells: Array(columnCount).fill(''),
});
Expand All @@ -99,7 +97,7 @@ const TableComponent = (props: TableProps) => {
const columnCount = value.rows[0].cells.length;

newValue.rows.splice(index, 0, {
_type: ROW_TYPE,
_type: rowType,
_key: uuid(),
cells: Array(columnCount).fill(''),
});
Expand Down Expand Up @@ -255,4 +253,8 @@ const TableComponent = (props: TableProps) => {
);
};

export function createTableComponent(rowType: string) {
return (props: TableProps) => <TableComponent {...props} rowType={rowType} />;
}

export default TableComponent;
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { definePlugin, defineType } from 'sanity';

import { TableComponent, TablePreview } from './components';
import { createTableComponent } from './components/TableComponent';
export type {
TableValue,
TableProps,
Expand Down Expand Up @@ -43,7 +44,7 @@ export const table = definePlugin<TableConfig | undefined>(config => {
},
],
components: {
input: TableComponent as any,
input: createTableComponent(tableRowSchema.name) as any,
preview: TablePreview as any,
},
preview: {
Expand Down

0 comments on commit 35d5457

Please sign in to comment.