Skip to content

Commit

Permalink
create focusPrevious composable
Browse files Browse the repository at this point in the history
  • Loading branch information
muratmerdoglu-dp committed Sep 16, 2024
1 parent eaa15da commit 248e4d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/modules/data/board/Board.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
UpdateColumnTitleSuccessPayload,
} from "./boardActions/boardActionPayload";
import { DeleteCardSuccessPayload } from "./cardActions/cardActionPayload";
import { useSetFocusPrevios } from "./focusPrevios.composable";

export const useBoardStore = defineStore("boardStore", () => {
const cardStore = useCardStore();
Expand Down Expand Up @@ -131,9 +132,17 @@ export const useBoardStore = defineStore("boardStore", () => {
const deleteCardSuccess = (payload: DeleteCardSuccessPayload) => {
if (!board.value) return;
const cardId = payload.cardId;

const columnIndex = board.value.columns.findIndex(
(column) => column.cards.find((c) => c.cardId === cardId) !== undefined
);

if (!payload.isOwnAction) {
const { focusedId } = useBoardFocusHandler(cardId);
if (focusedId?.value !== cardId) return;
useSetFocusPrevios(cardId, "card");
}

if (columnIndex !== -1) {
const cardIndex = board.value.columns[columnIndex].cards.findIndex(
(c) => c.cardId === cardId
Expand Down
38 changes: 38 additions & 0 deletions src/modules/data/board/focusPrevios.composable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useBoardStore, useBoardFocusHandler } from "@data-board";

type Level = "board" | "column" | "card" | "element";

export const useSetFocusPrevios = async (id: string, level: Level) => {
console.log(level);
const { board } = useBoardStore();

if (!board) return;
const { setFocus } = useBoardFocusHandler();

// if it's a card, find the column index
const columnIndex = board.columns.findIndex(
(column) => column.cards.find((c) => c.cardId === id) !== undefined
);

const cardIndex = board.columns[columnIndex].cards.findIndex(
(c) => c.cardId === id
);

if (cardIndex > 0) {
const previousCard = board.columns[columnIndex].cards[cardIndex - 1].cardId;
setFocus(previousCard);

const element = document.querySelector(
`[data-card-id="${previousCard}"]`
) as HTMLElement;

element?.setAttribute("tabindex", "0");
element?.dispatchEvent(
new KeyboardEvent("keypress", {
key: "Tab",
})
);

element?.focus();
}
};

0 comments on commit 248e4d1

Please sign in to comment.