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

DateInput: Open on focus #215

Merged
merged 2 commits into from
May 27, 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
6 changes: 6 additions & 0 deletions src/form/DateInput/DateInput.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ export const Range = () => {
</Stack>
);
};

export const OpenOnFocus = () => {
const [date, setDate] = useState<Date>(new Date());

return <DateInput fullWidth openOnFocus value={date} onChange={setDate} />;
};
19 changes: 19 additions & 0 deletions src/form/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {
ChangeEvent,
FocusEvent,
FC,
ReactElement,
useCallback,
Expand Down Expand Up @@ -30,6 +31,11 @@ export type DateInputProps = Omit<InputProps, 'value' | 'onChange'> & {
*/
placement?: Placement;

/**
* Open calendar on field focus
*/
openOnFocus?: boolean;

/**
* Icon to show in open calendar button.
*/
Expand All @@ -53,7 +59,9 @@ export const DateInput: FC<DateInputProps> = ({
placement = 'bottom-start',
isRange,
icon = <CalendarIcon />,
openOnFocus = true,
onChange,
onFocus,
...rest
}) => {
const [open, setOpen] = useState<boolean>(false);
Expand Down Expand Up @@ -108,6 +116,16 @@ export const DateInput: FC<DateInputProps> = ({
[format, isRange, onChange]
);

const focusHandler = useCallback(
(e: FocusEvent<HTMLInputElement>) => {
if (openOnFocus) {
setOpen(true);
}
onFocus?.(e);
},
[onFocus, openOnFocus]
);

useEffect(() => {
if (value) {
if (isRange) {
Expand Down Expand Up @@ -143,6 +161,7 @@ export const DateInput: FC<DateInputProps> = ({
{...rest}
value={inputValue}
onChange={inputChangeHandler}
onFocus={focusHandler}
/>
<Menu
open={open}
Expand Down
Loading