Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refocus calendar button after calendar modal close in Home page #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 73 additions & 68 deletions frontend/src/components/MiniCal/MiniCal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import DatePicker from 'react-datepicker';
import React, { useRef, forwardRef, HTMLProps } from 'react';
import DatePicker, { ReactDatePickerProps } from 'react-datepicker';
import cn from 'classnames';
import 'react-datepicker/dist/react-datepicker.css';
import './datepicker_override.css';
Expand Down Expand Up @@ -33,24 +33,26 @@ const Icon = () => (

const MiniCal = () => {
const { curDate, setCurDate } = useDate();
const inputRef = useRef<HTMLButtonElement>(null);

const updateDate = (d: Date) => {
setCurDate(d);
};
class CustomInput extends React.Component<any> {
render() {

const CustomInput = forwardRef<HTMLButtonElement>(
({ onClick, value }: HTMLProps<HTMLButtonElement>, ref) => {
return (
<button className={styles.customInput} onClick={this.props.onClick}>
<button className={styles.customInput} onClick={onClick} ref={ref}>
<span className={styles.primary}>
{isToday(curDate) ? 'Today ' : ' '}
{isTomorrow(curDate) ? 'Tomorrow ' : ' '}
</span>
<span className={styles.space} /> <Icon />
<span className={styles.space} /> {this.props.value}
<span className={styles.space} /> {value}
</button>
);
}
}
);

const Indicators = ({ date }: { date: Date }) => (
<svg
Expand Down Expand Up @@ -78,76 +80,79 @@ const MiniCal = () => {
window.scroll(x, y);
};

const renderHeader: ReactDatePickerProps['renderCustomHeader'] = ({
date,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled,
}) => (
<div>
<div className={styles.justify}>
<button
className={cn(styles.btn2, { [styles.active]: isToday(date) })}
onClick={() => {
updateDate(new Date());
pseudoScroll();
}}
>
TODAY
</button>
<Indicators date={date} />
<button
className={cn(styles.btn2, {
[styles.active]: isTomorrow(date),
})}
onClick={() => {
const tomorrow = new Date();
tomorrow.setDate(new Date().getDate() + 1);
updateDate(tomorrow);
pseudoScroll();
}}
>
TOMORROW
</button>
</div>
<div className={styles.justify}>
<button
className={styles.btn}
onClick={decreaseMonth}
disabled={prevMonthButtonDisabled}
>
{'<'}
</button>
<span className={styles.month}>
{`${date.toLocaleString('default', {
month: 'long',
})} ${date.getFullYear()}`}
</span>

<button
className={styles.btn}
onClick={increaseMonth}
disabled={nextMonthButtonDisabled}
>
{'>'}
</button>
</div>
</div>
);

return (
<div className={styles.root}>
<DatePicker
adjustDateOnChange
selected={curDate}
onChange={updateDate}
closeOnScroll={true}
closeOnScroll
dateFormat="MMM dd, yyyy"
showPopperArrow={false}
customInput={<CustomInput />}
// Set to arbitrary non empty value to preserve actual CustomInput ref
customInputRef="#"
customInput={<CustomInput ref={inputRef} />}
highlightDates={[{ 'custom--today': [new Date()] }]}
renderCustomHeader={({
date,
changeYear,
changeMonth,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled,
}) => (
<div>
<div className={styles.justify}>
<button
className={cn(styles.btn2, { [styles.active]: isToday(date) })}
onClick={() => {
updateDate(new Date());
pseudoScroll();
}}
>
TODAY
</button>
<Indicators date={date} />
<button
className={cn(styles.btn2, {
[styles.active]: isTomorrow(date),
})}
onClick={() => {
const tomorrow = new Date();
tomorrow.setDate(new Date().getDate() + 1);
updateDate(tomorrow);
pseudoScroll();
}}
>
TOMORROW
</button>
</div>
<div className={styles.justify}>
<button
className={styles.btn}
onClick={decreaseMonth}
disabled={prevMonthButtonDisabled}
>
{'<'}
</button>
<span className={styles.month}>
{`${date.toLocaleString('default', {
month: 'long',
})} ${date.getFullYear()}`}
</span>

<button
className={styles.btn}
onClick={increaseMonth}
disabled={nextMonthButtonDisabled}
>
{'>'}
</button>
</div>
</div>
)}
onCalendarClose={() => inputRef.current?.focus()}
renderCustomHeader={renderHeader}
/>
</div>
);
Expand Down
Loading