1
0
Fork 0
mirror of synced 2024-10-05 12:34:50 +13:00

Remove unnecessary save on first keypress in grids

This commit is contained in:
Andrew Kingston 2023-08-17 12:08:50 +01:00
parent e7c4ebea39
commit 733a638a99
4 changed files with 21 additions and 9 deletions

View file

@ -58,9 +58,14 @@
isReadonly: () => readonly, isReadonly: () => readonly,
getType: () => column.schema.type, getType: () => column.schema.type,
getValue: () => row[column.name], getValue: () => row[column.name],
setValue: value => { setValue: (value, options = { save: true }) => {
validation.actions.setError(cellId, null) validation.actions.setError(cellId, null)
updateValue(row._id, column.name, value) updateValue({
rowId: row._id,
column: column.name,
value,
save: options?.save,
})
}, },
} }
</script> </script>

View file

@ -120,8 +120,8 @@
document.addEventListener("keydown", handleKeyPress) document.addEventListener("keydown", handleKeyPress)
} }
const updateValue = (rowId, columnName, val) => { const updateValue = ({ column, value }) => {
newRow[columnName] = val newRow[column] = value
} }
const addViaModal = () => { const addViaModal = () => {

View file

@ -215,13 +215,15 @@
if ($focusedCellAPI && !$focusedCellAPI.isReadonly()) { if ($focusedCellAPI && !$focusedCellAPI.isReadonly()) {
const type = $focusedCellAPI.getType() const type = $focusedCellAPI.getType()
if (type === "number" && keyCodeIsNumber(keyCode)) { if (type === "number" && keyCodeIsNumber(keyCode)) {
$focusedCellAPI.setValue(parseInt(key)) // Update the value locally but don't save it yet
$focusedCellAPI.setValue(parseInt(key), { save: false })
$focusedCellAPI.focus() $focusedCellAPI.focus()
} else if ( } else if (
["string", "barcodeqr", "longform"].includes(type) && ["string", "barcodeqr", "longform"].includes(type) &&
(keyCodeIsLetter(keyCode) || keyCodeIsNumber(keyCode)) (keyCodeIsLetter(keyCode) || keyCodeIsNumber(keyCode))
) { ) {
$focusedCellAPI.setValue(key) // Update the value locally but don't save it yet
$focusedCellAPI.setValue(key, { save: false })
$focusedCellAPI.focus() $focusedCellAPI.focus()
} }
} }

View file

@ -311,7 +311,7 @@ export const createActions = context => {
} }
// Patches a row with some changes // Patches a row with some changes
const updateRow = async (rowId, changes) => { const updateRow = async (rowId, changes, options = { save: true }) => {
const $rows = get(rows) const $rows = get(rows)
const $rowLookupMap = get(rowLookupMap) const $rowLookupMap = get(rowLookupMap)
const index = $rowLookupMap[rowId] const index = $rowLookupMap[rowId]
@ -341,6 +341,11 @@ export const createActions = context => {
}, },
})) }))
// Stop here if we don't want to persist the change
if (!options?.save) {
return
}
// Save change // Save change
try { try {
inProgressChanges.update(state => ({ inProgressChanges.update(state => ({
@ -378,8 +383,8 @@ export const createActions = context => {
} }
// Updates a value of a row // Updates a value of a row
const updateValue = async (rowId, column, value) => { const updateValue = async ({ rowId, column, value, save = true }) => {
return await updateRow(rowId, { [column]: value }) return await updateRow(rowId, { [column]: value }, { save })
} }
// Deletes an array of rows // Deletes an array of rows