Skip to content

Commit 2238486

Browse files
committed
Added random number component
1 parent e6372c3 commit 2238486

File tree

6 files changed

+84
-2
lines changed

6 files changed

+84
-2
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ To build the app for production, run:
3030

3131
```bash
3232
npm run build
33-
```
33+
```
34+
35+
## Stencil
36+
Based on Stencil framework
37+
38+
Website: https://stenciljs.com/
39+
40+
Github: https://github.com/ionic-team/stencil

src/components/button-update.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Component, Prop, EventEmitter, Event } from '@stencil/core';
2+
3+
@Component({
4+
tag: 'button-update'
5+
})
6+
7+
export class ButtonUpdate {
8+
9+
@Prop() label: string;
10+
11+
@Event() updateComponent: EventEmitter;
12+
13+
handleClick(event: UIEvent) {
14+
this.updateComponent.emit();
15+
}
16+
17+
render() {
18+
if(this.label) {
19+
return (
20+
<button onClick={(event: UIEvent) => this.handleClick(event)}>{this.label}</button>
21+
);
22+
}
23+
}
24+
}

src/components/input-listener.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component } from '@stencil/core';
2+
3+
@Component({
4+
tag: 'input-listener'
5+
})
6+
7+
export class InputListener {
8+
9+
static inputChanged(event: UIEvent) {
10+
console.log(event.target);
11+
}
12+
13+
render() {
14+
return (
15+
<input value="" onChange={ (event: UIEvent) => InputListener.inputChanged(event)}/>
16+
);
17+
}
18+
}

src/components/random-number.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Component, Prop, State, Listen } from '@stencil/core';
2+
3+
@Component({
4+
tag: 'random-number'
5+
})
6+
7+
export class RandomNumber {
8+
9+
@State() randomNumber: number;
10+
@Prop() min: number;
11+
@Prop() max: number;
12+
13+
componentDidLoad() {
14+
this.randomNumber = Math.floor(Math.random()*(this.max- this.min+1)+ this.min);
15+
}
16+
17+
@Listen('updateComponent')
18+
updateNumberHandler() {
19+
this.randomNumber = Math.floor(Math.random()*(this.max- this.min+1)+ this.min);
20+
}
21+
22+
render() {
23+
return (
24+
<p>
25+
{this.randomNumber}
26+
</p>
27+
);
28+
}
29+
}

src/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212
<my-name></my-name>
1313
<todo-list></todo-list>
1414
<button-alert button-text="Show me the money!" alert-message="The money!"></button-alert>
15+
<input-listener></input-listener>
16+
<random-number min="1" max="200">
17+
<button-update label="New number"></button-update>
18+
</random-number>
1519
</body>
1620
</html>

stencil.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
exports.config = {
22
bundles: [
3-
{ components: ['my-name', 'my-first-component', 'todo-list', 'button-alert'] }
3+
{ components: ['my-name', 'my-first-component', 'todo-list', 'button-alert', 'input-listener', 'random-number', 'button-update'] }
44
],
55
collections: [
66
{ name: '@stencil/router' }

0 commit comments

Comments
 (0)