/docs/examples/todo.html

https://github.com/thysultan/dio.js · HTML · 66 lines · 61 code · 5 blank · 0 comment · 0 complexity · 1ea386c62ce4614bfd432cac61ee7fa2 MD5 · raw file

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset=utf-8>
  5. <meta http-equiv=X-UA-Compatible content=IE=edge>
  6. <meta name=viewport content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  7. <title>Todo App</title>
  8. <script src=https://unpkg.com/dyo></script>
  9. </head>
  10. <body>
  11. <main></main>
  12. <script>
  13. const {h, render, useReducer} = dyo
  14. const target = document.querySelector('main')
  15. const reducer = ({value = '', entries = []}, {type, payload}) => {
  16. switch (type) {
  17. case 'INPUT':
  18. return {value: payload, entries: entries}
  19. case 'SUBMIT':
  20. return {value: '', entries: !value ? entries : entries.concat({active: false, key: Math.random(), value: payload})}
  21. case 'DELETE':
  22. return {value: '', entries: entries.filter(entry => entry.key !== payload)}
  23. case 'ACTIVE':
  24. return {value: '', entries: entries.map(entry => entry.key !== payload ? entry : {...entry, active: !entry.active})}
  25. }
  26. }
  27. const Entry = ({key, value, active, dispatch}) => {
  28. return (
  29. h('li', {style: {margin: '8px 0', textTransform: 'capitalize'}},
  30. h('label', {},
  31. h('input', {type: 'checkbox', checked: active, style: {marginRight: '5px'}, onInput: event => {
  32. dispatch({type: 'ACTIVE', payload: key})
  33. }}),
  34. h('span', {style: active ? {color: 'rgba(0,0,0,.3)', textDecoration: 'line-through'} : null}, value),
  35. h('a', {
  36. style: {color: 'red', marginLeft: '10px'}, onClick: event => dispatch({type: 'DELETE', payload: key})
  37. }, '×')
  38. )
  39. )
  40. )
  41. }
  42. const Main = (props) => {
  43. const [{value, entries}, dispatch] = useReducer(reducer, {value: '', entries: []})
  44. return (
  45. h('main', {style: {fontFamily: 'sans-serif', background: '#eee', padding: '20px'}},
  46. h('h2', {}, 'Todos:'),
  47. h('ol', {}, entries.map(props => h(Entry, {...props, dispatch}))),
  48. h('form', {onSubmit: event => {
  49. dispatch({type: 'SUBMIT', payload: value})
  50. event.preventDefault()
  51. }},
  52. h('input', {value: value, onInput: event => dispatch({type: 'INPUT', payload: event.target.value})}),
  53. h('button', {}, 'Add Todo')
  54. )
  55. )
  56. )
  57. }
  58. render(h(Main), target)
  59. </script>
  60. </body>
  61. </html>