Last active
November 18, 2016 10:15
-
-
Save zehnpaard/66b5ea4a93df3add8f3874f746a88a55 to your computer and use it in GitHub Desktop.
Reagent port of official React tutorial upto the end https://facebook.github.io/react/tutorial/tutorial.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns react-tutorial.core | |
(:require | |
[reagent.core :as r])) | |
(defn calculateWinner [squares] | |
(let [lines [[0 1 2] | |
[3 4 5] | |
[6 7 8] | |
[0 3 6] | |
[1 4 7] | |
[2 5 8] | |
[0 4 8] | |
[2 4 6]]] | |
(->> lines | |
(filter (fn [[a b c]] | |
(and | |
(not (nil? (get squares a))) | |
(= (get squares a) | |
(get squares b) | |
(get squares c))))) | |
first | |
first | |
(get squares)))) | |
(defn Square [value click-fn] | |
[:button.square {:on-click click-fn} value]) | |
(defn Board [squares click-fn] | |
(let [renderSquare (fn [i] | |
[Square (get squares i) #(click-fn i)])] | |
[:div | |
[:div.board-row | |
[renderSquare 0] | |
[renderSquare 1] | |
[renderSquare 2]] | |
[:div.board-row | |
[renderSquare 3] | |
[renderSquare 4] | |
[renderSquare 5]] | |
[:div.board-row | |
[renderSquare 6] | |
[renderSquare 7] | |
[renderSquare 8]]])) | |
(defn Game [] | |
(let [history (r/atom [{:squares (apply vector (repeat 9 nil))}]) | |
xIsNext (r/atom true) | |
stepNumber (r/atom 0) | |
handle-click | |
(fn [i] | |
(let [squares (:squares (get @history @stepNumber))] | |
(if-not (or (calculateWinner squares) | |
(squares i)) | |
(let [next-move (if @xIsNext "X" "O") | |
next-squares (assoc squares i next-move) | |
next-step {:squares next-squares}] | |
(do (reset! stepNumber (count @history)) | |
(swap! history #(conj % next-step)) | |
(swap! xIsNext not)))))) | |
jumpTo | |
(fn [move] | |
(do (reset! stepNumber move) | |
(reset! xIsNext (zero? (mod move 2)))))] | |
(fn [] | |
(let [squares (:squares (get @history @stepNumber)) | |
status (if-some [winner (calculateWinner squares)] | |
(str "Winner: " winner) | |
(str "Next player: " (if @xIsNext "X" "O"))) | |
move-desc #(if (zero? %) | |
"Game start" | |
(str "Move #" %)) | |
move-elem (fn [move] | |
[:li {:key move} | |
[:a {:href "#" | |
:onClick #(jumpTo move)} | |
(move-desc move)]]) | |
moves (->> @history | |
count | |
range | |
(map move-elem) | |
doall)] | |
[:div.game | |
[:div.game-board | |
[Board squares handle-click]] | |
[:div.game-info | |
[:div status] | |
[:ol moves]]])))) | |
(r/render | |
[Game] | |
(js/document.getElementById "container")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── project.clj | |
├── resources | |
│ └── public | |
│ ├── css | |
│ │ └── main.css | |
│ └── index.html | |
└── src | |
└── react_tutorial | |
└── core.cljs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link href="css/main.css" rel="stylesheet" type="text/css"> | |
</head> | |
<body> | |
<div id="container"></div> | |
<script src="js/out/goog/base.js"></script> | |
<script src="js/main.js"></script> | |
<script>goog.require('react_tutorial.core')</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Square(props) { | |
return ( | |
<button className="square" onClick={() => props.onClick()}> | |
{props.value} | |
</button> | |
); | |
} | |
class Board extends React.Component { | |
renderSquare(i) { | |
return <Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)} />; | |
} | |
render() { | |
return ( | |
<div> | |
<div className="board-row"> | |
{this.renderSquare(0)} | |
{this.renderSquare(1)} | |
{this.renderSquare(2)} | |
</div> | |
<div className="board-row"> | |
{this.renderSquare(3)} | |
{this.renderSquare(4)} | |
{this.renderSquare(5)} | |
</div> | |
<div className="board-row"> | |
{this.renderSquare(6)} | |
{this.renderSquare(7)} | |
{this.renderSquare(8)} | |
</div> | |
</div> | |
); | |
} | |
} | |
class Game extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
history: [{ | |
squares: Array(9).fill(null) | |
}], | |
xIsNext: true, | |
stepNumber: 0 | |
}; | |
} | |
handleClick(i) { | |
const history = this.state.history.slice(0, this.state.stepNumber+1); | |
const current = history[history.length - 1]; | |
const squares = current.squares.slice(); | |
if (calculateWinner(squares) || squares[i]) { | |
return; | |
} | |
squares[i] = this.state.xIsNext ? 'X' : 'O'; | |
this.setState({ | |
history: history.concat([{ | |
squares: squares | |
}]), | |
xIsNext: !this.state.xIsNext, | |
stepNumber: history.length | |
}); | |
} | |
jumpTo(step) { | |
this.setState({ | |
stepNumber: step, | |
xIsNext: (step % 2) ? false : true, | |
}); | |
render() { | |
const history = this.state.history; | |
const current = history[this.state.stepNumber]; | |
const winner = calculateWinner(current.squares); | |
let status; | |
if (winner) { | |
status = 'Winner: ' + winner; | |
} else { | |
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O'); | |
} | |
const moves = history.map((step, move) => { | |
const desc = move ? | |
'Move #' + move : | |
'Game start'; | |
return ( | |
<li> | |
<a href="#" onClick={() => this.jumpTo(move)}>{desc}</a> | |
</li> | |
); | |
}); | |
return ( | |
<div className="game"> | |
<div className="game-board"> | |
<Board | |
squares={current.squares} | |
onClick={(i) => this.handleClick(i)} | |
/> | |
</div> | |
<div className="game-info"> | |
<div>{status}</div> | |
<ol>{moves}</ol> | |
</div> | |
</div> | |
); | |
} | |
} | |
// ======================================== | |
ReactDOM.render( | |
<Game />, | |
document.getElementById('container') | |
); | |
function calculateWinner(squares) { | |
const lines = [ | |
[0, 1, 2], | |
[3, 4, 5], | |
[6, 7, 8], | |
[0, 3, 6], | |
[1, 4, 7], | |
[2, 5, 8], | |
[0, 4, 8], | |
[2, 4, 6], | |
]; | |
for (let i = 0; i < lines.length; i++) { | |
const [a, b, c] = lines[i]; | |
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { | |
return squares[a]; | |
} | |
} | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font: 14px "Century Gothic", Futura, sans-serif; | |
margin: 20px; | |
} | |
ol, ul { | |
padding-left: 30px; | |
} | |
.board-row:after { | |
clear: both; | |
content: ""; | |
display: table; | |
} | |
.status { | |
margin-bottom: 10px; | |
} | |
.square { | |
background: #fff; | |
border: 1px solid #999; | |
float: left; | |
font-size: 24px; | |
font-weight: bold; | |
line-height: 34px; | |
height: 34px; | |
margin-right: -1px; | |
margin-top: -1px; | |
padding: 0; | |
text-align: center; | |
width: 34px; | |
} | |
.square:focus { | |
outline: none; | |
} | |
.kbd-navigation .square:focus { | |
background: #ddd; | |
} | |
.game { | |
display: flex; | |
flex-direction: row; | |
} | |
.game-info { | |
margin-left: 20px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject react-tutorial "0.0.1" | |
:dependencies [[org.clojure/clojure "1.8.0"] | |
[org.clojure/clojurescript "1.9.293"] | |
[reagent "0.6.0"]] | |
:plugins [[lein-cljsbuild "1.1.4"] | |
[lein-figwheel "0.5.7"]] | |
:cljsbuild | |
{:builds | |
{:dev {:source-paths ["src"] | |
:figwheel true | |
:compiler {:output-to "resources/public/js/main.js" | |
:output-dir "resources/public/js/out/"}}}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment