Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing paste when readOnly is true on input component #2774

Merged
merged 4 commits into from
Dec 5, 2024
Merged
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
10 changes: 9 additions & 1 deletion src/app-components/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ export type InputProps = {
>;

export function Input(props: InputProps) {
const { size = 'sm', ...rest } = props;
const { size = 'sm', readOnly, ...rest } = props;

const handlePaste = (event: React.ClipboardEvent<HTMLInputElement>) => {
if (readOnly) {
event.preventDefault();
}
};

if (props.textonly) {
const { value, id, className } = props;
Expand All @@ -56,7 +62,9 @@ export function Input(props: InputProps) {

return (
<Textfield
onPaste={handlePaste}
size={size}
readOnly={readOnly}
{...rest}
/>
);
Expand Down
22 changes: 21 additions & 1 deletion src/layout/Input/InputComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,33 @@ describe('InputComponent', () => {
expect(inputComponent).toHaveValue(formattedValue);
});

it('should prevent pasting when readOnly is true', async () => {
const initialValue = 'initial value';
await render({
component: {
readOnly: true,
},
queries: {
fetchFormData: () => Promise.resolve({ some: { field: initialValue } }),
},
});

const inputComponent = screen.getByRole('textbox') as HTMLInputElement;
expect(inputComponent).toHaveValue(initialValue);

await userEvent.click(inputComponent);

await userEvent.paste('pasted text');

expect(inputComponent).toHaveValue(initialValue);
});

const render = async ({ component, ...rest }: Partial<RenderGenericComponentTestProps<'Input'>> = {}) =>
await renderGenericComponentTest({
type: 'Input',
renderer: (props) => <InputComponent {...props} />,
component: {
id: 'mock-id',
readOnly: false,
required: false,
dataModelBindings: {
simpleBinding: { dataType: defaultDataTypeMock, field: 'some.field' },
Expand Down
4 changes: 3 additions & 1 deletion src/layout/Input/InputComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export const InputVariant = ({ node, overrideDisplay }: Pick<IInputProps, 'node'

const reactNumberFormatConfig = useMapToReactNumberConfig(formatting, formValue);
const variant = getVariantWithFormat(inputVariant, reactNumberFormatConfig?.number);

switch (variant.type) {
case 'search':
case 'text':
Expand Down Expand Up @@ -148,6 +147,9 @@ export const InputVariant = ({ node, overrideDisplay }: Pick<IInputProps, 'node'
* https://github.com/s-yadav/react-number-format/issues/349
* */
event.preventDefault();
if (inputProps.readOnly) {
return;
}
const pastedText = event.clipboardData.getData('Text');
if (pastedText.indexOf(',') !== -1) {
setValue('simpleBinding', pastedText.replace(',', '.'));
Expand Down
Loading