Skip to content

Commit 596b425

Browse files
committed
fix(tm): set proper position if tiles are added on demand
1 parent d748e2a commit 596b425

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/components/tile-manager/position.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type IgcTileManagerComponent from './tile-manager.js';
33
import type IgcTileComponent from './tile.js';
44

55
class TilesState {
6+
private _nextEmptyPosition = 0;
7+
68
public manager: IgcTileManagerComponent;
79

810
private get _tiles(): IgcTileComponent[] {
@@ -62,7 +64,11 @@ class TilesState {
6264
}
6365
}
6466
} else {
65-
tile.position = tiles.length;
67+
const positionedTiles = this._tiles.filter((tile) => tile.position > -1);
68+
tile.position =
69+
positionedTiles.length > 1
70+
? Math.max(...positionedTiles.map((tile) => tile.position)) + 1
71+
: this._nextEmptyPosition++;
6672
}
6773
}
6874

src/components/tile-manager/tile-manager.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,21 @@ describe('Tile Manager component', () => {
685685
});
686686
});
687687

688+
it('should correctly position tiles added dynamically after initialization', async () => {
689+
tileManager.replaceChildren();
690+
const tiles = Array.from(range(5)).map(() =>
691+
document.createElement(IgcTileComponent.tagName)
692+
);
693+
694+
tiles.forEach((tile) => tileManager.appendChild(tile));
695+
await elementUpdated(tileManager);
696+
697+
tileManager.tiles.forEach((tile, index) => {
698+
expect(tile.position).to.equal(index);
699+
expect(tile.style.order).to.equal(index.toString());
700+
});
701+
});
702+
688703
it('should set proper CSS order based on position', async () => {
689704
const firstTile = first(getTiles());
690705
firstTile.position = 6;

0 commit comments

Comments
 (0)