Skip to content

Commit

Permalink
add missing types
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisBullinger committed Jul 13, 2022
1 parent 1e61e32 commit 3cd0a9e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module.exports = {
root: true,
extends: [
'eslint:recommended',
'prettier',
'react-app',
'plugin:react/recommended',
'prettier',
],
plugins: ['@typescript-eslint'],
parser: '@typescript-eslint/parser',
Expand All @@ -14,8 +14,6 @@ module.exports = {
rules: {
'prefer-const': 'warn',
'no-console': 'warn',
'@typescript-eslint/no-extra-semi': 'off',
'semi-style': ['error', 'first'],
'no-debugger': 'warn',
'no-constant-condition': ['error', { checkLoops: false }],
'require-await': 'off',
Expand Down
31 changes: 20 additions & 11 deletions src/TableComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { useState, FunctionComponent, forwardRef } from 'react';
import React, {
useState,
FunctionComponent,
forwardRef,
FormEvent,
} from 'react';
import { uuid } from '@sanity/uuid';
import FormField from 'part:@sanity/components/formfields/default';
import PatchEvent, { set, unset } from 'part:@sanity/form-builder/patch-event';
Expand All @@ -24,10 +29,10 @@ type Props = {
value: {
rows: TableRow[];
};
onChange: (...any) => any;
onChange: (data: unknown) => unknown;
};

type TableRow = {
export type TableRow = {
_type: string;
_key: string;
cells: string[];
Expand All @@ -40,7 +45,7 @@ const TableComponent: FunctionComponent<Props> = props => {
callback: () => any;
} | null>(null);

const updateValue = value => {
const updateValue = (value: Props['value']) => {
return onChange(PatchEvent.from(set(value)));
};

Expand All @@ -63,7 +68,7 @@ const TableComponent: FunctionComponent<Props> = props => {
},
],
};
return updateValue({ ...(value || {}), ...newValue });
return updateValue({ ...(value ?? {}), ...newValue });
};

const confirmRemoveTable = () => {
Expand Down Expand Up @@ -142,7 +147,7 @@ const TableComponent: FunctionComponent<Props> = props => {
return updateValue(newValue);
};

const removeColumn = index => {
const removeColumn = (index: number) => {
const newValue = deepClone(value);
newValue.rows.forEach(row => {
row.cells.splice(index, 1);
Expand All @@ -151,9 +156,15 @@ const TableComponent: FunctionComponent<Props> = props => {
setDialog(null);
};

const updateCell = (e, rowIndex, cellIndex) => {
const updateCell = (
e: FormEvent<HTMLInputElement>,
rowIndex: number,
cellIndex: number
) => {
const newValue = deepClone(value);
newValue.rows[rowIndex].cells[cellIndex] = e.target.value;
newValue.rows[rowIndex].cells[cellIndex] = (
e.target as HTMLInputElement
).value;
return updateValue(newValue);
};

Expand Down Expand Up @@ -219,6 +230,4 @@ const TableComponent: FunctionComponent<Props> = props => {
);
};

export default forwardRef((props: RootProps, ref) => (
<TableComponent {...props} />
));
export default forwardRef<never, Props>(props => <TableComponent {...props} />);
12 changes: 6 additions & 6 deletions src/components/TableInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FunctionComponent } from 'react';
import { Box, Button, TextInput } from '@sanity/ui';
import { RemoveIcon } from '@sanity/icons';
import type { TableRow } from '../TableComponent';
import styles from './table.css';

const TableInput: FunctionComponent<{
Expand All @@ -13,20 +14,19 @@ const TableInput: FunctionComponent<{
removeRow: (index: number) => any;
removeColumn: (index: number) => any;
}> = props => {
const renderRowCell = rowIndex => (cell, cellIndex) => {
return (
const renderRowCell =
(rowIndex: number) => (cell: string, cellIndex: number) => {
<td key={`cell-${rowIndex}-${cellIndex}`}>
<TextInput
fontSize={1}
padding={3}
value={cell}
onChange={e => props.updateCell(e, rowIndex, cellIndex)}
/>
</td>
);
};
</td>;
};

const renderRow = (row, rowIndex) => {
const renderRow = (row: TableRow, rowIndex: number) => {
const renderCell = renderRowCell(rowIndex);
return (
<tr key={`row-${rowIndex}`}>
Expand Down
4 changes: 2 additions & 2 deletions src/components/TableMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent, useState } from 'react';
import React, { FormEventHandler, FunctionComponent, useState } from 'react';
import {
Box,
Button,
Expand Down Expand Up @@ -28,7 +28,7 @@ const TableMenu: FunctionComponent<{

const [count, setCount] = useState('');

const updateCount = e => {
const updateCount: FormEventHandler<HTMLInputElement> = e => {
setCount(e.currentTarget.value);
};

Expand Down
1 change: 1 addition & 0 deletions src/components/TablePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { FunctionComponent } from 'react';
import type { TableRow } from '../TableComponent';
import styles from './table.css';

const TablePreview: FunctionComponent<{ rows: TableRow[] }> = props => {
Expand Down
37 changes: 17 additions & 20 deletions src/schema/table.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import config from 'config:table';
import { DatabaseIcon } from '@sanity/icons';
import TableComponent from '../TableComponent';
import TablePreview from '../components/TablePreview';

Expand All @@ -24,24 +23,22 @@ export default {
select: {
rows: 'rows',
},
prepare({ rows }) {
return {
title: 'Table',
media: (
<svg
xmlns="http://www.w3.org/2000/svg"
width="1em"
height="1em"
viewBox="0 0 25 25"
fill="none"
stroke="currentColor"
strokeWidth="1.2"
>
<path d="M3 3h18v18H3zM21 9H3M21 15H3M12 3v18" />
</svg>
),
extendedPreview: <TablePreview rows={rows || []} />,
};
},
prepare: ({ rows }) => ({
title: 'Table',
media: (
<svg
xmlns="http://www.w3.org/2000/svg"
width="1em"
height="1em"
viewBox="0 0 25 25"
fill="none"
stroke="currentColor"
strokeWidth="1.2"
>
<path d="M3 3h18v18H3zM21 9H3M21 15H3M12 3v18" />
</svg>
),
extendedPreview: <TablePreview rows={rows || []} />,
}),
},
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"esModuleInterop": true,
"jsx": "react",
"lib": ["ESNext"],
"lib": ["ESNext", "DOM"],
"resolveJsonModule": true
},
"include": [
Expand Down

0 comments on commit 3cd0a9e

Please sign in to comment.