Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebfc72e

Browse files
added convenience methods style() and multiSelect()
1 parent d893ea2 commit ebfc72e

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ for multiple levels of data binding. This is because internally the `setState` m
3434
`CustomElement` also provides some convenience methods:
3535
- `show()`, show the element
3636
- `hide()`, hide the element
37-
- `select(selector: string)`, select and element from the component's Shadow DOM (calls `querySelector` internally)
37+
- `select(selector: string)`, select an element from the component's Shadow DOM (calls `querySelector` internally)
3838
- `selectAll(selector: string)`, select multiple elements from the component's Shadow DOM (calls `querySelectorAll` internally)
39+
- `style(element: HTMLElement, styles: Object)`, set multiple styles on an element at once
3940

4041
### Demo
4142
To run the demo, run `npm install` once and then `npm start` and view the demo on

src/custom-element.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export class CustomElement extends HTMLElement {
5757
return this.shadowRoot.querySelectorAll(selector);
5858
}
5959

60+
multiSelect(config) {
61+
Object.entries(config).forEach(([prop, selector]) => {
62+
this[prop] = this.select(selector);
63+
});
64+
}
65+
6066
show() {
6167
this.style.display = '';
6268
this.removeAttribute('hidden');
@@ -66,5 +72,9 @@ export class CustomElement extends HTMLElement {
6672
this.style.display = 'none';
6773
this.setAttribute('hidden', '');
6874
}
75+
76+
style(element, styles) {
77+
Object.assign(element.style, styles);
78+
}
6979
}
7080

test/custom-element.test.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('custom-element', () => {
3131
title: 'custom-element test'
3232
});
3333

34-
const actual = element.shadowRoot.querySelector('#title').textContent;
34+
const actual = element.select('#title').textContent;
3535
const expected = state.title;
3636

3737
assert.equal(actual, expected);
@@ -47,10 +47,10 @@ describe('custom-element', () => {
4747
}
4848
});
4949

50-
const actual1 = element.shadowRoot.querySelector('#name').textContent;
50+
const actual1 = element.select('#name').textContent;
5151
const expected1 = state.user.name;
5252

53-
const actual2 = element.shadowRoot.querySelector('#city').textContent;
53+
const actual2 = element.select('#city').textContent;
5454
const expected2 = state.user.address.city;
5555

5656
assert.equal(actual1, expected1);
@@ -66,7 +66,7 @@ describe('custom-element', () => {
6666

6767
element.setState(state);
6868

69-
const dataRepeater = element.shadowRoot.querySelector('data-repeater');
69+
const dataRepeater = element.select('data-repeater');
7070
const items = dataRepeater.shadowRoot.querySelectorAll('li');
7171

7272
assert.equal(dataRepeater.data, state.data.items);
@@ -85,10 +85,10 @@ describe('custom-element', () => {
8585
}
8686
});
8787

88-
const actual1 = element.shadowRoot.querySelector('#name').textContent;
88+
const actual1 = element.select('#name').textContent;
8989
const expected1 = state.user.name;
9090

91-
const actual2 = element.shadowRoot.querySelector('#city').textContent;
91+
const actual2 = element.select('#city').textContent;
9292
const expected2 = state.user.address.city;
9393

9494
assert.equal(actual1, expected1);
@@ -102,9 +102,9 @@ describe('custom-element', () => {
102102

103103
element.setState(newState);
104104

105-
const actual3 = element.shadowRoot.querySelector('#name').textContent;
105+
const actual3 = element.select('#name').textContent;
106106
const expected3 = newState.user.name;
107-
const actual4 = element.shadowRoot.querySelector('#city').textContent;
107+
const actual4 = element.select('#city').textContent;
108108

109109
assert.equal(actual3, expected3);
110110
assert.equal(actual4, expected2);
@@ -144,13 +144,13 @@ describe('custom-element', () => {
144144

145145
it('should select the correct DOM element', () => {
146146
const actual = element.select('#name');
147-
const expected = element.shadowRoot.querySelector('#name');
147+
const expected = element.select('#name');
148148

149149
assert.equal(actual, expected);
150150
});
151151

152152
it('should select the correct collection of DOM elements', () => {
153-
const actual = element.shadowRoot.querySelectorAll('p');
153+
const actual = element.selectAll('p');
154154
const expected = element.selectAll('p');
155155

156156
assert.deepEqual(actual, expected);
@@ -170,4 +170,41 @@ describe('custom-element', () => {
170170
assert.equal(element.style.display, '');
171171
assert.equal(element.hasAttribute('hidden'), false)
172172
});
173+
174+
it('should correctly set multiple styles on an element', () => {
175+
element.setState({
176+
title: 'custom-element test'
177+
});
178+
179+
const heading = element.select('h3');
180+
const styles = {
181+
color: 'red',
182+
textDecoration: 'underline',
183+
textAlign: 'center'
184+
};
185+
186+
element.style(heading, styles);
187+
188+
Object.entries(styles).forEach(([prop, style]) => {
189+
assert.equal(heading.style[prop], style);
190+
})
191+
});
192+
193+
it('should correctly select multiple elements and set these as properties on the element', () => {
194+
element.setState(state);
195+
196+
const title = element.select('#title');
197+
const name = element.select('#name');
198+
const city = element.select('#city');
199+
200+
element.multiSelect({
201+
titleObj: '#title',
202+
nameObj: '#name',
203+
cityObj: '#city'
204+
});
205+
206+
assert.deepEqual(element.titleObj, title);
207+
assert.deepEqual(element.nameObj, name);
208+
assert.deepEqual(element.cityObj, city);
209+
});
173210
});

0 commit comments

Comments
 (0)