import React from 'react'; import PropTypes from 'prop-types'; import { forbidExtraProps, nonNegativeInteger } from 'airbnb-prop-types'; import { css, withStyles, withStylesPropTypes } from 'react-with-styles'; import throttle from 'lodash/throttle'; import isTouchDevice from 'is-touch-device'; import noflip from '../utils/noflip'; import getInputHeight from '../utils/getInputHeight'; import openDirectionShape from '../shapes/OpenDirectionShape'; import { OPEN_DOWN, OPEN_UP, FANG_HEIGHT_PX, FANG_WIDTH_PX, DEFAULT_VERTICAL_SPACING, MODIFIER_KEY_NAMES, } from '../constants'; const FANG_PATH_TOP = `M0,${FANG_HEIGHT_PX} ${FANG_WIDTH_PX},${FANG_HEIGHT_PX} ${FANG_WIDTH_PX / 2},0z`; const FANG_STROKE_TOP = `M0,${FANG_HEIGHT_PX} ${FANG_WIDTH_PX / 2},0 ${FANG_WIDTH_PX},${FANG_HEIGHT_PX}`; const FANG_PATH_BOTTOM = `M0,0 ${FANG_WIDTH_PX},0 ${FANG_WIDTH_PX / 2},${FANG_HEIGHT_PX}z`; const FANG_STROKE_BOTTOM = `M0,0 ${FANG_WIDTH_PX / 2},${FANG_HEIGHT_PX} ${FANG_WIDTH_PX},0`; const propTypes = forbidExtraProps({ ...withStylesPropTypes, id: PropTypes.string.isRequired, placeholder: PropTypes.string, displayValue: PropTypes.string, ariaLabel: PropTypes.string, screenReaderMessage: PropTypes.string, focused: PropTypes.bool, disabled: PropTypes.bool, required: PropTypes.bool, readOnly: PropTypes.bool, openDirection: openDirectionShape, showCaret: PropTypes.bool, verticalSpacing: nonNegativeInteger, small: PropTypes.bool, block: PropTypes.bool, regular: PropTypes.bool, onChange: PropTypes.func, onFocus: PropTypes.func, onKeyDownShiftTab: PropTypes.func, onKeyDownTab: PropTypes.func, onKeyDownArrowDown: PropTypes.func, onKeyDownQuestionMark: PropTypes.func, // accessibility isFocused: PropTypes.bool, // describes actual DOM focus }); const defaultProps = { placeholder: 'Select Date', displayValue: '', ariaLabel: undefined, screenReaderMessage: '', focused: false, disabled: false, required: false, readOnly: null, openDirection: OPEN_DOWN, showCaret: false, verticalSpacing: DEFAULT_VERTICAL_SPACING, small: false, block: false, regular: false, onChange() {}, onFocus() {}, onKeyDownShiftTab() {}, onKeyDownTab() {}, onKeyDownArrowDown() {}, onKeyDownQuestionMark() {}, // accessibility isFocused: false, }; class DateInput extends React.PureComponent { constructor(props) { super(props); this.state = { dateString: '', isTouchDevice: false, }; this.onChange = this.onChange.bind(this); this.onKeyDown = this.onKeyDown.bind(this); this.setInputRef = this.setInputRef.bind(this); this.throttledKeyDown = throttle(this.onFinalKeyDown, 300, { trailing: false }); } componentDidMount() { this.setState({ isTouchDevice: isTouchDevice() }); } componentWillReceiveProps(nextProps) { const { dateString } = this.state; if (dateString && nextProps.displayValue) { this.setState({ dateString: '', }); } } componentDidUpdate(prevProps) { const { focused, isFocused } = this.props; if (prevProps.focused === focused && prevProps.isFocused === isFocused) return; if (focused && isFocused) { this.inputRef.focus(); } } onChange(e) { const { onChange, onKeyDownQuestionMark } = this.props; const dateString = e.target.value; // In Safari, onKeyDown does not consistently fire ahead of onChange. As a result, we need to // special case the `?` key so that it always triggers the appropriate callback, instead of // modifying the input value if (dateString[dateString.length - 1] === '?') { onKeyDownQuestionMark(e); } else { this.setState({ dateString }, () => onChange(dateString)); } } onKeyDown(e) { e.stopPropagation(); if (!MODIFIER_KEY_NAMES.has(e.key)) { this.throttledKeyDown(e); } } onFinalKeyDown(e) { const { onKeyDownShiftTab, onKeyDownTab, onKeyDownArrowDown, onKeyDownQuestionMark, } = this.props; const { key } = e; if (key === 'Tab') { if (e.shiftKey) { onKeyDownShiftTab(e); } else { onKeyDownTab(e); } } else if (key === 'ArrowDown') { onKeyDownArrowDown(e); } else if (key === '?') { e.preventDefault(); onKeyDownQuestionMark(e); } } setInputRef(ref) { this.inputRef = ref; } render() { const { dateString, isTouchDevice: isTouch, } = this.state; const { id, placeholder, ariaLabel, displayValue, screenReaderMessage, focused, showCaret, onFocus, disabled, required, readOnly, openDirection, verticalSpacing, small, regular, block, styles, theme: { reactDates }, } = this.props; const value = dateString || displayValue || ''; const screenReaderMessageId = `DateInput__screen-reader-message-${id}`; const withFang = showCaret && focused; const inputHeight = getInputHeight(reactDates, small); return (
{screenReaderMessage}
)}