Skip to content

feat(CalendarView): cellFocusable prop #187

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

Closed
wants to merge 2 commits into from
Closed
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 src/components/Calendar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ LANDING_BLOCK-->
| aria-label | The control's `aria-label` attribute | `string` | |
| aria-labelledby | The control's `aria-labelledby` attribute | `string` | |
| autoFocus | The control's `autofocus` attribute | `boolean` | |
| cellFocusable | The control's `cellFocusable` attribute | `boolean` | |
| className | The control's wrapper class name | `string` | |
| [defaultFocusedValue](#focused-value) | The date that is focused when the calendar first mounts (uncontrolled) | `DateTime` | |
| [defaultMode](#mode) | Initial mode to show in calendar | `days` `months` `quarters` `years` | |
Expand Down
40 changes: 39 additions & 1 deletion src/components/Calendar/__stories__/Calendar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import {dateTime, dateTimeParse} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
import {Tab, TabList, TabPanel, TabProvider} from '@gravity-ui/uikit';
import {Popup, Tab, TabList, TabPanel, TabProvider, Text, TextInput} from '@gravity-ui/uikit';
import {toaster} from '@gravity-ui/uikit/toaster-singleton';
import {action} from '@storybook/addon-actions';
import type {Meta, StoryObj} from '@storybook/react';
Expand Down Expand Up @@ -169,3 +169,41 @@ export const Custom: Story = {
controls: {exclude: ['mode', 'defaultMode', 'modes']},
},
};

export const InputFocus: Story = {
...Default,
render: function InputFocus(args) {
const [inputFocused, setInputFocused] = React.useState(false);

// Code from Tokenized Input
// Prevent losing focus on the input when clicking on the popup
const onMouseDown = React.useCallback((e: React.MouseEvent<HTMLDivElement>) => {
e.preventDefault();
}, []);

const inputRef = React.useRef<HTMLInputElement>(null);

return (
<div>
<Text as="h3">
When clicking in the calendar, the focus on the input should not be lost.
</Text>
<TextInput
style={{width: 200}}
ref={inputRef}
type="text"
onFocus={() => {
setInputFocused(true);
}}
onBlur={() => {
setInputFocused(false);
}}
/>
<Popup anchorElement={inputRef?.current} open={inputFocused} returnFocus={false}>
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions*/}
<div onMouseDown={onMouseDown}>{Default.render?.({...args})}</div>
</Popup>
</div>
);
},
};
4 changes: 4 additions & 0 deletions src/components/CalendarView/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export interface CalendarStateOptionsBase extends InputBase {
autoFocus?: boolean;
/** Controls the currently focused date within the calendar. */
focusedValue?: DateTime | null;
/** Whether to automatically focus the cell
* * @default true
*/
cellFocusable?: boolean;
/** The date that is focused when the calendar first mounts (uncontrolled). */
defaultFocusedValue?: DateTime;
/** Handler that is called when the focused date changes. */
Expand Down
9 changes: 8 additions & 1 deletion src/components/CalendarView/hooks/useCalendarState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export function useCalendarState(props: CalendarStateOptions): CalendarState {
);
const timeZone = props.timeZone || inputTimeZone;

const cellFocusable = props.cellFocusable ?? true;

const minValue = React.useMemo(
() => (props.minValue ? props.minValue.timeZone(timeZone) : undefined),
[timeZone, props.minValue],
Expand Down Expand Up @@ -225,7 +227,12 @@ export function useCalendarState(props: CalendarStateOptions): CalendarState {
}
},
isCellFocused(date: DateTime) {
return this.isFocused && focusedDate && date.isSame(focusedDate, currentMode);
return (
cellFocusable &&
this.isFocused &&
focusedDate &&
date.isSame(focusedDate, currentMode)
);
},
isCellDisabled(date: DateTime) {
return this.disabled || this.isInvalid(date);
Expand Down
Loading