๊ฐœ๋ฐœ ๐Ÿพ/ReactJS

React-datepicker ์ปค์Šคํ…€ ํ•˜๊ธฐ

JOTOKKI 2021. 2. 2. 19:30
728x90

๋‹ฌ๋ ฅ์„ ์“ธ ์ผ์ด ์žˆ์–ด์„œ 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

 

2. ๊ธฐ๋ณธ datepicker ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ ํ›„ App.js ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
// 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;

 

react-datepicker ๊ธฐ๋ณธ

์•„์ฃผ ์ž˜ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

 

๊ทธ๋Ÿฐ๋ฐ ์ €๋Š” ๊ธฐ๋ณธ ๊ทธ๋Œ€๋กœ๋ฅผ ์“ฐ๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š” ํ•„์š”์— ๋งž๊ฒŒ ์ปค์Šคํ…€์„ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. 

 

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

 

๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ์ฆ๊ฑฐ์šด ์ฝ”๋”ฉ ๐Ÿฅฐ

๋ฐ˜์‘ํ˜•