๋ฌ๋ ฅ์ ์ธ ์ผ์ด ์์ด์ React-datepicker ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ์ค๋ช ๊ณผ ์์ ์์ค๊ฐ ๋ถ์น์ ํ ๋๋์ ๋ฐ์์ด์. ๐ฅ
๋ฐ๋ชจ๋ฒ์ ์ ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด ์๋ฌ๊ฐ ๋นต๋นต ํฐ์ง๋๋ผ๊ตฌ์.
* react-datepicker : www.npmjs.com/package/react-datepicker
* react-datepicker demo : reactdatepicker.com/#example-custom-header
* lodash : www.npmjs.com/package/lodash
1. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
yarn add react-datepicker
// Calendar.js
import React, { useState } from "react";
import DatePicker from "react-datepicker";
const Calendar = () => {
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker selected={startDate} onChange={date => setStartDate(date)} />
);
};
export default Calendar
import "react-datepicker/dist/react-datepicker.css"; // css import
import Calendar from './Calendar'
function App() {
return (
<div className="App">
<Calendar />
</div>
);
}
export default App;
์์ฃผ ์ ๋์ํฉ๋๋ค.
๊ทธ๋ฐ๋ฐ ์ ๋ ๊ธฐ๋ณธ ๊ทธ๋๋ก๋ฅผ ์ฐ๋ ๊ฒ ๋ณด๋ค๋ ํ์์ ๋ง๊ฒ ์ปค์คํ ์ ํ์ฌ ์ฌ์ฉํ๊ณ ์ถ์ต๋๋ค.
1. ํ๊ตญ์ด ์ฌ์ฉ ( January -> 1์ )
2. ๋ ๋์ ์์ select๋ก ๋ณ๊ฒฝ
์ด๋ฅผ ์ํด datepicker์์ ํค๋๋ฅผ props๊ฐ์ ๋๊ฒจ์ฃผ๋ฏ๋ก์จ ์ปค์คํ ํ ์ ์๋๋ก ์ต์ ์ ์ ๊ณตํฉ๋๋ค.
Header custom ์์ค๋ฅผ ๊ทธ๋๋ก ๊ฐ์ ธ์ ๋ณด๊ฒ ์ต๋๋ค.
์ด๋ถ๋ถ์ ๊ฐ์ ธ์ ๊ทธ๋๋ก ๋ถ์ฌ๋ณด๋ฉด,
๋ค์๊ณผ ๊ฐ์ด ์๋ฌ๊ฐ ๋นต๋นต ํฐ์ง๋๋ค. ๐ฃ
- 'range' is not defined : range ํจ์๊ฐ ์์(์๋ฌ๋๋๊ฒ ๋น์ฐํ๋จ ์๋ฆฌ)
- 'getYear' is not defined : ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
- 'getMonth' is not defined : ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ถ๊ฐ ๋ํ๋์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ค์ ์ค์นํด ์ค๋๋ค.
3. ์ถ๊ฐ ๋ํ๋์ ์ถ๊ฐ
range ํจ์๋ ์ง์ ๊ตฌํํ๋ ค๊ณ ํ๋๋ฐ, ๋ป ๋๋ก ์ ์๋์ด ์๋๋๋ผ๊ตฌ์.
๊ทธ๋์ lodash ์ ๋์์ ๋ฐ์์ต๋๋ค. ๐ฅ
yarn add date-fns --save // getYear, getMonth
yarn add react-lodash // range ์ฌ์ฉ
4. ์์ค ์์
import React, { useState } from "react";
import { getYear, getMonth } from "date-fns"; // getYear, getMonth
import DatePicker, { registerLocale } from "react-datepicker"; // ํ๊ตญ์ด์ ์ฉ
import ko from 'date-fns/locale/ko'; // ํ๊ตญ์ด์ ์ฉ
registerLocale("ko", ko) // ํ๊ตญ์ด์ ์ฉ
const _ = require('lodash');
4-1. range() ์๋ฌ์ฒ๋ฆฌ
: range(์์๋ ๋, ๋ง์ง๋ง๋ ๋) ์ ๋๋ค.
const years = _.range(1990, getYear(new Date()) + 1, 1); // ์์
4-2. ๋ฐ์ดํฐ ์ธ์ด ์ ์ฉ
{...}
const months = ['1์','2์','3์','4์','5์','6์','7์','8์','9์','10์','11์','12์'];
<DatePicker
{ ... }
locale={ko} // ์ถ๊ฐ
/>
{...}
4-3. ๋ฐ์ดํฐ ํฌ๋ฉง (0000/00/00 -> 0000-00-00)
<DatePicker
{ ... }
dateFormat="yyyy-MM-dd" // ์ถ๊ฐ
/>
์ ๊ฐ ์ํ๋ ๋๋ก ์ ์ปค์คํ ๋์์ต๋๋ค. ๐
์ด ์ธ์ ๋ ํ์ํ ์ปค์คํ ์ ๋ฐ๋ชจ์ฌ์ดํธ์ ์ํ์ด ์ฌ๋ฌ๊ฐ ๋์์์ผ๋ ์ํ๋ ์์ค์์
์กฐ๊ธ์ฉ ์์ ํด์ ์ฌ์ฉํ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค.
* ์ต์ข ์์ค
import React, { useState } from "react";
import { getYear, getMonth } from "date-fns"; // getYear, getMonth
import DatePicker, { registerLocale } from "react-datepicker"; // ํ๊ตญ์ด์ ์ฉ
import ko from 'date-fns/locale/ko'; // ํ๊ตญ์ด์ ์ฉ
registerLocale("ko", ko) // ํ๊ตญ์ด์ ์ฉ
const _ = require('lodash');
const Calendar = () => {
const [startDate, setStartDate] = useState(new Date());
const years = _.range(1990, getYear(new Date()) + 1, 1); // ์์
const months = ['1์','2์','3์','4์','5์','6์','7์','8์','9์','10์','11์','12์'];
return (
<DatePicker
renderCustomHeader={({
date,
changeYear,
changeMonth,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled
}) => (
<div
style={{
margin: 10,
display: "flex",
justifyContent: "center"
}}
>
<button onClick={decreaseMonth} disabled={prevMonthButtonDisabled}>
{"<"}
</button>
<select
value={getYear(date)}
onChange={({ target: { value } }) => changeYear(value)}
>
{years.map(option => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<select
value={months[getMonth(date)]}
onChange={({ target: { value } }) =>
changeMonth(months.indexOf(value))
}
>
{months.map(option => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<button onClick={increaseMonth} disabled={nextMonthButtonDisabled}>
{">"}
</button>
</div>
)}
selected={startDate}
dateFormat={"yyyy-MM-dd"}
locale={ko}
onChange={date => setStartDate(date)}
/>
);
};
export default Calendar
๊ทธ๋ผ ์ค๋๋ ์ฆ๊ฑฐ์ด ์ฝ๋ฉ ๐ฅฐ
'๊ฐ๋ฐ ๐พ > ReactJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React][ํ๊ฒฝ์ธํ ] react-router-dom ์ฌ์ฉํ๊ธฐ (0) | 2021.05.25 |
---|---|
[์๋ฌ๋ ธํธ] defaultState for reducer handling [object Object] should be defined (0) | 2021.03.17 |
React Hook ์์ Scroll ์ปจํธ๋กค ํ๊ธฐ! top๋ฒํผ ๊ตฌํ (1) | 2021.02.09 |
๋ชจ๋๊ฐ ์์ฃผ ์ฌ์ฉํ๋ React ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ๋ฆฌ (0) | 2021.02.06 |
React ์คํ์ผ๋ง - emotion (3) | 2021.02.01 |