Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit b9b26b5

Browse files
committed
fix checkbox
1 parent 85fee4e commit b9b26b5

File tree

4 files changed

+76
-76
lines changed

4 files changed

+76
-76
lines changed

src/cdm/TableStateInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface DataState {
3737
rows: RowDataType[];
3838
actions: {
3939
addRow: (filename: string, columns: TableColumn[], ddbbConfig: LocalSettings) => void;
40-
updateCell: (rowIndex: number, column: TableColumn, value: Literal, columns: TableColumn[], ddbbConfig: LocalSettings, isMovingFile?: boolean) => void;
40+
updateCell: (rowIndex: number, column: TableColumn, value: Literal, columns: TableColumn[], ddbbConfig: LocalSettings, isMovingFile?: boolean) => Promise<void>;
4141
parseDataOfColumn: (column: TableColumn, input: string, ddbbConfig: LocalSettings) => void;
4242
updateDataAfterLabelChange: (column: TableColumn, label: string, columns: TableColumn[], ddbbConfig: LocalSettings) => Promise<void>;
4343
removeRow: (row: RowDataType) => Promise<void>;

src/components/cellTypes/CheckboxCell.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
import React, { useState } from "react";
1+
import React from "react";
2+
import shallow from "zustand/shallow";
23
import { TableColumn } from "cdm/FolderModel";
34
import { c } from "helpers/StylesHelper";
45
import { CellComponentProps } from "cdm/ComponentsModel";
56

67
function CheckboxCell(props: CellComponentProps) {
78
const { defaultCell } = props;
89
const { row, column, table } = defaultCell;
9-
const tableColumn = column.columnDef as TableColumn;
10+
const { tableState } = table.options.meta;
1011

11-
const dataActions = table.options.meta.tableState.data(
12-
(state) => state.actions
13-
);
12+
const dataActions = tableState.data((state) => state.actions);
1413

15-
const checkboxCell = table.options.meta.tableState.data(
16-
(state) => state.rows[row.index]
14+
const checkboxRow = tableState.data(
15+
(state) => state.rows[row.index],
16+
shallow
1717
);
1818

19-
const columnsInfo = table.options.meta.tableState.columns(
20-
(state) => state.info
21-
);
19+
const columnsInfo = tableState.columns((state) => state.info);
2220

23-
const configInfo = table.options.meta.tableState.configState(
24-
(state) => state.info
25-
);
21+
const configInfo = tableState.configState((state) => state.info);
2622

2723
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
28-
const newValue = event.target.checked ? 1 : 0;
24+
const newValue = event.target.checked;
2925
// save on disk
3026
dataActions.updateCell(
3127
row.index,
@@ -35,12 +31,12 @@ function CheckboxCell(props: CellComponentProps) {
3531
configInfo.getLocalSettings()
3632
);
3733
};
38-
3934
return (
40-
<div className={`${c("checkbox")}`}>
35+
<div key={`checkbox-div-${row.index}`} className={`${c("checkbox")}`}>
4136
<input
4237
type="checkbox"
43-
checked={Boolean(checkboxCell[tableColumn.key])}
38+
checked={checkboxRow[column.id] as boolean}
39+
key={`checkbox-input-${row.index}`}
4440
onChange={handleChange}
4541
/>
4642
</div>

src/components/cellTypes/NumberCell.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { CellComponentProps } from "cdm/ComponentsModel";
22
import { TableColumn } from "cdm/FolderModel";
3-
import { Literal } from "obsidian-dataview";
43
import React, {
54
ChangeEventHandler,
65
KeyboardEventHandler,
7-
useEffect,
86
useState,
97
} from "react";
108

src/stateManagement/data/handlers/UpdateCellHandlerAction.ts

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,79 @@ import { AbstractTableAction } from "stateManagement/AbstractTableAction";
1010

1111
export default class UpdateCellHandlerAction extends AbstractTableAction<DataState> {
1212
handle(tableActionResponse: TableActionResponse<DataState>): TableActionResponse<DataState> {
13-
const { view, set, implementation } = tableActionResponse;
14-
implementation.actions.updateCell = (
13+
const { view, set, get, implementation } = tableActionResponse;
14+
implementation.actions.updateCell = async (
1515
rowIndex: number,
1616
column: TableColumn,
1717
value: Literal,
1818
columns: TableColumn[],
1919
ddbbConfig: LocalSettings,
20-
isMovingFile?: boolean) => set((state) => {
21-
const modifiedRow = state.rows[rowIndex];
22-
let rowTFile = modifiedRow.__note__.getFile();
20+
isMovingFile?: boolean) => {
21+
const modifiedRow = get().rows[rowIndex];
22+
let rowTFile = modifiedRow.__note__.getFile();
2323

24-
// Update the row on memory
25-
modifiedRow[column.key] = value;
24+
// Update the row on memory
25+
modifiedRow[column.key] = value;
2626

27-
// Row Rules
28-
if (ddbbConfig.show_metadata_modified) {
29-
modifiedRow[MetadataColumns.MODIFIED] = DateTime.now();
30-
}
31-
32-
// Update the row on disk
33-
if (isMovingFile && ddbbConfig.group_folder_column === column.id) {
27+
// Row Rules
28+
if (ddbbConfig.show_metadata_modified) {
29+
modifiedRow[MetadataColumns.MODIFIED] = DateTime.now();
30+
}
31+
// Update the row on disk
32+
if (isMovingFile && ddbbConfig.group_folder_column === column.id) {
3433

35-
const moveInfo = {
36-
file: rowTFile,
37-
id: column.id,
38-
value: value,
39-
columns: columns,
40-
ddbbConfig: ddbbConfig,
41-
}
42-
moveFile(`${view.file.parent.path}/${value}`, moveInfo);
43-
// Update row file
44-
modifiedRow[
45-
MetadataColumns.FILE
46-
] = `[[${view.file.parent.path}/${value}/${rowTFile.name}|${rowTFile.basename}]]`;
47-
// Check if action.value is a valid folder name
48-
const auxPath =
49-
value !== ""
50-
? `${view.file.parent.path}/${value}/${rowTFile.name}`
51-
: `${view.file.parent.path}/${rowTFile.name}`;
34+
const moveInfo = {
35+
file: rowTFile,
36+
id: column.id,
37+
value: value,
38+
columns: columns,
39+
ddbbConfig: ddbbConfig,
40+
}
41+
await moveFile(`${view.file.parent.path}/${value}`, moveInfo);
42+
// Update row file
43+
modifiedRow[
44+
MetadataColumns.FILE
45+
] = `[[${view.file.parent.path}/${value}/${rowTFile.name}|${rowTFile.basename}]]`;
46+
// Check if action.value is a valid folder name
47+
const auxPath =
48+
value !== ""
49+
? `${view.file.parent.path}/${value}/${rowTFile.name}`
50+
: `${view.file.parent.path}/${rowTFile.name}`;
5251

53-
const recordRow: Record<string, Literal> = {};
54-
Object.entries(modifiedRow).forEach(([key, value]) => {
55-
recordRow[key] = value as Literal;
56-
});
52+
const recordRow: Record<string, Literal> = {};
53+
Object.entries(modifiedRow).forEach(([key, value]) => {
54+
recordRow[key] = value as Literal;
55+
});
5756

58-
modifiedRow.__note__ = new NoteInfo({
59-
...recordRow,
60-
file: {
61-
path: auxPath,
62-
},
63-
});
57+
modifiedRow.__note__ = new NoteInfo({
58+
...recordRow,
59+
file: {
60+
path: auxPath,
61+
},
62+
});
6463

65-
} else {
66-
// Save on disk
67-
updateRowFileProxy(
68-
rowTFile,
69-
column.id,
70-
value,
71-
columns,
72-
ddbbConfig,
73-
UpdateRowOptions.COLUMN_VALUE
74-
);
75-
}
76-
// Update rows without re render
77-
state.rows[rowIndex] = modifiedRow;
78-
return { rows: state.rows };
79-
});
64+
} else {
65+
// Save on disk
66+
await updateRowFileProxy(
67+
rowTFile,
68+
column.id,
69+
value,
70+
columns,
71+
ddbbConfig,
72+
UpdateRowOptions.COLUMN_VALUE
73+
);
74+
}
75+
set((state) => {
76+
// Save on memory
77+
return {
78+
rows: [
79+
...state.rows.slice(0, rowIndex),
80+
modifiedRow,
81+
...state.rows.slice(rowIndex + 1),
82+
]
83+
};
84+
})
85+
};
8086
tableActionResponse.implementation = implementation;
8187
return this.goNext(tableActionResponse);
8288
}

0 commit comments

Comments
 (0)