-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(walk): check tagName earlier * fix(watch): check path and not obj * feat: add `mounted` * feat: each && props * example: trying out each, mounted & props * docs: remove @todo's from `mount()`
- Loading branch information
Showing
7 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { div, p, button, classNames } from '../lib/one' | ||
import styles from './styles.module.css' | ||
|
||
function view({ state }) { | ||
return div([ | ||
p(`Count: ${state.count}`, { | ||
className: classNames( | ||
styles.paragraph, | ||
state.count < 0 && styles.low, | ||
state.count > 3 && styles.high, | ||
), | ||
}), | ||
Button('Dec', () => state.count--), | ||
Button('Inc', () => state.count++), | ||
]) | ||
} | ||
|
||
function Button(text, onclick) { | ||
return button(text, { onclick, className: styles.button }) | ||
} | ||
|
||
const state = { count: 0 } | ||
|
||
const Counter = { | ||
selector: '.counter', | ||
state, | ||
view, | ||
} | ||
|
||
export { Counter } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { button, div, h2, p, span } from '../lib/one' | ||
import { getUser } from './services/get-user' | ||
import styles from './styles.module.css' | ||
|
||
function view({ state }) { | ||
async function onclick() { | ||
state.isLoading = true | ||
state.user = await getUser(1) | ||
state.notFound = false | ||
state.isLoading = false | ||
} | ||
|
||
return div([ | ||
h2('Profile'), | ||
!state.user && !state.notFound || state.isLoading ? div([ | ||
skeleton('20px', '100px'), | ||
skeleton('20px', '80px'), | ||
skeleton('20px', '180px'), | ||
]) : div( | ||
state.notFound ? [ | ||
div('User not found', { className: styles.warning }), | ||
button('Try again', { onclick, className: styles.button }), | ||
] : | ||
Object.keys(state.user).map(key => { | ||
if (key === 'id') return | ||
return p([ | ||
span(`${uppercaseFirst(key)}: `), | ||
span(state.user[key], { className: styles.bold }), | ||
]) | ||
}) | ||
), | ||
], { className: styles.profile }) | ||
} | ||
|
||
function uppercaseFirst(str) { | ||
return `${str.charAt(0).toUpperCase()}${str.substring(1)}` | ||
} | ||
|
||
async function mounted({ state, props }) { | ||
const user = await getUser(props.get('id')) | ||
|
||
state.isLoading = false | ||
state.notFound = !Boolean(user) | ||
state.user = user ? user : undefined | ||
} | ||
|
||
function skeleton(height = '1em', width = '100%') { | ||
return div([], { | ||
className: styles.skeleton, | ||
style: `height: ${height}; width: ${width};` | ||
}) | ||
} | ||
|
||
const Profile = { | ||
selector: '.profile', | ||
view, | ||
state: { user: undefined, notFound: false, isLoading: true }, | ||
mounted, | ||
} | ||
|
||
export { Profile } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,8 @@ | ||
import { mount, button, p, div, classNames } from '../lib/one' | ||
import styles from './styles.module.css' | ||
|
||
function view({ state }) { | ||
return div([ | ||
p(`Count: ${state.count}`, { | ||
className: classNames( | ||
styles.paragraph, | ||
state.count < 0 && styles.low, | ||
state.count > 3 && styles.high, | ||
), | ||
}), | ||
button('Dec', { onclick() { state.count-- }, className: styles.button }), | ||
button('Inc', { onclick() { state.count++ }, className: styles.button }), | ||
]) | ||
} | ||
|
||
const state = { count: 0 } | ||
|
||
document.querySelectorAll('.counter').forEach(el => { | ||
mount({ el, view, state }) | ||
}) | ||
|
||
import { each } from '../lib/one' | ||
import { Counter } from './Counter' | ||
import { Profile } from './Profile' | ||
|
||
each([ | ||
Counter, | ||
Profile, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function getUser(id) { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(findUser(id)) | ||
}, 2250) | ||
}) | ||
|
||
function findUser(id) { | ||
return users.find(user => user.id === parseInt(id)) | ||
} | ||
} | ||
|
||
// Mock data | ||
// { id, username, age, address } | ||
const users = [ | ||
{ id: 1, username: 'John', age: 32, address: '123 Main St' }, | ||
{ id: 2, username: 'Jane', age: 25, address: '456 Main St' }, | ||
{ id: 3, username: 'Bob', age: 45, address: '789 Main St' }, | ||
] | ||
|
||
export { getUser } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters