[MM-57727] Convert SearchDateSuggestion to functional component (#26708)

* Convert SearchDateSuggestion to functional component

* Review fixes

* Review fix

Co-authored-by: Daniel Espino García <larkox@gmail.com>

* Another review fix

---------

Co-authored-by: Daniel Espino García <larkox@gmail.com>
This commit is contained in:
MeHow25 2024-04-12 13:09:12 +02:00 committed by GitHub
parent 98712737e6
commit 1dd7046eff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import type {Locale} from 'date-fns';
import React from 'react';
import React, {useCallback, useEffect, useState} from 'react';
import {DayPicker} from 'react-day-picker';
import Constants from 'utils/constants';
@ -20,66 +20,63 @@ type Props = SuggestionProps<never> & {
preventClose?: () => void;
}
export default class SearchDateSuggestion extends React.PureComponent<Props> {
private loadedLocales: Record<string, Locale> = {};
state = {
datePickerFocused: false,
};
handleDayClick = (day: Date) => {
const dayString = new Date(Date.UTC(day.getFullYear(), day.getMonth(), day.getDate())).toISOString().split('T')[0];
this.props.onClick(dayString, this.props.matchedPretext);
};
handleKeyDown = (e: KeyboardEvent) => {
if (Keyboard.isKeyPressed(e, Constants.KeyCodes.DOWN) && document.activeElement?.id === 'searchBox') {
this.setState({datePickerFocused: true});
} else if (Keyboard.isKeyPressed(e, Constants.KeyCodes.ESCAPE)) {
this.props.handleEscape?.();
}
};
componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown);
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown);
}
iconLeft = () => {
const IconLeft = () => {
return (
<i className='icon icon-chevron-left'/>
);
};
iconRight = () => {
const IconRight = () => {
return (
<i className='icon icon-chevron-right'/>
);
};
render() {
const locale: string = this.props.locale;
const DAY_PICKER_ICONS = {IconRight, IconLeft};
this.loadedLocales = Utils.getDatePickerLocalesForDateFns(locale, this.loadedLocales);
const SearchDateSuggestion = ({
currentDate,
handleEscape,
locale,
preventClose,
matchedPretext,
onClick,
}: Props) => {
let loadedLocales: Record<string, Locale> = {};
loadedLocales = Utils.getDatePickerLocalesForDateFns(locale, loadedLocales);
const [datePickerFocused, setDatePickerFocused] = useState(false);
const handleDayClick = useCallback((day: Date) => {
const dayString = new Date(Date.UTC(day.getFullYear(), day.getMonth(), day.getDate())).toISOString().split('T')[0];
onClick(dayString, matchedPretext);
}, [onClick, matchedPretext]);
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (Keyboard.isKeyPressed(e, Constants.KeyCodes.DOWN) && document.activeElement?.id === 'searchBox') {
setDatePickerFocused(true);
} else if (Keyboard.isKeyPressed(e, Constants.KeyCodes.ESCAPE)) {
handleEscape?.();
}
};
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [handleEscape]);
return (
<DayPicker
onDayClick={this.handleDayClick}
onDayClick={handleDayClick}
showOutsideDays={true}
mode={'single'}
locale={this.loadedLocales[locale]}
initialFocus={this.state.datePickerFocused}
onMonthChange={this.props.preventClose}
locale={loadedLocales[locale]}
initialFocus={datePickerFocused}
onMonthChange={preventClose}
id='searchDatePicker'
selected={this.props.currentDate}
components={{
IconRight: this.iconRight,
IconLeft: this.iconLeft,
}}
selected={currentDate}
components={DAY_PICKER_ICONS}
/>
);
}
}
};
export default React.memo(SearchDateSuggestion);