Skip to content

Instantly share code, notes, and snippets.

@velopert

velopert/App.js Secret

Created April 18, 2017 23:30
Show Gist options
  • Save velopert/444fd4cdee4411efa7e74e981c40c11a to your computer and use it in GitHub Desktop.
Save velopert/444fd4cdee4411efa7e74e981c40c11a to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import Header from './components/Header';
import Container from './components/Container';
import ViewSelector from './components/ViewSelector';
import FloatingButton from './components/FloatingButton';
import ContactModal from './components/ContactModal';
import Dimmed from './components/Dimmed';
import oc from 'open-color';
function generateRandomColor() {
const colors = [
'gray',
'red',
'pink',
'grape',
'violet',
'indigo',
'blue',
'cyan',
'teal',
'green',
'lime',
'yellow',
'orange'
];
// 0 부터 12까지 랜덤 숫자
const random = Math.floor(Math.random() * 13);
return oc[colors[random]][6];
}
class App extends Component {
state = {
view: 'favorite',
modal: {
visible: false,
mode: null // create 혹은 modify
}
}
// view 선택 메소드 정의
handleSelectView = (view) => this.setState({view})
// 모달 관련 메소드들
modalHandler = {
show: (mode, payload) => {
this.setState({
modal: {
mode,
visible: true,
...payload // payload 안의 값을 풀어서 여기에 넣음
}
})
},
hide: () => {
this.setState({
modal: {
...this.state.modal, // 기존 값들을 복사해서 안에 넣음
visible: false
}
})
},
change: ({name, value}) => {
this.setState({
modal: {
...this.state.modal,
[name]: value // 인자로 전달받은 name 의 값을 value 로 설정
}
})
},
// 추후 구현될 메소드들
action: {
create: null,
modify: null,
remove: null
}
}
// FloatingButton 클릭
handleFloatingButtonClick = () => {
// 현재 view 가 list 가 아니면 list 로 설정
const { view } = this.state;
if(view !== 'list')
this.setState({view: 'list'});
// Contact 추가 모달 띄우기
this.modalHandler.show(
'create',
{
name: '',
phone: '',
color: generateRandomColor()
}
);
}
render() {
// 레퍼런스 준비
const {
handleSelectView,
handleFloatingButtonClick,
modalHandler
} = this;
const {
view,
modal
} = this.state;
return (
<div>
<Header/>
<ViewSelector onSelect={handleSelectView} selected={view}/>
{/* view 값에 따라 다른 컨테이너를 보여준다 */}
<Container visible={view==='favorite'}>즐겨찾기</Container>
<Container visible={view==='list'}>리스트</Container>
<ContactModal
{...modal}
onHide={modalHandler.hide}
onChange={modalHandler.change}
/>
<Dimmed visible={modal.visible}/>
<FloatingButton onClick={handleFloatingButtonClick}/>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment