Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add TodoMVC as example #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion example/consumer/index.ts

This file was deleted.

12 changes: 12 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<link rel="stylesheet" href="todo.css">
</head>
<body>
<div id="app"></div>
<script src="/dist/main.js"></script>
</body>
</html>
52 changes: 27 additions & 25 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
{
"name": "reactivedb-expmple",
"version": "0.7.0",
"description": "Example for ReactiveDB",
"main": "index.ts",
"name": "reactivedb-todo-mvc",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"dev": "cross-env NODE_ENV=development webpack-dev-server --hot --port 9999",
"build": "cross-env NODE_ENV=production webpack"
},
"repository": {
"type": "git",
"url": "git+https://github.com/teambition/ReactiveDB.git"
},
"keywords": [
"ReactiveDB",
"RxJS",
"Lovefield"
],
"author": "[email protected]",
"license": "MIT",
"bugs": {
"url": "https://github.com/teambition/ReactiveDB/issues"
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/react": "^16.7.20",
"@types/react-dom": "^16.0.11",
"cross-env": "^5.2.0",
"ts-loader": "^5.2.2",
"tslint": "^5.11.0",
"tslint-config-standard": "^8.0.1",
"tslint-loader": "^3.5.4",
"typescript": "^3.2.4",
"webpack": "^4.23.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10"
},
"homepage": "https://github.com/teambition/ReactiveDB#readme",
"dependencies": {
"antd": "^2.7.3",
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"devDependencies": {
"typescript": "^2.2.1"
"@types/classnames": "^2.2.6",
"classnames": "^2.2.6",
"react": "^16.8.0-alpha.0",
"react-dom": "16.8.0-alpha.0",
"reactivedb": "0.12.0-alpha.1-incremental",
"rxjs": "^6.3.3"
}
}
3 changes: 0 additions & 3 deletions example/rdb/Database.ts

This file was deleted.

160 changes: 0 additions & 160 deletions example/rdb/defineSchema.ts

This file was deleted.

1 change: 0 additions & 1 deletion example/rdb/index.ts

This file was deleted.

13 changes: 13 additions & 0 deletions example/src/TodoMVC.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from 'react'

import { TodoList, Filter, Header } from './components'

export const TodoMVC = () => {
return (
<>
<Header />
<TodoList />
<Filter />
</>
)
}
52 changes: 52 additions & 0 deletions example/src/components/filter.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as React from 'react'
import { useContext } from 'react'
import cx from 'classnames'

import { source, TodoContext } from '../controller'
import { useObservable } from '../hooks'

export const Filter = React.memo(() => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will there have any problem using React.memp with empty props?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this should be ok, just like

shouldComponentUpdate() {
  return false
}

const filter = useObservable(source.filter, 'all')
const context = useContext(TodoContext)

const setAll = () => context.setFilter('all')
const setActive = () => context.setFilter('active')
const setCompleted = () => context.setFilter('completed')

const handlers = [
{
href: '#/',
className: cx({ selected: filter === 'all' }),
onClick: setAll,
children: 'All'
},
{
href: '#/active',
className: cx({ selected: filter === 'active' }),
onClick: setActive,
children: 'Active'
},
{
href: '#/completed',
className: cx({ selected: filter === 'completed' }),
onClick: setCompleted,
children: 'Complete'
}
]

const items = handlers.map((item) => {
return (
<li key={ item.href }>
<a { ...item } />
</li>
)
})

return (
<footer className='footer'>
<ul className="filters">
{ items }
</ul>
</footer>
)
})
35 changes: 35 additions & 0 deletions example/src/components/header.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react'
import { useState, useContext } from 'react'

import { TodoContext } from '../controller'

export const Header = () => {
const [ newItem, setValue ] = useState('')
const context = useContext(TodoContext)

const onInput = (e: React.SyntheticEvent<HTMLInputElement>) => {
const target = e.target as HTMLInputElement
setValue(target.value)
}

const onAdd = (e: React.KeyboardEvent<HTMLInputElement>) => {
const target = e.target as HTMLInputElement
if (e.key === 'Enter') {
context.addItem(target.value)
setValue('')
}
}

return (
<header className='header'>
<h1>todos</h1>
<input
className='new-todo'
value={ newItem }
placeholder='What needs to be done?'
onKeyPress={ onAdd }
onChange={ onInput }
/>
</header>
)
}
4 changes: 4 additions & 0 deletions example/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './filter.component'
export * from './list.component'
export * from './header.component'
export * from './item.component'
Loading