Skip to content

Commit ee9e4ab

Browse files
committed
developing
1 parent 93a9449 commit ee9e4ab

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

src/components/Widgets/ReactTableWidget.jsx

+54-30
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ const EditableCell = ({
3838
}) => {
3939
// We need to keep and update the state of the cell normally
4040
const [value, setValue] = React.useState(initialValue);
41+
const [selected, setSelected] = React.useState(false);
4142

4243
const onChange = (e) => {
4344
setValue(e.target.value);
4445
};
4546

4647
// We'll only update the external data when the input is blurred
4748
const onBlur = () => {
49+
setSelected(false);
4850
updateMyData(index, id, value);
4951
};
5052

@@ -53,7 +55,19 @@ const EditableCell = ({
5355
setValue(initialValue);
5456
}, [initialValue]);
5557

56-
return <input value={value} onChange={onChange} onBlur={onBlur} />;
58+
return selected ? (
59+
// eslint-disable-next-line jsx-a11y/no-autofocus
60+
<input autoFocus value={value} onChange={onChange} onBlur={onBlur} />
61+
) : (
62+
<span
63+
role="button"
64+
tabIndex={0}
65+
onClick={() => setSelected(true)}
66+
onKeyDown={() => setSelected(true)}
67+
>
68+
{value}
69+
</span>
70+
);
5771
};
5872

5973
const defaultColumn = {
@@ -156,6 +170,7 @@ function Table({ columns, data, updateMyData, skipPageReset }) {
156170
style={{ width: '100px' }}
157171
/>
158172
</span>{' '}
173+
{/* eslint-disable-next-line jsx-a11y/no-onchange */}
159174
<select
160175
value={pageSize}
161176
onChange={(e) => {
@@ -173,14 +188,30 @@ function Table({ columns, data, updateMyData, skipPageReset }) {
173188
);
174189
}
175190

176-
function ReactDataTableWidget(props) {
191+
const ReactDataTableWidget = (props) => {
177192
// Set our editable cell renderer as the default Cell renderer
178-
let { columns, items, csvexport, csvimport } = props;
193+
let { schema, value, onChange, id, csvexport, csvimport } = props;
179194

180195
const intl = useIntl();
181-
const tablecolumns = React.useMemo(() => columns, [columns]);
196+
const header_columns = schema.fieldsets[0].fields.map((field) => {
197+
return { Header: schema.properties[field].title, accessor: field };
198+
});
199+
200+
// Set our editable cell renderer as the default Cell renderer
201+
const tablecolumns = React.useMemo(
202+
() => [
203+
{
204+
Header: 'React table header',
205+
columns: header_columns,
206+
},
207+
],
208+
// eslint-disable-next-line react-hooks/exhaustive-deps
209+
[],
210+
);
182211

183-
const [data, setData] = React.useState(() => items);
212+
// const tablecolumns = React.useMemo(() => columns, [columns]);
213+
214+
const [data, setData] = React.useState(() => value);
184215
const [originalData] = React.useState(data);
185216
const [skipPageReset, setSkipPageReset] = React.useState(false);
186217

@@ -190,31 +221,24 @@ function ReactDataTableWidget(props) {
190221
// When our cell renderer calls updateMyData, we'll use
191222
// the rowIndex, columnId and new value to update the
192223
// original data
193-
const updateMyData = (rowIndex, columnId, value) => {
224+
const updateMyData = (rowIndex, columnId, updateValue) => {
194225
// We also turn on the flag to not reset the page
195226
setSkipPageReset(true);
196227
setData((old) =>
197228
old.map((row, index) => {
198229
if (index === rowIndex) {
199230
return {
200231
...old[rowIndex],
201-
[columnId]: value,
232+
[columnId]: updateValue,
202233
};
203234
}
204235
return row;
205236
}),
206237
);
207-
208-
// return items back to the component
209-
items = items.map((row, index) => {
210-
if (index === rowIndex) {
211-
return {
212-
...items[rowIndex],
213-
[columnId]: value,
214-
};
215-
}
216-
return row;
217-
});
238+
const newvalue = value.map((v, i) =>
239+
i !== rowIndex ? v : { ...v, [columnId]: updateValue },
240+
);
241+
onChange(id, newvalue);
218242
};
219243

220244
// After data chagnes, we turn the flag back off
@@ -278,21 +302,21 @@ function ReactDataTableWidget(props) {
278302
let modifiedcount = newdata.length - newdatacount;
279303

280304
setData(newdata);
281-
props.value.items = newdata;
305+
onChange(id, newdata);
282306
toast.success(
283307
<Toast
284308
success
285309
autoClose={5000}
286-
content={
287-
(intl.formatMessage(messages.csv_file_imported_correctly) +
288-
' ',
289-
+intl.formatMessage(messages.import_new_imported_item_count, {
310+
content={`${intl.formatMessage(
311+
messages.csv_file_imported_correctly,
312+
)} ${intl.formatMessage(
313+
messages.import_new_imported_item_count,
314+
{
290315
count: newdatacount,
291-
}) + ' ',
292-
+intl.formatMessage(messages.import_modified_item_count, {
293-
count: modifiedcount,
294-
}))
295-
}
316+
},
317+
)} ${intl.formatMessage(messages.import_modified_item_count, {
318+
count: modifiedcount,
319+
})}`}
296320
/>,
297321
);
298322
}}
@@ -307,7 +331,7 @@ function ReactDataTableWidget(props) {
307331
<>
308332
<div>
309333
<button type="button" {...getRootProps()}>
310-
intl.formatMessage(messages.import_csv_file)
334+
{intl.formatMessage(messages.import_csv_file)}
311335
</button>
312336
<div>{acceptedFile && acceptedFile.name}</div>
313337
<button {...getRemoveFileProps()}>Remove</button>
@@ -326,6 +350,6 @@ function ReactDataTableWidget(props) {
326350
/>
327351
</>
328352
);
329-
}
353+
};
330354

331355
export default ReactDataTableWidget;

0 commit comments

Comments
 (0)