Skip to content

Commit

Permalink
fix: autofocus now works
Browse files Browse the repository at this point in the history
  • Loading branch information
Troff8 committed May 29, 2023
1 parent f0e2f2e commit 2cd1539
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/components/GoalDeleteModal/GoalDeleteModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ChangeEvent, useCallback, useState } from 'react';
import React, { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react';
import dynamic from 'next/dynamic';
import { danger0 } from '@taskany/colors';
import {
Expand Down Expand Up @@ -28,6 +28,20 @@ interface GoalDeleteModalProps {

export const GoalDeleteModal: React.FC<GoalDeleteModalProps> = ({ shortId, onConfirm, onCancel }) => {
const [deleteConfirmation, setDeleteConfirmation] = useState('');
// FIXME: try to find better way to solve this issue with autoFocus
const ref = useRef<HTMLInputElement>(null);

useEffect(() => {
const globalListener = () => {
setTimeout(() => ref.current && ref.current.focus(), 0);
};

window.addEventListener(ModalEvent.GoalDeleteModal, globalListener);

return () => {
window.removeEventListener(ModalEvent.GoalDeleteModal, globalListener);
};
});

const onConfirmationInputChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {
setDeleteConfirmation(e.currentTarget.value);
Expand Down Expand Up @@ -56,6 +70,7 @@ export const GoalDeleteModal: React.FC<GoalDeleteModalProps> = ({ shortId, onCon

<Form>
<FormInput
ref={ref}
flat="bottom"
placeholder={shortId}
autoComplete="off"
Expand Down

0 comments on commit 2cd1539

Please sign in to comment.