React test

by moehiko

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.min.js"></script>
<html></html>
<head></head>
<body>
<div id="contents"></div>
</body>

CSS

ul, li {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	vertical-align: baseline;
}

li {
  list-style: none;
  margin-bottom: 3px;
}

body {
  background-color: #f5f5f5;
}

#contents {
  padding: 10px;
}

#menu {
  margin-bottom: 10px;
}

.mod-ra {
  background-color: #fff;
  margin-bottom: 13px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}

.room {
  border-bottom: 1px solid #d8d8d8;
  padding: 5px;
}

.more-room-button {
  text-align: center;
}

#more-building-button {
  padding-top: 20px;
  text-align: center;
  height: 45px;
  background-color: #fff;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}

.hidden {
  display: none;
}

React

const {createStore} = Redux;

// Action名の定義
const SORT = 'SORT',
			MOREROOM = 'MOREROOM';
      
// Action Creator
function sort(value) {
  // Action
  return {
    type: SORT,
    value,
  };
}

function showMoreRoom(value) {
  // Action
  return {
    type: MOREROOM,
    value,
  };
}

// Reducer
function reducer(state, action) {
  switch (action.type) {
    case SORT:
	    state.rooms = sort(state.rooms, action.sortKey)
      return state
		case MOREROOM:
			state.displayCount = state.displayCount + 3
      return state
		default:
      return state
  }
  // このメソッド内の処理はajaxでサーバサイドで検索することになる
  // 検証のためフロントでソートを仮実装

  function sort(rooms, sortKey) {
    if (sortKey == "default" || sortKey == 'low') {
	    ret = rooms.sort(function (a, b) {
  	   	return (a.rent < b.rent)? -1 : 1
    	})	
    } else {
	    ret = rooms.sort(function (a, b) {
  		 	return (a.rent > b.rent)? -1 : 1
   	 	})	
		}
   	return ret 
  }
}

// store
// 初期state変数(initialState)の作成
const initialState = {
	rooms : [
    {num:1, rent: 10000, area: 30},
    {num:2, rent: 20000, area: 20},
    {num:3, rent: 30000, area: 10},
    {num:4, rent: 40000, area: 10},
    {num:5, rent: 50000, area: 10}
	],
  displayCount: 3
}
// createStore()メソッドを使ってStoreの作成
const store = createStore(reducer, initialState);

class Menu extends React.Component {
	handleOnChange(event) {
  	store.dispatch({ type: 'SORT', value: event.currentTarget.value});
  }
	render() {
  	return (
      <select onChange={this.handleOnChange}>
        <option value="low">安い順</option>
        <option value="high">高い順</option>
      </select>
    )
  }
}

// View
class Room extends React.Component {
  render() {
    return (
   	 <div className="room" key={this.key}>
       <ul>
          <li>部屋{this.props.num}</li>
          <li>賃料:{this.props.rent}</li>
          <li>面積:{this.props.area}</li>
        </ul>
    </div>
    )
  }
}

class RoomList extends React.Component {
  render() {
  	let list = [],
    		rooms =...