From f3f89f062abc74fa9beec67c499ed710d4c92a0d Mon Sep 17 00:00:00 2001 From: Maksim Eltyshev Date: Thu, 6 Jun 2024 19:59:39 +0200 Subject: [PATCH] ref: Move to separate function --- client/src/components/Card/NameEdit.jsx | 5 ++--- client/src/components/CardModal/Activities/CommentEdit.jsx | 5 ++--- client/src/components/CardModal/Tasks/NameEdit.jsx | 5 ++--- client/src/components/List/NameEdit.jsx | 5 ++--- client/src/utils/element-helpers.js | 5 +++++ 5 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 client/src/utils/element-helpers.js diff --git a/client/src/components/Card/NameEdit.jsx b/client/src/components/Card/NameEdit.jsx index 48b7dec..1487a95 100644 --- a/client/src/components/Card/NameEdit.jsx +++ b/client/src/components/Card/NameEdit.jsx @@ -5,6 +5,7 @@ import TextareaAutosize from 'react-textarea-autosize'; import { Button, Form, TextArea } from 'semantic-ui-react'; import { useClosableForm, useField } from '../../hooks'; +import { focusEnd } from '../../utils/element-helpers'; import styles from './NameEdit.module.scss'; @@ -79,9 +80,7 @@ const NameEdit = React.forwardRef(({ children, defaultValue, onUpdate }, ref) => useEffect(() => { if (isOpened) { - const text = field.current.ref.current; - text.focus(); - text.setSelectionRange(text.value.length + 1, text.value.length + 1); + focusEnd(field.current.ref.current); } }, [isOpened]); diff --git a/client/src/components/CardModal/Activities/CommentEdit.jsx b/client/src/components/CardModal/Activities/CommentEdit.jsx index c75c8a7..cd088df 100755 --- a/client/src/components/CardModal/Activities/CommentEdit.jsx +++ b/client/src/components/CardModal/Activities/CommentEdit.jsx @@ -6,6 +6,7 @@ import TextareaAutosize from 'react-textarea-autosize'; import { Button, Form, TextArea } from 'semantic-ui-react'; import { useForm } from '../../../hooks'; +import { focusEnd } from '../../../utils/element-helpers'; import styles from './CommentEdit.module.scss'; @@ -70,9 +71,7 @@ const CommentEdit = React.forwardRef(({ children, defaultData, onUpdate }, ref) useEffect(() => { if (isOpened) { - const text = textField.current.ref.current; - text.focus(); - text.setSelectionRange(text.value.length + 1, text.value.length + 1); + focusEnd(textField.current.ref.current); } }, [isOpened]); diff --git a/client/src/components/CardModal/Tasks/NameEdit.jsx b/client/src/components/CardModal/Tasks/NameEdit.jsx index 2f26b27..88044d2 100755 --- a/client/src/components/CardModal/Tasks/NameEdit.jsx +++ b/client/src/components/CardModal/Tasks/NameEdit.jsx @@ -5,6 +5,7 @@ import TextareaAutosize from 'react-textarea-autosize'; import { Button, Form, TextArea } from 'semantic-ui-react'; import { useField } from '../../../hooks'; +import { focusEnd } from '../../../utils/element-helpers'; import styles from './NameEdit.module.scss'; @@ -65,9 +66,7 @@ const NameEdit = React.forwardRef(({ children, defaultValue, onUpdate }, ref) => useEffect(() => { if (isOpened) { - const text = field.current.ref.current; - text.focus(); - text.setSelectionRange(text.value.length + 1, text.value.length + 1); + focusEnd(field.current.ref.current); } }, [isOpened]); diff --git a/client/src/components/List/NameEdit.jsx b/client/src/components/List/NameEdit.jsx index cbd65c4..ffda745 100755 --- a/client/src/components/List/NameEdit.jsx +++ b/client/src/components/List/NameEdit.jsx @@ -4,6 +4,7 @@ import TextareaAutosize from 'react-textarea-autosize'; import { TextArea } from 'semantic-ui-react'; import { useField } from '../../hooks'; +import { focusEnd } from '../../utils/element-helpers'; import styles from './NameEdit.module.scss'; @@ -71,9 +72,7 @@ const NameEdit = React.forwardRef(({ children, defaultValue, onUpdate }, ref) => useEffect(() => { if (isOpened) { - const text = field.current.ref.current; - text.focus(); - text.setSelectionRange(text.value.length + 1, text.value.length + 1); + focusEnd(field.current.ref.current); } }, [isOpened]); diff --git a/client/src/utils/element-helpers.js b/client/src/utils/element-helpers.js new file mode 100644 index 0000000..15fe9c4 --- /dev/null +++ b/client/src/utils/element-helpers.js @@ -0,0 +1,5 @@ +// eslint-disable-next-line import/prefer-default-export +export const focusEnd = (element) => { + element.focus(); + element.setSelectionRange(element.value.length + 1, element.value.length + 1); +};