Skip to content

Commit 134ae6a

Browse files
authored
Merge pull request #3161 from adumesny/master
React removeWidget()
2 parents 1c2ca9d + 8b3be43 commit 134ae6a

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

react/lib/grid-stack-provider.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ export function GridStackProvider({
7272

7373
const removeWidget = useCallback(
7474
(id: string) => {
75-
const element = document.body.querySelector(`[gs-id="${id}"]`);
76-
77-
if (element) {
78-
gridStack?.removeWidget(element as GridItemHTMLElement);
79-
}
75+
const element = document.body.querySelector<GridItemHTMLElement>(`[gs-id="${id}"]`);
76+
if (element) gridStack?.removeWidget(element);
8077

8178
setRawWidgetMetaMap((prev) => {
8279
const newMap = new Map<string, GridStackWidget>(prev);

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ export interface GridItemHTMLElement extends HTMLElement {
8282

8383
/**
8484
* Type representing various ways to specify grid elements.
85-
* Can be a CSS selector string, HTMLElement, or GridItemHTMLElement.
85+
* Can be a CSS selector string, GridItemHTMLElement (HTML element with GS variables when loaded).
8686
*/
87-
export type GridStackElement = string | HTMLElement | GridItemHTMLElement;
87+
export type GridStackElement = string | GridItemHTMLElement;
8888

8989
/**
9090
* Event handler function types for the .on() method.

src/utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,17 @@ export class Utils {
123123

124124
let list = root.querySelectorAll(els);
125125
if (!list.length && els[0] !== '.' && els[0] !== '#') {
126+
// see if mean to be a class
126127
list = root.querySelectorAll('.' + els);
127-
if (!list.length) { list = root.querySelectorAll('#' + els) }
128+
129+
// else if mean to be an id
130+
if (!list.length) list = root.querySelectorAll('#' + els);
131+
132+
// else see if gs-id attribute
133+
if (!list.length) {
134+
const el = root.querySelector<HTMLElement>(`[gs-id="${els}"]`);
135+
return el ? [el] : [];
136+
}
128137
}
129138
return Array.from(list) as HTMLElement[];
130139
}

0 commit comments

Comments
 (0)