@talend/react-components
Version:
Set of react components.
96 lines • 2.73 kB
JavaScript
import { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { DateContext } from '../Context';
import { checkSupportedDateFormat, checkSupportedTimezone, extractDate, extractFromDate, extractPartsFromTextInput } from '../date-extraction';
import { jsx as _jsx } from "react/jsx-runtime";
function ContextualManager(props) {
function getDateOptions() {
return {
dateFormat: props.dateFormat,
useUTC: props.useUTC,
required: props.required,
timezone: props.timezone
};
}
const initialState = extractDate(props.value, getDateOptions());
const [state, setState] = useState(initialState);
useEffect(() => {
if (props.value !== state.date) {
const newState = extractDate(props.value, getDateOptions());
setState(newState);
}
}, [props.value]);
useEffect(() => {
checkSupportedDateFormat(props.dateFormat);
}, [props.dateFormat]);
useEffect(() => {
if (props.timezone) {
checkSupportedTimezone(props.timezone);
}
}, [props.timezone]);
function onChange(event, origin, nextState) {
if (!props.onChange) {
return;
}
const {
errorMessage,
date,
textInput,
errors
} = nextState;
props.onChange(event, {
errors,
errorMessage,
date,
textInput,
origin
});
}
function onInputChange(event) {
const textInput = event.target.value;
const nextState = extractPartsFromTextInput(textInput, getDateOptions(), props.isDisabledChecker);
setState(nextState);
onChange(event, 'INPUT', nextState);
}
function onPickerChange(event, {
date
}) {
const nextState = extractFromDate(date, getDateOptions());
setState(nextState);
onChange(event, 'PICKER', nextState);
}
return /*#__PURE__*/_jsx(DateContext.Provider, {
value: {
value: {
textInput: state.textInput,
date: state.localDate
},
inputManagement: {
onChange: onInputChange,
placeholder: props.dateFormat
},
pickerManagement: {
onSubmit: onPickerChange,
useUTC: props.useUTC
}
},
children: props.children
});
}
ContextualManager.propTypes = {
children: PropTypes.node,
dateFormat: PropTypes.string,
onChange: PropTypes.func,
required: PropTypes.bool,
timezone: PropTypes.string,
useUTC: PropTypes.bool,
value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string]),
isDisabledChecker: PropTypes.func
};
ContextualManager.defaultProps = {
dateFormat: 'YYYY-MM-DD',
useUTC: false
};
ContextualManager.displayName = 'Date.Manager';
export default ContextualManager;
//# sourceMappingURL=Manager.component.js.map