Skip to content
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: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"create-react-class": "^15.6.3",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
Expand Down
4 changes: 4 additions & 0 deletions src/api/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ module.exports = {
async reloadTodoDatas() {
let { data: response } = await axios.get('https://jsonplaceholder.typicode.com/todos');
return response;
},
async reloadOmDbApi() {
let { data: response} = await axios.get('http://www.omdbapi.com/?s=Batman&apikey=34472924');
return response;
}
};
86 changes: 60 additions & 26 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,72 @@
import React from "react";
import ReactDOM from "react-dom";
import Header from './components/Header';
import Todo from './components/Todo';
import List from './components/List';
import api from './api/base';


import {TabsControl} from './components/TabsControl';
import {Tab}from './components/TabsControl';
import { exists } from "fs";
import "./css/TabsControl.css";

class HelloMessage extends React.Component {

constructor(props) {
super(props);
this.state = {
todos: []
};
}
constructor(props) {
super(props);
this.state = {
todos: [],
omdblist: []
};

}


async componentDidMount() {
const todos = await api.reloadTodoDatas();
const { Search: omdblist} = await api.reloadOmDbApi();

this.setState({
"todos": todos,
"omdblist": omdblist,
})
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

可以改寫如下,

const { Search: omdblist} = await api.reloadOmDbApi();
// 接下來就可以直接,

this.setState({
  todos,
  omdblist
});

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

好的 立馬來嘗試

}

async componentDidMount() {
const todos = await api.reloadTodoDatas();
this.setState({
todos
});
}
render() {

const { todos } = this.state;
const { omdblist } = this.state;
return (
<div>
<h1> {this.props.name}</h1>
<TabsControl baseWidth={400}>
<Tab name="first">
<div className="container">
<ul>
{
todos.map((todo, index) => {
return <Todo key={index} id={todo.id} title={todo.title} />
})
}
</ul>
</div>
</Tab>
<Tab name="second">
<table>
<tbody>
{
omdblist.map((list, index) => {
return <List key={index} list={list} />
})
}
</tbody>
</table>
</Tab>
</TabsControl>
</div>

render() {
const { todos } = this.state;
return (
<div>
<Header/>
<div className="container">
<h1>Hi {this.props.name}</h1>
</div>
</div>
);
}
);
}
}

const App = document.getElementById("app");
ReactDOM.render(<HelloMessage name="Caesar" />, App);
ReactDOM.render(<HelloMessage name="My youli" />, App);
17 changes: 17 additions & 0 deletions src/components/List.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

這邊的 react 是沒有用到的,可以不需要

Copy link
Copy Markdown
Author

@youlixiao youlixiao Jun 4, 2019

Choose a reason for hiding this comment

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

是喔 我去試試看
可是我剛試著去拿掉會出錯

import "../css/list.css";

const List = ({list}) => {
return (
<tr>
<td>
<img src= {list.Poster} />
</td>
<td>
<p>{list.Title}({list.Year})({list.Type})</p>
</td>
</tr>
);
}

export default List;
62 changes: 62 additions & 0 deletions src/components/TabsControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from "react";



const TabsControl = (props) => {
var currentIndex = 0;
const setClass = (index, name) => {

var elems = document.querySelectorAll(".tab-title-item");
elems.forEach(function (e) {
e.classList.remove("active");
if (e.id === name) {
e.classList.add("active");
}
})
var elems = document.querySelectorAll(".tab-content-item");
elems.forEach(function (e) {
e.classList.remove("active");
if (e.id === name) {
e.classList.add("active");
}
})


}
const getTitleItemCssClasses = (index) => {
return index === currentIndex ? "tab-title-item active" : "tab-title-item";
}
const getContentItemCssClasses = (index) => {
return index === currentIndex ? "tab-content-item active" : "tab-content-item";
}
return (
<div>
<nav className="tab-title-items">

{React.Children.map(props.children, (element, index) => {
return (
<div onClick={() => setClass(index, element.props.name)} id={element.props.name} key={element.props.name} className={getTitleItemCssClasses(index)}>
{element.props.name}
</div>
)
})}
</nav>
<div className="tab-content-items">
{React.Children.map(props.children, (element, index) => {
return (<div data={element.props.name} id={element.props.name} className={getContentItemCssClasses(index)}>{element}</div>)
})}
</div>
</div>
)
}


const Tab = (props) => {
return (
<div>{props.children}</div>
);
};
module.exports = {
TabsControl,
Tab,
};
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

這邊的排版明顯與上面的縮排不同,請採用統一格式,
1tab = 4space || 2space

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

好的,我去看一下排版狀況

8 changes: 8 additions & 0 deletions src/components/Todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react";
const Todo = ({id, title}) => {
return (
<li key={id}>{title}</li>
);
}

export default Todo;
21 changes: 21 additions & 0 deletions src/css/list.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
img {
width:80px;
height:100px;
margin-left: auto;
margin-right: auto;
}
tr:nth-child(even) {
background: #CCC
}

tr:nth-child(odd) {
background: #FFF
}

p {
color: #0D33FF;
}

table {
width: 100%;
}
39 changes: 39 additions & 0 deletions src/css/tabsControl.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.container,
.container * {
box-sizing: border-box;
}

.container {
border: 1px solid #999;
}
.tab-title-items {
bottom: 0;
display: table;
width: 100%;
height: 50px;
padding: 0;
table-layout: fixed;
border-bottom: 1px solid #999;
}

.tab-title-item {
display: table-cell;
width: 1%;
height: 50px;
color: #333;
text-align: center;
vertical-align: middle;
}

.tab-title-item.active{
background-color: #333;
color: #fff;
}

.tab-content-item{
display: none;
}
.tab-content-item.active{
display: block;
}