Files
twenty/packages/twenty-front/src/modules/ui/field/input/components/TextAreaInput.tsx
T
Raphaël Bosi c6e5bab4e9 Replace hotkey scopes by focus stack (Part 4 - Inputs) (#12933)
# Replace hotkey scopes by focus stack (Part 4 - Inputs)

This PR is the 4th part of a refactoring aiming to deprecate the hotkey
scopes api in favor of the new focus stack api which is more robust.
Part 1: https://github.com/twentyhq/twenty/pull/12673
Part 2: https://github.com/twentyhq/twenty/pull/12798
Part 3: https://github.com/twentyhq/twenty/pull/12910

In this part, I refactored all inputs in the app so that each input has
a unique id which can be used to track the focused element.
2025-07-07 15:42:12 +02:00

120 lines
3.0 KiB
TypeScript

import styled from '@emotion/styled';
import { ChangeEvent, useEffect, useRef, useState } from 'react';
import TextareaAutosize from 'react-textarea-autosize';
import { LightCopyIconButton } from '@/object-record/record-field/components/LightCopyIconButton';
import { useRegisterInputEvents } from '@/object-record/record-field/meta-types/input/hooks/useRegisterInputEvents';
import { isDefined } from 'twenty-shared/utils';
import { TEXT_INPUT_STYLE } from 'twenty-ui/theme';
import { turnIntoEmptyStringIfWhitespacesOnly } from '~/utils/string/turnIntoEmptyStringIfWhitespacesOnly';
export type TextAreaInputProps = {
instanceId: string;
disabled?: boolean;
className?: string;
placeholder?: string;
autoFocus?: boolean;
value: string;
onEnter: (newText: string) => void;
onEscape: (newText: string) => void;
onTab?: (newText: string) => void;
onShiftTab?: (newText: string) => void;
onClickOutside: (event: MouseEvent | TouchEvent, inputValue: string) => void;
hotkeyScope: string;
onChange?: (newText: string) => void;
maxRows?: number;
copyButton?: boolean;
};
const StyledTextArea = styled(TextareaAutosize)`
${TEXT_INPUT_STYLE}
align-items: center;
display: flex;
justify-content: center;
resize: none;
max-height: 400px;
width: calc(100% - ${({ theme }) => theme.spacing(7)});
line-height: 18px;
`;
const StyledLightIconButtonContainer = styled.div`
background: transparent;
position: absolute;
top: 16px;
transform: translateY(-50%);
right: 0;
`;
export const TextAreaInput = ({
instanceId,
disabled,
className,
placeholder,
autoFocus,
value,
hotkeyScope,
onEnter,
onEscape,
onTab,
onShiftTab,
onClickOutside,
onChange,
maxRows,
copyButton = true,
}: TextAreaInputProps) => {
const [internalText, setInternalText] = useState(value);
const handleChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
const targetValue = turnIntoEmptyStringIfWhitespacesOnly(
event.target.value,
);
setInternalText(targetValue);
onChange?.(targetValue);
};
const wrapperRef = useRef<HTMLTextAreaElement>(null);
const copyRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (isDefined(wrapperRef.current)) {
wrapperRef.current.setSelectionRange(
wrapperRef.current.value.length,
wrapperRef.current.value.length,
);
}
}, []);
useRegisterInputEvents({
focusId: instanceId,
inputRef: wrapperRef,
copyRef: copyRef,
inputValue: internalText,
onEnter,
onEscape,
onClickOutside,
onTab,
onShiftTab,
hotkeyScope,
});
return (
<>
<StyledTextArea
placeholder={placeholder}
disabled={disabled}
className={className}
ref={wrapperRef}
onChange={handleChange}
autoFocus={autoFocus}
value={internalText}
maxRows={maxRows}
/>
{copyButton && (
<StyledLightIconButtonContainer ref={copyRef}>
<LightCopyIconButton copyText={internalText} />
</StyledLightIconButtonContainer>
)}
</>
);
};