15.2.13 • Published 2 years ago

bpk-component-datepicker-css v15.2.13

Weekly downloads
290
License
Apache-2.0
Repository
github
Last release
2 years ago

bpk-component-datepicker

Backpack datepicker component.

Installation

npm install bpk-component-datepicker --save-dev

Usage

import React, { Component } from 'react';
import BpkDatepicker, { CALENDAR_SELECTION_TYPE } from 'bpk-component-datepicker';
import format from 'date-fns/format';

const formatDate = date => format(date, 'dd/MM/yyyy');
const formatDateFull = date => format(date, 'do MMMM yyyy');
const formatMonth = date => format(date, 'MMMM yyyy');
const daysOfWeek = [
  {
    name: 'Sunday',
    nameAbbr: 'Sun',
    index: 0,
    isWeekend: true,
  },
  // ...
];

export default class App extends Component {
  constructor() {
    super();

    this.state = {
      selectionConfiguration: {
        type: CALENDAR_SELECTION_TYPE.single,
        date: null,
      }
    }
  }

  handleDateSelect = (date) => {
    this.setState({
      selectionConfiguration: {
        type: this.props.selectionConfiguration.type,
        date: date,
      },
    });
  }

  render() {
    return (
      <BpkDatepicker
        id="datepicker"
        daysOfWeek={daysOfWeek}
        weekStartsOn={1}
        changeMonthLabel="Change month"
        closeButtonText="Close"
        title="Departure date"
        getApplicationElement={() => document.getElementById('pagewrap')}
        formatDate={formatDate}
        formatMonth={formatMonth}
        formatDateFull={formatDateFull}
        onDateSelect={this.handleDateSelect}
        selectionConfiguration={this.state.selectionConfiguration}
      />
    );
  }
}

By default BpkCalendar is used but the calendar component is fully configurable through the calendarComponent prop.

import React, { Component } from 'react';
import BpkDatepicker from 'bpk-component-datepicker';
import {
  BpkCalendarNav,
  BpkCalendarGridHeader,
  BpkCalendarGridWithTransition,
  BpkCalendarDate,
  withCalendarState,
  composeCalendar,
} from 'bpk-component-calendar';
import { colorSagano } from '@skyscanner/bpk-foundations-web/tokens/base.es6';

const ColoredCalendarDate = props =>
  <BpkCalendarDate {...props} style={{ backgroundColor: colorSagano }} />;

const CalendarWithColoredDates = withCalendarState(
  composeCalendar(
    BpkCalendarNav,
    BpkCalendarGridHeader,
    BpkCalendarGridWithTransition,
    ColoredCalendarDate,
  ),
);

const CustomPicker = () => (
  <BpkDatepicker
    id="datepicker"
    calendarComponent={CalendarWithColoredDates}
    daysOfWeek={daysOfWeek}
    weekStartsOn={1}
    changeMonthLabel="Change month"
    closeButtonText="Close"
    title="Departure date"
    getApplicationElement={() => document.getElementById('pagewrap')}
    formatDate={formatDate}
    formatMonth={formatMonth}
    formatDateFull={formatDateFull}
    onDateSelect={this.handleDateSelect}
    selectionConfiguration={this.state.selectionConfiguration}
  />
);

Theming: In order to theme the modal, a renderTarget needs to be supplied as a function which returns a DOM node in the scope of a BpkThemeProvider.

Props

For more information on some these props, check the BpkCalendar documentation.

Note: Because this component uses a modal on mobile viewports, you need to let it know what the root element of your application is by returning it's DOM node via the function passed to the getApplicationElement prop (see the example above) - this is to "hide" your application from screen readers whilst the datepicker is open. The pagewrap element id is a convention we use internally at Skyscanner. In most cases it should "just work".

PropertyPropTypeRequiredDefault Value
changeMonthLabelstringtrue-
closeButtonTextstringtrue-
titlestringtrue-
idstringtrue-
getApplicationElementfunctrue-
daysOfWeekarrayOf(object)true-
weekStartsOnnumbertrue-
formatDatefunctrue-
formatDateFullfunctrue-
formatMonthfunctrue-
calendarComponentelementTypefalseBpkCalendar
inputComponentelementTypefalseBpkInput
dateModifiersobjectfalse{} (*)
fixedWidthboolfalsetrue
inputPropsobjectfalse{}
markOutsideDaysboolfalsetrue (*)
markTodayboolfalsetrue (*)
maxDateDatefalsenew Date() + 1 year (*)
minDateDatefalsenew Date() (*)
onDateSelectfuncfalsenull
initiallyFocusedDateDatefalsenull
renderTargetfuncfalsenull
isOpenboolfalsefalse
onOpenChangefuncfalsenull
validboolfalsenull
selectionConfigurationobjectfalse{ type: CALENDAR_SELECTION_TYPE.single, date: null }

(*) Default value is defined on child component

Theme Props

  • calendarDateTextColor
  • calendarDateTextHoverColor
  • calendarDateTextActiveColor
  • calendarDateTextFocusColor
  • calendarDateTextSelectedColor
  • calendarDateSelectedBackgroundColor
  • calendarDateFocusedBorderColor
  • calendarNudgerIconColor
  • calendarNudgerIconHoverColor
  • calendarNudgerIconActiveColor
  • linkColor
  • linkHoverColor
  • linkActiveColor
  • linkVisitedColor
15.2.13

2 years ago

15.2.11

2 years ago

15.2.9

2 years ago

15.2.7

2 years ago

15.2.5

2 years ago

15.2.2

2 years ago

15.2.3

2 years ago

15.2.6

2 years ago

15.2.1

2 years ago

14.1.0

2 years ago

14.1.1

2 years ago

15.0.1

2 years ago

15.1.10

2 years ago

15.1.5

2 years ago

15.1.4

2 years ago

15.1.7

2 years ago

14.0.0

3 years ago

13.0.18

3 years ago

13.0.15

3 years ago

13.0.13

3 years ago

13.0.12

3 years ago

13.0.11

3 years ago

13.0.10

3 years ago

13.0.9

3 years ago

13.0.8

3 years ago

13.0.7

3 years ago

13.0.6

3 years ago

13.0.5

3 years ago

13.0.4

3 years ago

13.0.3

3 years ago

13.0.2

3 years ago

13.0.1

3 years ago

13.0.0

3 years ago

12.1.6

3 years ago

12.1.8

3 years ago

12.1.5

3 years ago

12.1.4

3 years ago

12.1.3

3 years ago

12.1.2

3 years ago

12.1.1

3 years ago

12.1.0

3 years ago

12.0.1

3 years ago

12.0.0

3 years ago

11.3.57

3 years ago

11.3.56

3 years ago

11.3.55

3 years ago

11.3.51

3 years ago

11.3.53

3 years ago

11.3.52

3 years ago

11.3.50

3 years ago

11.3.49

3 years ago

11.3.48

3 years ago

11.3.46

3 years ago

11.3.45

3 years ago

11.3.47

3 years ago

11.3.44

3 years ago

11.3.43

3 years ago

11.3.42

3 years ago

11.3.41

3 years ago

11.3.40

3 years ago

11.3.39

3 years ago

11.3.38

3 years ago

11.3.37

3 years ago

11.3.36

3 years ago

11.3.35

3 years ago

11.3.34

3 years ago

11.3.33

3 years ago

11.3.32

3 years ago

11.3.31

3 years ago

11.3.30

3 years ago

11.3.29

3 years ago

11.3.28

3 years ago

11.3.27

3 years ago

11.3.26

3 years ago

11.3.25

3 years ago

11.3.24

3 years ago

11.3.23

3 years ago

11.3.22

3 years ago

11.3.20

3 years ago

11.3.21

3 years ago

11.3.19

3 years ago

11.3.18

3 years ago

11.3.15

3 years ago

11.3.14

3 years ago

11.3.13

3 years ago

11.3.12

3 years ago

11.3.11

4 years ago

11.3.10

4 years ago

11.3.9

4 years ago

11.3.8

4 years ago

11.3.6

4 years ago

11.3.5

4 years ago

11.3.4

4 years ago

11.3.3

4 years ago

11.3.2

4 years ago

11.3.1

4 years ago

11.3.0

4 years ago

11.2.40

4 years ago

11.2.39

4 years ago

11.2.38

4 years ago

11.2.36

4 years ago

11.2.35

4 years ago

11.2.34

4 years ago

11.2.29

4 years ago

11.2.26

4 years ago

11.2.24

4 years ago

11.2.23

4 years ago

11.2.22

4 years ago

11.2.21

4 years ago