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

fix:Calendar: timeOnly Today button should be Now #5797

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions components/lib/api/Locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ let locales = {
am: 'am',
pm: 'pm',
today: 'Today',
now: 'Now',
weekHeader: 'Wk',
firstDayOfWeek: 0,
showMonthAfterYear: false,
Expand Down
6 changes: 5 additions & 1 deletion components/lib/api/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,13 @@ export interface LocaleOptions {
*/
monthNamesShort?: string[];
/**
* Today
* Today (Calendar date only)
*/
today?: string;
/**
* Now (Calendar using time)
*/
now?: string;
/**
* Wk
*/
Expand Down
33 changes: 26 additions & 7 deletions components/lib/calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const Calendar = React.memo(
const nextButton = React.useRef(false);
const viewChangedWithKeyDown = React.useRef(false);
const onChangeRef = React.useRef(null);
const isClearClicked = React.useRef(false);

const [currentView, setCurrentView] = React.useState('date');
const [currentMonth, setCurrentMonth] = React.useState(null);
Expand Down Expand Up @@ -495,6 +496,8 @@ export const Calendar = React.memo(
};

const onClearButtonClick = (event) => {
isClearClicked.current = true;

updateModel(event, null);
updateInputfield(null);
hide();
Expand Down Expand Up @@ -2994,14 +2997,20 @@ export const Calendar = React.memo(
prevPropValue = prevPropValue[0];
}

if ((!prevPropValue && propValue) || (propValue && propValue instanceof Date && propValue.getTime() !== prevPropValue.getTime())) {
let viewDate = props.viewDate && isValidDate(props.viewDate) ? props.viewDate : propValue && isValidDate(propValue) ? propValue : new Date();
let viewDate = props.viewDate && isValidDate(props.viewDate) ? props.viewDate : propValue && isValidDate(propValue) ? propValue : new Date();

validateDate(viewDate);
if (isClearClicked.current && props.showTime) {
viewDate.setHours(0, 0, 0);

setViewDateState(viewDate);
viewStateChanged.current = true;
isClearClicked.current = false;
}

if ((!prevPropValue && propValue) || (propValue && propValue instanceof Date && propValue.getTime() !== prevPropValue.getTime())) {
validateDate(viewDate);
}

setViewDateState(viewDate);
viewStateChanged.current = true;
};

const createBackwardNavigator = (isVisible) => {
Expand Down Expand Up @@ -4000,7 +4009,9 @@ export const Calendar = React.memo(

const createButtonBar = () => {
if (props.showButtonBar) {
const { today, clear } = localeOptions(props.locale);
const { today, clear, now } = localeOptions(props.locale);
const nowDate = new Date();
const isHidden = (props.minDate && props.minDate > nowDate) || (props.maxDate && props.maxDate < nowDate);
const buttonbarProps = mergeProps(
{
className: cx('buttonbar')
Expand All @@ -4010,7 +4021,15 @@ export const Calendar = React.memo(

return (
<div {...buttonbarProps}>
<Button type="button" label={today} onClick={onTodayButtonClick} onKeyDown={(e) => onContainerButtonKeydown(e)} className={classNames(props.todayButtonClassName, cx('todayButton'))} pt={ptm('todayButton')} />
<Button
type="button"
label={props.showTime ? now : today}
onClick={onTodayButtonClick}
onKeyDown={(e) => onContainerButtonKeydown(e)}
className={classNames(props.todayButtonClassName, cx('todayButton'))}
pt={ptm('todayButton')}
style={isHidden ? { visibility: 'hidden' } : undefined}
/>
<Button type="button" label={clear} onClick={onClearButtonClick} onKeyDown={(e) => onContainerButtonKeydown(e)} className={classNames(props.clearButtonClassName, cx('clearButton'))} pt={ptm('clearButton')} />
</div>
);
Expand Down
Loading