Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function Input(props) {
const { initialValue, onChange } = props;
const [inputtedValue, setInputtedValue] = React.useState(initialValue);
const debouncedValue = useDebounce(inputtedValue, 300);
const prevValue = usePrevious(debouncedValue);
React.useEffect(() => {
if (prevValue === undefined || prevValue === debouncedValue) return;
onChange(debouncedValue);
// Just want to watch the value change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedValue]);
React.useEffect(() => {
setInputtedValue(initialValue);
}, [initialValue]);
function handleChange(e) {
const newInputtedValue = e.target.value;
setInputtedValue(newInputtedValue);
const [inputtedString, setInputtedString] = React.useState(formatDateProp(dataFormat));
const [possibleDate, setPossibleDate] = React.useState(null);
const [shouldShowCalendar, setShouldShowCalendar] = React.useState(false);
const prevDate = usePrevious(date);
// Ref
const calendarRef = React.useRef(null);
const inputRef = React.useRef(null);
// Effect
React.useEffect(() => {
if ((!date && !prevDate) || (date && prevDate && date.isSame(prevDate, "day"))) return;
setInputtedString(formatDateProp(dataFormat));
}, [dataFormat, date, prevDate, formatDateProp, humanFormat]);
const debouncedPossibleDate = useDebounce(possibleDate, 300);
const extendedInputProps = extractChildrenProps(children, DateInput);
const extendedPopoverProps = extractChildrenProps(children, DatePickerPopover);
function hideCalendar() {
if (shouldShowCalendar) setShouldShowCalendar(false);
if (inputRef.current) inputRef.current.blur();
}
function showCalendar() {
if (!shouldShowCalendar) setShouldShowCalendar(true);
}
function parseInput() {
let newDate = moment(inputtedString, dataFormat);
if (!newDate.isValid()) newDate = moment(inputtedString);