fixed default values

This commit is contained in:
Anish Roy
2023-04-17 21:25:10 +05:30
parent e802db5725
commit 83080d267b
2 changed files with 19 additions and 6 deletions

View File

@@ -28,6 +28,7 @@ import {
tableSortsAtom,
addRowAtom,
_updateRowDbAtom,
tableColumnsOrderedAtom,
} from "@src/atoms/tableScope";
export default function AddRow() {
@@ -215,10 +216,22 @@ export function AddRowArraySubTable() {
const anchorEl = useRef<HTMLDivElement>(null);
const [addRowAt, setAddNewRowAt] = useState<"top" | "bottom">("bottom");
const [tableColumnsOrdered] = useAtom(tableColumnsOrderedAtom, tableScope);
if (!updateRowDb) return null;
const handleClick = () => {
updateRowDb("", {}, undefined, {
const initialValues: Record<string, any> = {};
// Set initial values based on default values
for (const column of tableColumnsOrdered) {
if (column.config?.defaultValue?.type === "static")
initialValues[column.key] = column.config.defaultValue.value!;
else if (column.config?.defaultValue?.type === "null")
initialValues[column.key] = null;
}
updateRowDb("", initialValues, undefined, {
index: 0,
operation: {
addRow: addRowAt,

View File

@@ -260,9 +260,9 @@ export function useFirestoreDocAsCollectionWithAtom<T = TableRow>(
const addNewRow = (addTo: "top" | "bottom", base?: TableRow) => {
let temp: T[] = [];
const newRow = (i: number) =>
({
...base,
const newRow = (i: number) => {
return {
...(base ?? update),
_rowy_ref: {
id: doc(firebaseDb, path).id,
path: doc(firebaseDb, path).path,
@@ -271,8 +271,8 @@ export function useFirestoreDocAsCollectionWithAtom<T = TableRow>(
parentField: fieldName,
},
},
} as T);
} as T;
};
setDataAtom((prevData) => {
temp = unsortRows(prevData);