Airbnb clone, add the calendar to the page

This post is part of a new series where we build a clone of Airbnb with Next.js. See the first post here.

Now that we have the react-day-picker library in place, we can start using it.

Let’s first define a component to hold it. We call it DateRangePicker. I give it this name since we must define the range of dates for the booking.

Create a components/DateRangePicker.js file. In it, we import the react-day-picker component, and we just output a string to make sure our component displays in the page:

import DayPickerInput from 'react-day-picker/DayPickerInput'

export default function DateRangePicker() {
  return (
    <div>
      <DayPickerInput />
    </div>
  )
}

Import this in the pages/houses/[id].js file:

pages/houses/[id].js

//...
import DateRangePicker from '../../components/DateRangePicker'

export default function House(props) {
  return (
    <Layout
    content={
      <div className='container'>
        ...
        <aside>
            <h2>Choose a date</h2>
            <DateRangePicker />
        </aside>
        ...
      </div>
    }
    />
  )
)

Here’s the output

But if you click the input element, ooops:

Go back to components/DateRangePicker.js and also import the component’s CSS file provided in the npm package (not every package has a CSS file, this was mentioned in the package documentation):

components/DateRangePicker.js

import DayPickerInput from 'react-day-picker/DayPickerInput'
import 'react-day-picker/lib/style.css'

export default () => (
  <div>
    <DayPickerInput />
  </div>
)

Now we’ll get the date picker rendered nicely:

Great!

Now let’s go back to our components/DateRangePicker.js file. We need to have a “check-in” box, and a “check-out” box. We can put them in 2 separate rows, with 2 labels:

export default function DateRangePicker() {
  return (
    <div className="date-range-picker-container">
      <div>
        <label>From:</label>
        <DayPickerInput />
      </div>
      <div>
        <label>To:</label>
        <DayPickerInput />
      </div>
    </div>
  )
}

Let’s add this bit of CSS to display this markup nicely:

import DayPickerInput from 'react-day-picker/DayPickerInput'
import 'react-day-picker/lib/style.css'

export default function DateRangePicker() {
  return (
    <div className="date-range-picker-container">
      <div>
        <label>From:</label>
        <DayPickerInput />
      </div>
      <div>
        <label>To:</label>
        <DayPickerInput />
      </div>
      <style jsx>
        {`
          .date-range-picker-container div {
            display: grid;
            grid-template-columns: 30% 70%;
            padding: 10px;
          }
          label {
            padding-top: 10px;
          }
        `}
      </style>
      <style jsx global>
        {`
          .DayPickerInput input {
            width: 120px;
            padding: 10px;
            font-size: 16px;
          }
        `}
      </style>
    </div>
  )
}

This should be the final result of our lesson:

See the code on GitHub


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This post is part of a new series where we build a clone of Airbnb with Next.js. See the first post here.

Now that we have the react-day-picker library in place, we can start using it.

Let’s first define a component to hold it. We call it DateRangePicker. I give it this name since we must define the range of dates for the booking.

Create a components/DateRangePicker.js file. In it, we import the react-day-picker component, and we just output a string to make sure our component displays in the page:

import DayPickerInput from 'react-day-picker/DayPickerInput'

export default function DateRangePicker() {
  return (
    <div>
      <DayPickerInput />
    </div>
  )
}

Import this in the pages/houses/[id].js file:

pages/houses/[id].js

//...
import DateRangePicker from '../../components/DateRangePicker'

export default function House(props) {
  return (
    <Layout
    content={
      <div className='container'>
        ...
        <aside>
            <h2>Choose a date</h2>
            <DateRangePicker />
        </aside>
        ...
      </div>
    }
    />
  )
)

Here’s the output

But if you click the input element, ooops:

Go back to components/DateRangePicker.js and also import the component’s CSS file provided in the npm package (not every package has a CSS file, this was mentioned in the package documentation):

components/DateRangePicker.js

import DayPickerInput from 'react-day-picker/DayPickerInput'
import 'react-day-picker/lib/style.css'

export default () => (
  <div>
    <DayPickerInput />
  </div>
)

Now we’ll get the date picker rendered nicely:

Great!

Now let’s go back to our components/DateRangePicker.js file. We need to have a “check-in” box, and a “check-out” box. We can put them in 2 separate rows, with 2 labels:

export default function DateRangePicker() {
  return (
    <div className="date-range-picker-container">
      <div>
        <label>From:</label>
        <DayPickerInput />
      </div>
      <div>
        <label>To:</label>
        <DayPickerInput />
      </div>
    </div>
  )
}

Let’s add this bit of CSS to display this markup nicely:

import DayPickerInput from 'react-day-picker/DayPickerInput'
import 'react-day-picker/lib/style.css'

export default function DateRangePicker() {
  return (
    <div className="date-range-picker-container">
      <div>
        <label>From:</label>
        <DayPickerInput />
      </div>
      <div>
        <label>To:</label>
        <DayPickerInput />
      </div>
      <style jsx>
        {`
          .date-range-picker-container div {
            display: grid;
            grid-template-columns: 30% 70%;
            padding: 10px;
          }
          label {
            padding-top: 10px;
          }
        `}
      </style>
      <style jsx global>
        {`
          .DayPickerInput input {
            width: 120px;
            padding: 10px;
            font-size: 16px;
          }
        `}
      </style>
    </div>
  )
}

This should be the final result of our lesson:

See the code on GitHub


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-12-08T05:00:00+00:00) Airbnb clone, add the calendar to the page. Retrieved from https://www.scien.cx/2021/12/08/airbnb-clone-add-the-calendar-to-the-page/

MLA
" » Airbnb clone, add the calendar to the page." flaviocopes.com | Sciencx - Wednesday December 8, 2021, https://www.scien.cx/2021/12/08/airbnb-clone-add-the-calendar-to-the-page/
HARVARD
flaviocopes.com | Sciencx Wednesday December 8, 2021 » Airbnb clone, add the calendar to the page., viewed ,<https://www.scien.cx/2021/12/08/airbnb-clone-add-the-calendar-to-the-page/>
VANCOUVER
flaviocopes.com | Sciencx - » Airbnb clone, add the calendar to the page. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/08/airbnb-clone-add-the-calendar-to-the-page/
CHICAGO
" » Airbnb clone, add the calendar to the page." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/12/08/airbnb-clone-add-the-calendar-to-the-page/
IEEE
" » Airbnb clone, add the calendar to the page." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/12/08/airbnb-clone-add-the-calendar-to-the-page/. [Accessed: ]
rf:citation
» Airbnb clone, add the calendar to the page | flaviocopes.com | Sciencx | https://www.scien.cx/2021/12/08/airbnb-clone-add-the-calendar-to-the-page/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.