Skip to content

Commit

Permalink
update row yMap onBlur
Browse files Browse the repository at this point in the history
  • Loading branch information
NGimbal committed Mar 9, 2021
1 parent 9d3353a commit 240680e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 102 deletions.
15 changes: 3 additions & 12 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ const Styles = styled.div`
}
`

// const ydoc = new Y.Doc()

// const wsProvider = new WebsocketProvider('ws://localhost:1234', 'my-roomname', ydoc)

// wsProvider.on('status', event => {
// window.alert(event.status)
// console.log(event.status) // logs "connected" or "disconnected"
// })

const blankRow = {
format: 'lala',
divNum: '1235',
Expand All @@ -67,7 +58,7 @@ const blankRow = {

const connectDoc = (doc) => {
console.log('connect to a provider with room', doc.guid)
const wsProvider = new WebsocketProvider('ws://localhost:1234', 'my-room2', doc)
const wsProvider = new WebsocketProvider('ws://localhost:1234', 'my-room3', doc)

wsProvider.on('status', event => {
// window.alert(event.status)
Expand All @@ -80,7 +71,7 @@ const connectDoc = (doc) => {
function App() {

const ydoc = useYDoc('docguid', connectDoc)
const {data: table, push: yPush, insert: yInsert} = useYArray(ydoc.getArray('table1'))
const {data, push: yPush, insert: yInsert} = useYArray(ydoc.getArray('table1'))

const columns = React.useMemo(() => [
{
Expand Down Expand Up @@ -148,7 +139,7 @@ function App() {
return (
<div className="App" style={{width:250}}>
<Styles>
<DataTable columns={columns} table={table}/>
<DataTable columns={columns} data={data}/>
</Styles>
<input type="button" value="+" onClick={addRow}/>
<ImportCSV doc={ydoc} yPush={yPush} yInsert={yInsert}/>
Expand Down
106 changes: 16 additions & 90 deletions src/Components/DataTable/DataTable.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,38 @@
import React from 'react'
import React, { useEffect } from 'react'
import styled from 'styled-components'
import { useTable, useGroupBy, useExpanded, useRowState } from 'react-table'
import { useYDoc, useYArray, useYMap } from 'zustand-yjs'

// TODO:
// Cell is rendered normally, except when click, turns to editable cell
//

// Create an editable cell renderer
// const EditableCell = ({
// // value: initialValue,
// row: { original },
// // row: { index },
// column: { id },
// }) => {

// // Keep and update the state of the cell
// const { set, data } = useYMap(original)

// const onChange = e => {
// console.log("onChange")
// // setValue(e.target.value)
// set(id, `${e.target.value}`)
// }

// // We'll only update the external data when the input is blurred
// // const onBlur = () => {
// // console.log("blur")
// // }

// return <input value={data[id]} onChange={onChange} />
// }

const SimpleEditableCell = ({
value: initialValue,
set: set,
column: { id },
}) => {

const [val, setVal] = React.useState(initialValue)

// Keep and update the state of the cell
// const { set, data } = useYMap(original)
useEffect(() => {
setVal(initialValue)
},[initialValue])

const onChange = e => {
console.log("onChange")
// setValue(e.target.value)
set(id, `${e.target.value}`)
setVal(`${e.target.value}`)
}

// We'll only update the external data when the input is blurred
// const onBlur = () => {
// console.log("blur")
// }
const onBlur = e => {
set(id, `${e.target.value}`)
}

return <input value={initialValue} onChange={onChange} />
return <input value={val} onChange={onChange} onBlur={onBlur} />
}

const EditableRow = ({
// value: initialValue,
// row: { original },
row: row,
// row: { index },
// column: { id },
}) => {

// Keep and update the state of the cell
const { set, data } = useYMap(row.original)

// const onChange = e => {
// console.log("onChange")
// console.log(e)
// setValue(e.target.value)
// set(id, `${e.target.value}`)
// }

// We'll only update the external data when the input is blurred
// const onBlur = () => {
// console.log("blur")
// }

return (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
Expand Down Expand Up @@ -116,32 +71,12 @@ const EditableRow = ({
}

const GroupedRow = ({
// value: initialValue,
// row: { original },
row: row,
// row: { index },
// column: { id },
}) => {

// Keep and update the state of the cell
// const { set, data } = useYMap(row.original)

// const onChange = e => {
// console.log("onChange")
// console.log(e)
// setValue(e.target.value)
// set(id, `${e.target.value}`)
// }

// We'll only update the external data when the input is blurred
// const onBlur = () => {
// console.log("blur")
// }

return (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
// console.log(cell)
return (
<td
{...cell.getCellProps()}
Expand All @@ -164,7 +99,7 @@ const GroupedRow = ({
</>
) : cell.isAggregated ? (
cell.render('Aggregated')
) : cell.isPlaceholder? null : (
) : cell.isPlaceholder ? null : (
cell.render('Cell') // this shouldn't happen?
)
}
Expand All @@ -175,11 +110,7 @@ const GroupedRow = ({
)
}

function DataTable({ columns, table: data }) {
// const data = React.useMemo(() =>
// table.map(row => row.toJSON())
// , [table])

function DataTable({ columns, data }) {
// Use the state and functions returned from useTable to build your UI
const {
getTableProps,
Expand All @@ -195,13 +126,8 @@ function DataTable({ columns, table: data }) {
},
useGroupBy,
useExpanded,
// useRowState,
)


// console.log(data)

// Render the UI for your table
return (
<table {...getTableProps()}>
<thead>
Expand All @@ -222,14 +148,14 @@ function DataTable({ columns, table: data }) {
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
// is this going to be rly slow?

// is there a better place to do this?
if(!row.canExpand){
row.values = row.original.toJSON()
}
// console.log(prepareRow)

prepareRow(row)

// row.canExpand ? renderGrouped(row) : renderEditable(row)
return (
<>
{row.canExpand ? <GroupedRow row={row}/> : <EditableRow row={row}/>}
Expand Down

0 comments on commit 240680e

Please sign in to comment.