Skip to content

Commit 21a1923

Browse files
committed
Add possible null and undefined as classes to createElement
1 parent c1e5779 commit 21a1923

5 files changed

Lines changed: 19 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
v1.0.10
5+
-------
6+
7+
* Add possible pass classes as `null` or `undefined` to `createElement`.
8+
49
v1.0.9
510
------
611

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fivelab/web-utils",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"private": false,
55
"type": "module",
66
"description": "The helpers for easy manipulate with dom.",

src/dom/create-element.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type CommonOptions = {
1212
};
1313

1414
export type BaseOptions = CommonOptions & {
15-
classList?: string[];
15+
classList?: Array<string | null | undefined>;
1616
style?: Partial<CSSStyleDeclaration>;
1717
} & ContentOptions;
1818

@@ -118,7 +118,8 @@ export function createElement(tag: string, options: BaseOptions = {}): HTMLEleme
118118
}
119119

120120
if (options.classList && options.classList.length) {
121-
el.classList.add(...options.classList);
121+
const classes = options.classList.filter((e): e is string => Boolean(e));
122+
el.classList.add(...classes);
122123
}
123124

124125
if (options.dataset) {

tests/dom/create-element.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ describe('create element', () => {
1313
expect([...el.classList]).toEqual(expect.arrayContaining(['a', 'b']));
1414
});
1515

16+
it('creates element with null and undefined classes', () => {
17+
const el = createElement('div', {
18+
classList: ['a', null, 'c', undefined, 'b']
19+
});
20+
21+
expect([...el.classList]).toEqual(expect.arrayContaining(['a', 'c', 'b']));
22+
});
23+
1624
it('sets textContent (including empty string)', () => {
1725
const el1 = createElement('span', { text: 'hello' });
1826
expect(el1.textContent).toBe('hello');

0 commit comments

Comments
 (0)