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

提交代码 #38

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
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@
"classnames": "^2.2.6",
"lodash": "^4.17.15",
"prop-types": "^15.7.2",
"ramda": "^0.27.0",
"rc-pagination": "^2.0.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^7.2.0",
"redux": "^4.0.5"
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-observable": "^1.2.0",
"rxjs": "^6.5.4",
"rxjs-compat": "^6.5.4"
},
"devDependencies": {
"react-scripts": "3.4.0"
},
"browserslist": [">0.2%", "not dead", "not ie <= 11", "not op_mini all"]
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
15 changes: 15 additions & 0 deletions src/Epics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import { ajax } from 'rxjs/observable/dom/ajax';
import { combineEpics } from 'redux-observable';
// import {SELECT_PLAYER} from '../constants/ActionTypes';
// const url = '';
// const ListEpic= (action$)=> (
// action$
// .ofType(SELECT_PLAYER)
// .catch((error) =>console.log(error))
// 可以截取action,拍扁成流形式,在这里进行异步请求数据处理之类,。rxjs提供了很多异步处理操作符,
// )

// export const rootEpic = combineEpics(ListEpic);
export const rootEpic = combineEpics();
9 changes: 9 additions & 0 deletions src/actions/PlayersActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ export function starPlayer(id) {
id,
};
}

export function selectedPlay(id,selected) {

return {
type: types.SELECT_PLAYER,
id,
selected,
};
}
27 changes: 14 additions & 13 deletions src/components/AddPlayerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ import PropTypes from 'prop-types';
import styles from './AddPlayerInput.css';

class AddPlayerInput extends Component {
render() {
return (
<input
type="text"
autoFocus={true}
className={classnames('form-control', styles.addPlayerInput)}
placeholder="Type the name of a player"
value={this.state.name}
onChange={this.handleChange.bind(this)}
onKeyDown={this.handleSubmit.bind(this)}
/>
);
}

constructor(props, context) {
super(props, context);
Expand All @@ -36,6 +23,20 @@ class AddPlayerInput extends Component {
this.setState({ name: '' });
}
}
render() {
return (
<input
type="text"
autoFocus={true}
className={classnames('form-control', styles.addPlayerInput)}
placeholder="Type the name of a player"
value={this.state.name}
onChange={this.handleChange.bind(this)}
onKeyDown={this.handleSubmit.bind(this)}
/>
);
}

}

AddPlayerInput.propTypes = {
Expand Down
16 changes: 16 additions & 0 deletions src/components/PlayerList.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,19 @@
padding-left: 0;
margin-bottom: 0;
}
.footer {
display: flex;
margin: 20px 0;
font-size:16px;
}


.rcPagination {
display: flex;
list-style: none;
padding-left: 10px;
}
.rcPagination li{
margin: 0 10px;

}
81 changes: 65 additions & 16 deletions src/components/PlayerList.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
import React, { Component } from 'react';
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import styles from './PlayerList.css';
import './PlayerList.css';

import PlayerListItem from './PlayerListItem';
import * as R from 'ramda';
import Pagination from 'rc-pagination';

const isArray = (Arr) => {
return R.is(Array, Arr) && !R.isEmpty(Arr)
}

class PlayerList extends PureComponent {
constructor(props) {
super(props);

this.state = {
page: 5,
current: 1,
}
}
pageNum = (current) => {
this.setState({
current
});
}

class PlayerList extends Component {
render() {
const { players } = this.props;
const { current, page } = this.state;

const getCurrentList = (list, current, page) => {
return R.slice(page * current - page, page * current)(list)
}

return (
<ul className={styles.playerList}>
{this.props.players.map((player, index) => {
return (
<PlayerListItem
key={index}
id={index}
name={player.name}
team={player.team}
position={player.position}
starred={player.starred}
{...this.props.actions}
<div>
<ul className={styles.playerList}>
{getCurrentList(players, current, page).map((player, index) => {
return (
<PlayerListItem
key={index}
id={index}
name={player.name}
team={player.team}
position={player.position}
starred={player.starred}
{...this.props.actions}
/>
);
})}
</ul>

{
isArray(players) && players.length > 5 &&
(<footer className={'footer'}>
<span>分页:</span>
<Pagination
current={current}
onChange={this.pageNum}
total={players.length}
defaultPageSize={page}
className={'rcPagination'}
pageSize={page}
showQuickJump
/>
);
})}
</ul>
</footer>
)
}

</div>
);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/components/PlayerList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as R from 'ramda';

const isArray = (Arr) => {
return R.is(Array, Arr) && !R.isEmpty(Arr)
}

const getCurrentList = (list, current, page) => {
return R.slice(page * current - page, page * current)(list)
}


test('测试isArray和getCurrentList方法,获取current页数,每页5个,结果是否正确', () => {
const testList = [1,2,3,4,5,6,7,8,9,10,11]
const current = 2;
const page = 5
expect(getCurrentList(testList,current,page)).toEqual([6,7,8,9,10]);

})
4 changes: 4 additions & 0 deletions src/components/PlayerListItem.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
button:focus {
outline: 0 !important;
}

.selectPlay {
height: 34px;
}
21 changes: 21 additions & 0 deletions src/components/PlayerListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import classnames from 'classnames';
import PropTypes from 'prop-types';
import styles from './PlayerListItem.css';

const options = [
{key:'SF',value:'SF'},
{key:'SG',value:'SG'},
{key:'PE',value:'PE'},
{key:'PG',value:'PG'},
]
class PlayerListItem extends Component {

componentDidMount() {
// console.log(this.props)
}

render() {
return (
<li className={styles.playerListItem}>
Expand Down Expand Up @@ -35,6 +46,15 @@ class PlayerListItem extends Component {
>
<i className="fa fa-trash" />
</button>
<select onChange={(e) => this.props.selectedPlay(this.props.id,e.target.value)} value={this.props.position} className={`btn btn-default selectPlay`}>
{
options.map((item,index)=>{
return (
<option value={item.key} key={index}>{item.value}</option>
)
})
}
</select>
</div>
</li>
);
Expand All @@ -48,6 +68,7 @@ PlayerListItem.propTypes = {
position: PropTypes.string.isRequired,
starred: PropTypes.bool,
starPlayer: PropTypes.func.isRequired,
selectedPlay:PropTypes.func.isRequired,
};

export default PlayerListItem;
2 changes: 2 additions & 0 deletions src/constants/ActionTypes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const ADD_PLAYER = 'ADD_PLAYER';
export const STAR_PLAYER = 'STAR_PLAYER';
export const DELETE_PLAYER = 'DELETE_PLAYER';

export const SELECT_PLAYER = 'SELECT_PLAYER';
12 changes: 10 additions & 2 deletions src/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import React, { Component } from 'react';
import { combineReducers, createStore } from 'redux';
import { applyMiddleware,combineReducers, createStore } from 'redux';
import { Provider } from 'react-redux';
import { createEpicMiddleware } from 'redux-observable';
import { composeWithDevTools } from 'redux-devtools-extension'
import { rootEpic } from '../Epics';

import PlayerListApp from './PlayerListApp';
import * as reducers from '../reducers';

// 实例化一个epic异常流框架
const epicMiddleware = createEpicMiddleware();
const reducer = combineReducers(reducers);
const store = createStore(reducer);
const store = createStore(reducer, composeWithDevTools(applyMiddleware(epicMiddleware)))

epicMiddleware.run(rootEpic);


export default class App extends Component {
render() {
Expand Down
4 changes: 3 additions & 1 deletion src/containers/PlayerListApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Component } from 'react';
import styles from './PlayerListApp.css';
import { connect } from 'react-redux';

import { addPlayer, deletePlayer, starPlayer } from '../actions/PlayersActions';
import { addPlayer, deletePlayer, starPlayer,selectedPlay } from '../actions/PlayersActions';
import { PlayerList, AddPlayerInput } from '../components';

class PlayerListApp extends Component {
Expand All @@ -15,6 +15,7 @@ class PlayerListApp extends Component {
addPlayer: this.props.addPlayer,
deletePlayer: this.props.deletePlayer,
starPlayer: this.props.starPlayer,
selectedPlay:this.props.selectedPlay,
};

return (
Expand All @@ -37,5 +38,6 @@ export default connect(
addPlayer,
deletePlayer,
starPlayer,
selectedPlay,
},
)(PlayerListApp);
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './containers/App';
// import 'rc-pagination/dist/rc-pagination.min.css';
import './index.css';

ReactDOM.render(
Expand Down
8 changes: 8 additions & 0 deletions src/reducers/playerlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ export default function players(state = initialState, action) {
...state,
playersById: players,
};
case types.SELECT_PLAYER:
let Arr = [...state.playersById];
let getItem = Arr.find((item, index) => index === action.id);
getItem.position = action.selected;

return {
...state,
playersById: Arr,
};
default:
return state;
}
Expand Down