Make cell selection and row selection mutually exclusive

This commit is contained in:
Andrew Kingston 2024-06-20 14:04:32 +01:00
parent 0f3decd127
commit 93e222036e
No known key found for this signature in database
2 changed files with 37 additions and 32 deletions

View File

@ -134,12 +134,6 @@
case "Enter": case "Enter":
focusCell() focusCell()
break break
case " ":
case "Space":
if ($config.canDeleteRows) {
toggleSelectRow()
}
break
default: default:
startEnteringValue(e.key, e.which) startEnteringValue(e.key, e.which)
} }

View File

@ -32,20 +32,6 @@ export const createStores = context => {
null null
) )
// Toggles whether a certain row ID is selected or not
const toggleSelectedRow = id => {
selectedRows.update(state => {
let newState = {
...state,
[id]: !state[id],
}
if (!newState[id]) {
delete newState[id]
}
return newState
})
}
return { return {
focusedCellId, focusedCellId,
focusedCellAPI, focusedCellAPI,
@ -58,12 +44,7 @@ export const createStores = context => {
keyboardBlocked, keyboardBlocked,
isDragging, isDragging,
buttonColumnWidth, buttonColumnWidth,
selectedRows: { selectedRows,
...selectedRows,
actions: {
toggleRow: toggleSelectedRow,
},
},
} }
} }
@ -112,7 +93,7 @@ export const deriveStores = context => {
} }
export const createActions = context => { export const createActions = context => {
const { focusedCellId, hoveredRowId } = context const { focusedCellId, hoveredRowId, selectedRows } = context
// Callback when leaving the grid, deselecting all focussed or selected items // Callback when leaving the grid, deselecting all focussed or selected items
const blur = () => { const blur = () => {
@ -120,12 +101,32 @@ export const createActions = context => {
hoveredRowId.set(null) hoveredRowId.set(null)
} }
// Toggles whether a certain row ID is selected or not
const toggleSelectedRow = id => {
selectedRows.update(state => {
let newState = {
...state,
[id]: !state[id],
}
if (!newState[id]) {
delete newState[id]
}
return newState
})
}
return { return {
ui: { ui: {
actions: { actions: {
blur, blur,
}, },
}, },
selectedRows: {
...selectedRows,
actions: {
toggleRow: toggleSelectedRow,
},
},
} }
} }
@ -186,18 +187,21 @@ export const initialise = context => {
lastFocusedRowId = id lastFocusedRowId = id
}) })
// Remember the last focused cell ID so that we can store the previous one
let lastFocusedCellId = null let lastFocusedCellId = null
focusedCellId.subscribe(id => { focusedCellId.subscribe(id => {
// Remember the last focused cell ID so that we can store the previous one
previousFocusedCellId.set(lastFocusedCellId) previousFocusedCellId.set(lastFocusedCellId)
lastFocusedCellId = id lastFocusedCellId = id
})
// Remove hovered row when a cell is selected // Remove hovered row when a cell is selected
focusedCellId.subscribe(cell => { if (id && get(hoveredRowId)) {
if (cell && get(hoveredRowId)) {
hoveredRowId.set(null) hoveredRowId.set(null)
} }
// Clear row selection when focusing a cell
if (id && Object.keys(get(selectedRows)).length) {
selectedRows.set({})
}
}) })
// Pull row height from table as long as we don't have a fixed height // Pull row height from table as long as we don't have a fixed height
@ -215,4 +219,11 @@ export const initialise = context => {
rowHeight.set(get(definition)?.rowHeight || DefaultRowHeight) rowHeight.set(get(definition)?.rowHeight || DefaultRowHeight)
} }
}) })
// Clear focused cell when selecting rows
selectedRows.subscribe(rows => {
if (get(focusedCellId) && Object.keys(rows).length) {
focusedCellId.set(null)
}
})
} }