All files / lib/DatePicker index.js

96.97% Statements 64/66
84.78% Branches 39/46
100% Functions 16/16
96.97% Lines 64/66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304                                                              44x 44x 44x 3x 2x             10x   10x     10x 10x       35x 35x   35x   35x       1x 1x         17x           4x 4x 4x 4x       15x   15x     15x       20x   20x     20x       9x       6x   6x 6x   6x     1x   1x       1x   1x 1x     1x 1x 1x   1x 1x 1x     2x 2x 2x     1x 1x 1x           6x 6x 6x                       45x   45x   45x 79x         45x     45x               45x       6x                 45x                             89x                                                                         89x                                       89x             89x                                                                
/**
 * @category controls
 * @component date-picker
 * @variations collab-ui-react
 */
 
import React from 'react';
import PropTypes from 'prop-types';
import { EventOverlay } from '@collab-ui/react';
import DatePickerCalendar from '@collab-ui/react/DatePicker/DatePickerCalendar';
import {
  addDays,
  addWeeks,
  isDayDisabled,
  isSameDay,
  subtractDays,
  subtractWeeks,
} from '@collab-ui/react/utils/dateUtils';
import moment from 'moment';
import { omit } from 'lodash';
 
class DatePicker extends React.Component {
 
  state = {
    anchorNode: null,
    focus: null,
    isOpen: false,
    selected: null,
  };
 
  getChildContext = () => {
    const { onMonthChange } = this.props;
    const { focus, selected } = this.state;
    return {
      handleDayClick: (event, date) => this.handleSelect(event, date),
      handleMonthChange: (event, date) => onMonthChange && onMonthChange(event, date.toDate()),
      focus,
      selected,
    };
  };
 
  componentWillMount() {
    const selectedDate = moment(this.props.selectedDate);
    const isValid =
      selectedDate.isValid()
      && !isDayDisabled(selectedDate, this.props);
 
    isValid && this.setPreSelection(selectedDate);
    isValid && this.setSelected(selectedDate);
  }
 
  componentDidUpdate (prevProps) {
    const selectedDate = moment(this.props.selectedDate);
    const prevSelectedDate = moment(prevProps.selectedDate);
    const isValid =
      selectedDate.isValid()
      && !isDayDisabled(selectedDate, this.props);
    if(
      isValid
      && !isSameDay(prevSelectedDate, selectedDate)
    ) {
      this.setSelected(selectedDate);
      this.setPreSelection(selectedDate);
    }
  }
 
  setOpen = open => {
    this.setState({
      isOpen: open,
    });
  };
 
  handleSelect = (event, date) => {
    const { shouldCloseOnSelect } = this.props;
    this.setPreSelection(date, event);
    this.setSelected(date, event);
    shouldCloseOnSelect && this.setOpen(false);
  };
 
  setSelected = (date, event) => {
    const { onSelect } = this.props;
 
    !isDayDisabled(date, this.props)
    && this.setState({
      selected: date,
    }, () => onSelect && onSelect(event, date.toDate()));
  };
 
  setPreSelection = (date, event) => {
    const { onChange } = this.props;
 
    !isDayDisabled(date, this.props)
    && this.setState({
      focus: date,
    }, () => onChange && onChange(event, date.toDate()));
  };
 
  handleInputClick = () => {
    this.setOpen(true);
  };
 
  handleInputKeyDown = event => {
    const { focus, isOpen } = this.state;
 
    let flag = false;
    const copy = moment(focus);
 
    switch (!event.shiftKey && event.which) {
      case 32:
      case 13:
        Iif(!isOpen) {
          this.handleInputClick();
        } else Eif (
          moment.isMoment(focus) ||
          moment.isDate(focus)
        ) {
          this.handleSelect(event, copy);
        }
        flag = true;
        break;
 
      case 38: //up
        this.setPreSelection(subtractWeeks(copy, 1));
        flag = true;
        break;
      case 37:// left
        this.setPreSelection(subtractDays(copy, 1));
        flag = true;
        break;
 
      case 39: //right
        this.setPreSelection(addDays(copy, 1));
        flag = true;
        break;
 
      case 40: //bottom
        this.setPreSelection(addWeeks(copy, 1));
        flag = true;
        break;
 
      default:
        break;
    }
 
    Eif (flag) {
      event.stopPropagation();
      event.preventDefault();
    }
  };
 
  render() {
    const {
      children,
      className,
      direction,
      isDynamic,
      showArrow,
      ...props
    } = this.props;
 
    const { selected, focus, anchorNode, isOpen } = this.state;
 
    const trigger = React.cloneElement(children, {
      ref: ref => !this.state.anchorNode && this.setState({ anchorNode: ref}),
      onClick: this.handleInputClick,
      onKeyDown: this.handleInputKeyDown,
    });
 
    const otherProps = omit({...props}, ['onSelect', 'onChange', 'onMonthChange', 'shouldCloseOnSelect']);
 
    const calendar = (
      <DatePickerCalendar
        focus={focus}
        selected={selected}
        {...otherProps}
      />
    );
 
    const content = (
      <EventOverlay
        allowClickAway
        isOpen={isOpen}
        anchorNode={anchorNode}
        close={() => this.setOpen(false)}
        direction={direction}
        showArrow={showArrow}
        isDynamic={isDynamic}
      >
        {calendar}
      </EventOverlay>
    );
 
    return (
      <div
        className={
          'cui-datepicker-container' +
          `${(className && ` ${className}`) || ''}`
        }
      >
        {trigger}
        {content}
      </div>
    );
  }
}
 
 
DatePicker.propTypes = {
  /** @prop Children nodes to render inside DatePicker | null */
  children: PropTypes.element,
  /** @prop Optional css class string | '' */
  className: PropTypes.string,
  /** @prop Set the direction in which the DatePicker opens | 'bottom-center' */
  direction: PropTypes.string,
  /** @prop Function to filter Dates | null */
  filterDate: PropTypes.func,
  /** @prop Sets the DatePicker EventOverlay to be dynamic | true */
  isDynamic: PropTypes.bool,
  /** @prop Sets the language of the DatePicker | 'en' */
  locale: PropTypes.string,
  /** @prop Sets the last date in which the DatePicker does not disable | null */
  maxDate: PropTypes.instanceOf(Date),
  /** @prop Sets the first date in which the DatePicker does not disable | null */
  minDate: PropTypes.instanceOf(Date),
  /** @prop Sets the format of the month | 'MMMM YYYY' */
  monthFormat: PropTypes.string,
  /** @prop Text to display for blindness accessibility features | 'next' */
  nextArialLabel: PropTypes.string,
  /** @prop Handler invoked when user makes a chnage within the DatePicker | null */
  onChange: PropTypes.func,
  /** @prop Handler invoked when user makes a change to the selected month within DatePicker | null */
  onMonthChange: PropTypes.func,
  /** @prop Handler invoked when user selects a date within DatePicker | null */
  onSelect: PropTypes.func,
  /** @prop Text to display for blindness accessibility features | 'previous' */
  previousArialLabel: PropTypes.string,
  /** @prop Initial Selected Date for DatePicker | moment().toDate()  */
  selectedDate: PropTypes.instanceOf(Date),
  /** @prop Determines if the DatePicker should close when a date is selected | true */
  shouldCloseOnSelect: PropTypes.bool,
  /** @prop Determines if the DatePicker should show the open/close arrow | false */
  showArrow: PropTypes.bool,
};
 
DatePicker.defaultProps = {
  children: null,
  className: '',
  direction: 'bottom-center',
  filterDate: null,
  isDynamic: true,
  locale: 'en',
  maxDate: null,
  minDate: null,
  monthFormat: 'MMMM YYYY',
  nextArialLabel: 'next',
  onChange: null,
  onMonthChange: null,
  onSelect: null,
  previousArialLabel: 'previous',
  selectedDate: moment().toDate(),
  shouldCloseOnSelect: true,
  showArrow: false,
};
 
DatePicker.childContextTypes = {
  focus: PropTypes.instanceOf(moment),
  handleDayClick: PropTypes.func,
  handleMonthChange: PropTypes.func,
  selected: PropTypes.instanceOf(moment),
};
 
DatePicker.displayName = 'DatePicker';
 
 
export default DatePicker;
 
/**
* @component date-picker
* @section default
* @react
 
import { Button, DatePicker } from '@collab-ui/react';
 
export default class DatePickerDefault extends React.PureComponent {
  state = {
    date: null,
  };
  render() {
    const { date } = this.state;
    return (
      <span>
        <h4 className="columns">Selected Date: {date && date.toDateString()}</h4>
        <DatePicker onSelect={(e, date) => this.setState({ date })}>
          <Button
            children='Pick a Date'
            ariaLabel='DatePicker'
          />
        </DatePicker>
      </span>
    );
  }
}
**/