Skip to content
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
19 changes: 13 additions & 6 deletions lib/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export const DateTimePicker: FC<DateTimePickerProps> = (defaultProps) => {
const mergedOptions = useMemo(() => mergeHooks(options, props), [options, props]);
const nodeRef = useRef<HTMLElement | null>(null);
const flatpickrRef = useRef<flatpickr.Instance>(undefined);
const mergedOptionsRef = useRef(mergedOptions);
mergedOptionsRef.current = mergedOptions;
const onCreateRef = useRef(onCreate);
onCreateRef.current = onCreate;
const onDestroyRef = useRef(onDestroy);
onDestroyRef.current = onDestroy;

useImperativeHandle(
defaultProps.ref,
Expand All @@ -67,20 +73,21 @@ export const DateTimePicker: FC<DateTimePickerProps> = (defaultProps) => {

useEffect(() => {
const createFlatpickrInstance = () => {
mergedOptions.onClose =
mergedOptions.onClose ||
const opts = mergedOptionsRef.current;
opts.onClose =
opts.onClose ||
(() => {
if (nodeRef.current?.blur) nodeRef.current.blur();
});

// @ts-expect-error for some reason the default import isnt working correctly
flatpickrRef.current = (flatpickr?.default || flatpickr)(nodeRef.current as HTMLElement, mergedOptions);
flatpickrRef.current = (flatpickr?.default || flatpickr)(nodeRef.current as HTMLElement, opts);

onCreate?.(flatpickrRef.current);
onCreateRef.current?.(flatpickrRef.current);
};

const destroyFlatpickrInstance = () => {
onDestroy?.(flatpickrRef.current);
onDestroyRef.current?.(flatpickrRef.current);
if (flatpickrRef.current) {
flatpickrRef.current.destroy();
}
Expand All @@ -92,7 +99,7 @@ export const DateTimePicker: FC<DateTimePickerProps> = (defaultProps) => {
return () => {
destroyFlatpickrInstance();
};
}, [mergedOptions, onCreate, onDestroy]);
}, []);
Comment on lines 99 to +102

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iahmedgamal i am concerned about this, there was a reason mergedOptions was a dependency


useEffect(() => {
if (flatpickrRef.current) {
Expand Down
12 changes: 12 additions & 0 deletions test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,16 @@ describe('react-flatpickr', () => {
expect(spy).toHaveBeenCalled();
});
});

describe('when options are passed inline without memoization', () => {
it('does not recreate the flatpickr instance when re-rendered with inline options', () => {
const spy = jest.fn();
const {unmount, rerender} = render(<DateTimePicker options={{enableTime: true}} onCreate={spy} />);

rerender(<DateTimePicker options={{enableTime: true}} onCreate={spy} />);

expect(spy).toHaveBeenCalledTimes(1);
unmount();
});
});
});