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

Added inputRef to Options for better handling of focus #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/Components/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IOptions } from "../Options"
import defaultOptions from "../Options"
import DatePickerPopup from "./DatePickerPopup"
import DatePickerProvider, { DatePickerContext } from "./DatePickerProvider"
import { useCombinedRef } from "../hooks/useCombinedRefs"

export interface IDatePickerProps {
value?: Date
Expand Down Expand Up @@ -31,6 +32,7 @@ const DatePickerMain = ({ value, options: customOptions, children }: { value?: D
const { setShow, show } = useContext(DatePickerContext)
const InputRef = useRef<HTMLInputElement>(null)
const DatePickerRef = useRef<HTMLDivElement>(null)
const combinedInputRef = useCombinedRef(options?.inputRef || null, InputRef)

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
Expand All @@ -57,7 +59,7 @@ const DatePickerMain = ({ value, options: customOptions, children }: { value?: D
<CalendarIcon />
</div>
<Input
ref={InputRef}
ref={combinedInputRef}
idProp={options?.inputIdProp}
valueProp={value}
nameProp={options?.inputNameProp}
Expand Down
3 changes: 2 additions & 1 deletion src/Options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement } from "react"
import { ReactElement, Ref } from "react"

interface ITheme {
background: string
Expand Down Expand Up @@ -37,6 +37,7 @@ export interface IOptions {
inputIdProp?: string
inputPlaceholderProp?: string
inputDateFormatProp?: Intl.DateTimeFormatOptions
inputRef?: Ref<HTMLInputElement>
}

const options: IOptions = {
Expand Down
16 changes: 16 additions & 0 deletions src/hooks/useCombinedRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MutableRefObject, RefCallback, useCallback } from "react";

type RefType<T> = MutableRefObject<T> | RefCallback<T> | null;

export function useCombinedRef<T>(...refs: RefType<T | null>[]): RefCallback<T> {
return useCallback(instance => {
refs.forEach(ref => {
if (ref instanceof Function) {
ref(instance);
}
else if (ref) {
ref.current = instance;
}
});
}, [...refs]);
}