-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
BC-7808 - implement key changing to force the component to re-rendering
- Loading branch information
1 parent
07db2fb
commit 594e27a
Showing
8 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { reactive } from "vue"; | ||
|
||
const renderKeyList = reactive<Record<string, number>>({}); | ||
|
||
const getMaxRenderKey = () => { | ||
return Object.values(renderKeyList).reduce((a, b) => Math.max(a, b), 0); | ||
}; | ||
|
||
/* | ||
* This artificial generating renderKey forces a component to re-render by changing the element key. | ||
* It is used to fix issue described on https://ticketsystem.dbildungscloud.de/browse/BC-7806 | ||
* | ||
* This solution is based on the article on https://michaelnthiessen.com/force-re-render/ | ||
*/ | ||
export const useForceRender = (id: string) => { | ||
const generateRenderKey = () => { | ||
renderKeyList[id] = getMaxRenderKey() + 1; | ||
}; | ||
|
||
const getRenderKey = (): number => { | ||
if (!renderKeyList[id]) generateRenderKey(); | ||
return renderKeyList[id]; | ||
}; | ||
|
||
return { | ||
generateRenderKey, | ||
getRenderKey, | ||
}; | ||
}; |
16 changes: 16 additions & 0 deletions
16
src/modules/data/board/fixSamePositionDnD.composable.unit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useForceRender } from "./fixSamePositionDnD.composable"; | ||
|
||
describe("fixSamePositionDnD", () => { | ||
it("should set render key to the list", () => { | ||
const { getRenderKey } = useForceRender("test-id-#1"); | ||
const renderKey = getRenderKey(); | ||
expect(renderKey).toBe(1); | ||
}); | ||
|
||
it('should increase the render key when "generateRenderKey" is called', () => { | ||
const { generateRenderKey, getRenderKey } = useForceRender("test-id-#1"); | ||
expect(getRenderKey()).toBe(1); | ||
generateRenderKey(); | ||
expect(getRenderKey()).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters