-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
7,607 additions
and
698 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,27 +2,55 @@ | |
"name": "sanity-plugin-table", | ||
"version": "2.1.0", | ||
"description": "Table schema type and input component for Sanity CMS", | ||
"main": "lib/schema/table.js", | ||
"main": "lib/TableComponent.js", | ||
"scripts": { | ||
"build": "babel src -d lib -D", | ||
"prepublishOnly": "npm run build" | ||
"build": "sanipack build", | ||
"watch": "sanipack build --watch", | ||
"prepublishOnly": "sanipack build && sanipack verify" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/rdunk/sanity-plugin-table.git" | ||
}, | ||
"author": "Rupert Dunk <[email protected]>", | ||
"keywords": [ | ||
"sanity", | ||
"sanity-plugin" | ||
], | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"@sanity/icons": "^1.0.1", | ||
"@sanity/ui": "^0.33.10", | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1", | ||
"styled-components": "^5.2.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.5", | ||
"@babel/core": "^7.1.6", | ||
"@babel/plugin-proposal-class-properties": "^7.1.0", | ||
"@babel/preset-env": "^7.1.6", | ||
"@babel/preset-react": "^7.0.0" | ||
"@types/react": "^17.0.3", | ||
"eslint": "7.22.0", | ||
"eslint-config-prettier": "8.1.0", | ||
"eslint-config-sanity": "5.1.0", | ||
"eslint-plugin-react": "7.22.0", | ||
"prettier": "2.2.1", | ||
"sanipack": "1.0.8" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/rdunk/sanity-plugin-table/issues" | ||
}, | ||
"homepage": "https://github.com/rdunk/sanity-plugin-table#readme", | ||
"prettier": { | ||
"semi": false, | ||
"printWidth": 100, | ||
"bracketSpacing": false, | ||
"singleQuote": true | ||
}, | ||
"dependencies": { | ||
"@sanity/uuid": "^3.0.1", | ||
"prop-types": "^15.7.2", | ||
"react": "^17.0.1" | ||
"eslintConfig": { | ||
"parser": "sanipack/babel/eslint-parser", | ||
"extends": [ | ||
"sanity", | ||
"sanity/react", | ||
"prettier", | ||
"prettier/react" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import React, { useState, FunctionComponent, forwardRef } from 'react'; | ||
import { randomKey } from '@sanity/util/lib/pathUtils'; | ||
import FormField from 'part:@sanity/components/formfields/default'; | ||
import PatchEvent, { set, unset } from 'part:@sanity/form-builder/patch-event'; | ||
import config from 'config:table'; | ||
import TableControl from './components/TableControl'; | ||
import TableInput from './components/TableInput'; | ||
import TableMenu from './components/TableMenu'; | ||
import { Box, Button, Card, Dialog, Flex, Inline, Text } from '@sanity/ui'; | ||
|
||
interface RootProps { | ||
level: number; | ||
markers: any[]; | ||
type: { | ||
title: string; | ||
description: string; | ||
options: Record<string, any>; | ||
}; | ||
value: { | ||
rows: TableRow[]; | ||
}; | ||
onChange: (...any) => any; | ||
} | ||
|
||
// This probably isn't necessary anymore | ||
function deepClone<T>(obj: T): T { | ||
return JSON.parse(JSON.stringify(obj)); | ||
} | ||
|
||
const TableComponent: FunctionComponent<RootProps> = (props) => { | ||
const { type, level, value, markers, onChange } = props; | ||
const [dialog, setDialog] = useState<{ | ||
type: string; | ||
callback: () => any; | ||
} | null>(null); | ||
|
||
const updateValue = (value) => { | ||
return onChange(PatchEvent.from(set(value))); | ||
}; | ||
|
||
const resetValue = () => { | ||
return onChange(PatchEvent.from(unset())); | ||
}; | ||
|
||
const createTable = () => { | ||
const newValue = { | ||
rows: [ | ||
{ | ||
_type: config.rowType, | ||
_key: randomKey(), | ||
cells: ['', ''], | ||
}, | ||
{ | ||
_type: config.rowType, | ||
_key: randomKey(), | ||
cells: ['', ''], | ||
}, | ||
], | ||
}; | ||
return updateValue({...(value || {}), ...newValue }); | ||
}; | ||
|
||
const confirmRemoveTable = () => { | ||
setDialog({ type: 'table', callback: removeTable }); | ||
}; | ||
|
||
const removeTable = () => { | ||
resetValue(); | ||
setDialog(null); | ||
}; | ||
|
||
const addRows = (count: number = 1) => { | ||
const newValue = { ...value }; | ||
// Calculate the column count from the first row | ||
const columnCount = value.rows[0].cells.length; | ||
for (let i = 0; i < count; i++) { | ||
// Add as many cells as we have columns | ||
newValue.rows.push({ | ||
_type: config.rowType, | ||
_key: randomKey(), | ||
cells: Array(columnCount).fill(''), | ||
}); | ||
} | ||
return updateValue(newValue); | ||
}; | ||
|
||
const removeRow = (index: number) => { | ||
const newValue = deepClone(value); | ||
newValue.rows.splice(index, 1); | ||
updateValue(newValue); | ||
setDialog(null); | ||
}; | ||
|
||
const confirmRemoveRow = (index: number) => { | ||
if (value.rows.length <= 1) return confirmRemoveTable(); | ||
return setDialog({ type: 'row', callback: () => removeRow(index) }); | ||
}; | ||
|
||
const confirmRemoveColumn = (index: number) => { | ||
if (value.rows[0].cells.length <= 1) return confirmRemoveTable(); | ||
return setDialog({ type: 'column', callback: () => removeColumn(index) }); | ||
}; | ||
|
||
const addColumns = (count: number) => { | ||
const newValue = deepClone(value); | ||
// Add a cell to each of the rows | ||
newValue.rows.forEach((_, i) => { | ||
for (let j = 0; j < count; j++) { | ||
newValue.rows[i].cells.push(''); | ||
} | ||
}); | ||
return updateValue(newValue); | ||
}; | ||
|
||
const removeColumn = (index) => { | ||
const newValue = deepClone(value); | ||
newValue.rows.forEach((row) => { | ||
row.cells.splice(index, 1); | ||
}); | ||
updateValue(newValue); | ||
setDialog(null); | ||
}; | ||
|
||
const updateCell = (e, rowIndex, cellIndex) => { | ||
const newValue = deepClone(value); | ||
newValue.rows[rowIndex].cells[cellIndex] = e.target.value; | ||
return updateValue(newValue); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{dialog && ( | ||
<Dialog | ||
header={`Remove ${dialog.type}`} | ||
id="dialog-remove" | ||
onClose={() => setDialog(null)} | ||
zOffset={1000} | ||
> | ||
<Card padding={4}> | ||
<Text>Are you sure you want to remove this {dialog.type}?</Text> | ||
<Box marginTop={4}> | ||
<Inline space={1} style={{ textAlign: 'right' }}> | ||
<Button | ||
text="Cancel" | ||
mode="ghost" | ||
onClick={() => setDialog(null)} | ||
/> | ||
<Button | ||
text="Confirm" | ||
tone="critical" | ||
onClick={() => dialog.callback()} | ||
/> | ||
</Inline> | ||
</Box> | ||
</Card> | ||
</Dialog> | ||
)} | ||
<Flex align="flex-start" justify="space-between"> | ||
<FormField | ||
label={type.title} | ||
markers={markers} | ||
description={type.description} | ||
level={level} | ||
__unstable_changeIndicator={false} | ||
/> | ||
{value?.rows?.length && ( | ||
<TableMenu | ||
addColumns={addColumns} | ||
addRows={addRows} | ||
remove={confirmRemoveTable} | ||
placement="left" | ||
/> | ||
)} | ||
</Flex> | ||
{value?.rows?.length && <TableInput | ||
rows={value.rows} | ||
removeRow={confirmRemoveRow} | ||
removeColumn={confirmRemoveColumn} | ||
updateCell={updateCell} | ||
/>} | ||
{(!value || !value?.rows?.length) && ( | ||
<TableControl create={createTable} /> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default forwardRef((props: RootProps, ref) => ( | ||
<TableComponent {...props} /> | ||
)); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.