@material-ui/lab
Version:
Material-UI Lab - Incubator for Material-UI React components.
144 lines (130 loc) • 4.85 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import * as React from 'react';
import { withStyles, createStyles } from '@material-ui/core/styles';
import { useUtils } from '../internal/pickers/hooks/useUtils';
import { useMaskedInput } from '../internal/pickers/hooks/useMaskedInput';
import { WrapperVariantContext } from '../internal/pickers/wrappers/WrapperVariantContext';
import { mergeRefs, executeInTheNextEventLoopTick } from '../internal/pickers/utils';
export const styles = theme => createStyles({
root: {
display: 'flex',
alignItems: 'baseline',
[theme.breakpoints.down('xs')]: {
flexDirection: 'column',
alignItems: 'center'
}
},
toLabelDelimiter: {
margin: '8px 0',
[theme.breakpoints.up('sm')]: {
margin: '0 16px'
}
}
});
/**
* @ignore - internal component.
*/
const DateRangePickerInput = (_ref) => {
let {
classes,
containerRef,
currentlySelectingRangeEnd,
disableOpenPicker,
endText,
forwardedRef,
onBlur,
onChange,
open,
openPicker,
rawValue: [start, end],
readOnly,
renderInput,
setCurrentlySelectingRangeEnd,
startText,
TextFieldProps,
validationError: [startValidationError, endValidationError]
} = _ref,
other = _objectWithoutPropertiesLoose(_ref, ["classes", "containerRef", "currentlySelectingRangeEnd", "disableOpenPicker", "endText", "forwardedRef", "onBlur", "onChange", "open", "openPicker", "rawValue", "rawValue", "readOnly", "renderInput", "setCurrentlySelectingRangeEnd", "startText", "TextFieldProps", "validationError"]);
const utils = useUtils();
const startRef = React.useRef(null);
const endRef = React.useRef(null);
const wrapperVariant = React.useContext(WrapperVariantContext);
React.useEffect(() => {
if (!open) {
return;
}
if (currentlySelectingRangeEnd === 'start') {
var _startRef$current;
(_startRef$current = startRef.current) === null || _startRef$current === void 0 ? void 0 : _startRef$current.focus();
} else if (currentlySelectingRangeEnd === 'end') {
var _endRef$current;
(_endRef$current = endRef.current) === null || _endRef$current === void 0 ? void 0 : _endRef$current.focus();
}
}, [currentlySelectingRangeEnd, open]); // TODO: rethink this approach. We do not need to wait for calendar to be updated to rerender input (looks like freezing)
// TODO: so simply break 1 react's commit phase in 2 (first for input and second for calendars) by executing onChange in the next tick
const lazyHandleChangeCallback = React.useCallback((...args) => executeInTheNextEventLoopTick(() => onChange(...args)), [onChange]);
const handleStartChange = (date, inputString) => {
lazyHandleChangeCallback([date, utils.date(end)], inputString);
};
const handleEndChange = (date, inputString) => {
lazyHandleChangeCallback([utils.date(start), date], inputString);
};
const openRangeStartSelection = () => {
if (setCurrentlySelectingRangeEnd) {
setCurrentlySelectingRangeEnd('start');
}
if (!disableOpenPicker) {
openPicker();
}
};
const openRangeEndSelection = () => {
if (setCurrentlySelectingRangeEnd) {
setCurrentlySelectingRangeEnd('end');
}
if (!disableOpenPicker) {
openPicker();
}
};
const openOnFocus = wrapperVariant === 'desktop';
const startInputProps = useMaskedInput(_extends({}, other, {
readOnly,
rawValue: start,
onChange: handleStartChange,
label: startText,
validationError: startValidationError !== null,
TextFieldProps: _extends({}, TextFieldProps, {
ref: startRef,
variant: 'outlined',
focused: open && currentlySelectingRangeEnd === 'start'
}),
inputProps: {
onClick: !openOnFocus ? openRangeStartSelection : undefined,
onFocus: openOnFocus ? openRangeStartSelection : undefined
}
}));
const endInputProps = useMaskedInput(_extends({}, other, {
readOnly,
label: endText,
rawValue: end,
onChange: handleEndChange,
validationError: endValidationError !== null,
TextFieldProps: _extends({}, TextFieldProps, {
ref: endRef,
variant: 'outlined',
focused: open && currentlySelectingRangeEnd === 'end'
}),
inputProps: {
onClick: !openOnFocus ? openRangeEndSelection : undefined,
onFocus: openOnFocus ? openRangeEndSelection : undefined
}
}));
return /*#__PURE__*/React.createElement("div", {
onBlur: onBlur,
className: classes.root,
ref: mergeRefs([containerRef, forwardedRef])
}, renderInput(startInputProps, endInputProps));
};
export default withStyles(styles, {
name: 'MuiPickersDateRangePickerInput'
})(DateRangePickerInput);