-
Notifications
You must be signed in to change notification settings - Fork 18
/
index-hook.js
124 lines (106 loc) · 2.78 KB
/
index-hook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* @project react-swing
* Created by ssanjun on 2016. 7. 12..
*/
import React, { useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import ReactSwing from '../dist/react-swing.js';
const App = () => {
const [stack, setStack] = useState(null);
const [cardCount, setCardCount] = useState(4);
const stackEl = useRef();
// throwOut Method
const throwCard = () => {
// ReactSwing Card Directions
console.log('ReactSwing.DIRECTION', ReactSwing.DIRECTION);
console.log('stack', stack);
console.log('stack.getConfig', stack.getConfig());
console.log('stackEl', stackEl);
// ReactSwing Component Childrens
const targetEl = stack.childElements[1];
console.log('targetEl', targetEl);
if (targetEl && targetEl.current) {
// stack.getCard
const card = stack.getCard(targetEl.current);
console.log('card', card);
// throwOut method call
card.throwOut(100, 200, ReactSwing.DIRECTION.RIGHT);
}
};
const addCard = () => {
setCardCount(cardCount + 1);
};
const renderCards = () => {
const cardData = [
{
icon: '♣',
className: 'clubs',
},
{
icon: '♦',
className: 'diamonds',
},
{
icon: '♥',
className: 'hearts',
},
{
icon: '♠',
className: 'spades',
},
];
const cards = [];
for (let i = 0; i < cardCount; i++) {
const data = cardData[i % cardData.length];
cards.push(
<div key={i} className={`card ${data.className}`} ref={`card${i}`}>
{data.icon}
</div>,
);
}
return cards;
};
return (
<div>
<div id="viewport">
{/*
ReactSwing Element
*/}
<ReactSwing
className="stack"
setStack={(stack) => setStack(stack)}
ref={stackEl}
throwout={(e) => console.log('throwout', e)}
>
{/*
children elements is will be Card
*/}
{/**
<div className="card clubs" ref="card1" throwout={(e) => console.log('card throwout', e)}>
♣
</div>
<div className="card diamonds" ref="card2">
♦
</div>
<div className="card hearts" ref="card3">
♥
</div>
<div className="card spades" ref="card4">
♠
</div>
*/}
{renderCards()}
</ReactSwing>
</div>
<div className="control">
<button type="button" onClick={throwCard.bind(this)}>
throw Card
</button>
<button type="button" onClick={addCard.bind(this)}>
add Card
</button>
</div>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('app'));