@material-ui/lab
Version:
Material-UI Lab - Incubator for Material-UI React components.
64 lines (58 loc) • 2.63 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import * as React from 'react';
import Picker from './Picker';
import { parsePickerInputValue } from '../date-utils';
import { withDefaultProps } from '../withDefaultProps';
import { KeyboardDateInput } from '../KeyboardDateInput';
import { withDateAdapterProp } from '../withDateAdapterProp';
import { makeWrapperComponent } from '../wrappers/makeWrapperComponent';
import { PureDateInput } from '../PureDateInput';
import { usePickerState } from '../hooks/usePickerState';
const valueManager = {
emptyValue: null,
parseInput: parsePickerInputValue,
areValuesEqual: (utils, a, b) => utils.isEqual(a, b)
};
export function makePickerWithStateAndWrapper(Wrapper, {
name,
useInterceptProps,
useValidation,
DefaultToolbarComponent
}) {
const WrapperComponent = makeWrapperComponent(Wrapper, {
KeyboardDateInputComponent: KeyboardDateInput,
PureDateInputComponent: PureDateInput
});
function PickerWithState(__props) {
const allProps = useInterceptProps(__props);
const validationError = useValidation(allProps.value, allProps) !== null;
const {
pickerProps,
inputProps,
wrapperProps
} = usePickerState(allProps, valueManager); // Note that we are passing down all the value without spread.
// It saves us >1kb gzip and make any prop available automatically on any level down.
const other = _objectWithoutPropertiesLoose(allProps, ["value", "onChange"]);
const AllDateInputProps = _extends({}, inputProps, other, {
validationError
});
return /*#__PURE__*/React.createElement(WrapperComponent, _extends({
wrapperProps: wrapperProps,
DateInputProps: AllDateInputProps
}, other), /*#__PURE__*/React.createElement(Picker, _extends({}, pickerProps, {
toolbarTitle: allProps.label || allProps.toolbarTitle,
ToolbarComponent: other.ToolbarComponent || DefaultToolbarComponent,
DateInputProps: AllDateInputProps
}, other)));
}
const FinalPickerComponent = withDefaultProps({
name
}, withDateAdapterProp(PickerWithState)); // tslint:disable-next-line
// @ts-ignore Simply ignore generic values in props, because it is impossible
// to keep generics without additional cast when using forwardRef
// @see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/35834
return /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(FinalPickerComponent, _extends({}, props, {
forwardedRef: ref
})));
}