Skip to content

Commit

Permalink
added event listener to prevent new-line insertion in quote fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
abelpz committed Jan 27, 2025
1 parent f6e1a43 commit 074a807
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion public/build_number
Original file line number Diff line number Diff line change
@@ -1 +1 @@
280-f9e231e
281-f6e1a43
21 changes: 21 additions & 0 deletions src/components/translatable/TranslatableTSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { getReferenceFilterOptions } from './referenceFilterOptions';
import { useContentUpdateProps } from '../../hooks/useContentUpdateProps';
import { UpdateBranchButton } from '../branch-merger/components/UpdateBranchButton';
import ErrorDialog from '../dialogs/ErrorDialog';
import { useEventListeners } from '../../hooks/useEventListeners';

const delimiters = { row: '\n', cell: '\t' };

Expand All @@ -51,6 +52,7 @@ export default function TranslatableTSV({
// manage the state of the resources for the provider context
const [resources, setResources] = useState([]);
const [isSaving, setIsSaving] = useState(false);
const { addEventListener, clearEventListeners } = useEventListeners();

const {
state: {
Expand Down Expand Up @@ -116,6 +118,25 @@ export default function TranslatableTSV({
page: 0,
rowsPerPage: 25,
rowsPerPageOptions: [10, 25, 50, 100],
onCellClick: (colData, cellMeta) => {
clearEventListeners();

const { name: columnName } = colData.props.tableMeta.columnData;
if (columnName !== "Quote") return;

const quoteElement = cellMeta.event.target;
if (!quoteElement?.addEventListener) return;

// Prevent Enter key from creating new lines in Quote cells
const handleQuoteKeyPress = (event) => {
if (event.key === 'Enter') {
event.preventDefault();
event.stopPropagation();
}
};

addEventListener(quoteElement, 'keypress', handleQuoteKeyPress);
}
};

const rowHeader = useDeepCompareCallback((rowData, actionsMenu) => {
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useEventListeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useCallback, useRef, useEffect } from 'react';

export function useEventListeners() {
const abortController = useRef(new AbortController());

const clearEventListeners = useCallback(() => {
abortController.current.abort();
abortController.current = new AbortController();
}, []);

const addEventListener = useCallback((target, eventType, listener) => {
target.addEventListener(eventType, listener, {
signal: abortController.current.signal
});
}, []);

// Cleanup event listeners on unmount
useEffect(() => clearEventListeners, [clearEventListeners]);

return { addEventListener, clearEventListeners };
}

0 comments on commit 074a807

Please sign in to comment.