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

[MOL-17154][AM] update handle change behaviour in textarea #625

Merged
merged 3 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
14 changes: 9 additions & 5 deletions src/form/form-textarea.tsx
qroll marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const FormTextareaComponent = (
mobileCols,
tabletCols,
desktopCols,
transformValue,
...otherProps
} = props;

Expand Down Expand Up @@ -56,11 +57,14 @@ const FormTextareaComponent = (
// =============================================================================
const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const newValue = event.target.value;
if (!(otherProps.maxLength && newValue.length > otherProps.maxLength)) {
setStateValue(newValue);
event.target.value = newValue;
if (onChange) onChange(event);
}

const transformedValue = transformValue
? transformValue(newValue ?? "")
: newValue;

setStateValue(transformedValue);
event.target.value = transformedValue;
if (onChange) onChange(event);
};

// =============================================================================
Expand Down
22 changes: 16 additions & 6 deletions src/input-textarea/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export const TextareaBase = React.forwardRef(TextareaBaseComponent);
// =============================================================================

const TextareaComponent = (
{ value, disabled, rows = 5, onChange, ...otherProps }: TextareaProps,
{
value,
disabled,
rows = 5,
onChange,
transformValue,
...otherProps
}: TextareaProps,
ref: TextareaRef
) => {
// -------------------------------------------------------------------------
Expand All @@ -51,11 +58,14 @@ const TextareaComponent = (
// -------------------------------------------------------------------------
const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const newValue = event.target.value;
if (!(otherProps.maxLength && newValue.length > otherProps.maxLength)) {
setStateValue(newValue);
event.target.value = newValue;
if (onChange) onChange(event);
}

const transformedValue = transformValue
? transformValue(newValue ?? "")
: newValue;

setStateValue(transformedValue);
event.target.value = transformedValue;
if (onChange) onChange(event);
};

// -------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/input-textarea/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface TextareaProps
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
error?: boolean | undefined;
"data-testid"?: string | undefined;
transformValue?: ((value: string) => string) | undefined;
renderCustomCounter?:
| ((maxLength: number, currentValueLength: number) => JSX.Element)
| undefined;
Expand Down
6 changes: 6 additions & 0 deletions stories/form/form-textarea/form-textarea.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const renderCustomCounter = (maxLength: number, currentValueLength: number) => {
};
```

<Heading3>Transform value</Heading3>

You can specify the `transformValue` prop to format or sanitise input.

<Canvas of={FormTextareaStories.TransformValue} />

<Heading3>Rendering in grid layouts</Heading3>

You can also specify [ColDiv's](/docs/getting-started-layout-column-divs--docs)
Expand Down
18 changes: 18 additions & 0 deletions stories/form/form-textarea/form-textarea.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ export const Default: StoryObj<Component> = {
},
};

export const TransformValue: StoryObj<Component> = {
render: () => {
return (
<StoryContainer>
<Container>
<Form.Textarea
label="This removes special characters"
maxLength={100}
transformValue={(value) =>
value.replace(/[^A-Za-z0-9 ]/g, "")
}
/>
</Container>
</StoryContainer>
);
},
};

export const WithCustomCounter: StoryObj<Component> = {
render: () => {
return (
Expand Down
5 changes: 5 additions & 0 deletions stories/form/form-textarea/props-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const DATA: ApiTableSectionProps[] = [
"(maxLength: number,currentValueLength: number) => JSX.Element",
],
},
{
name: "transformValue",
description: "Function to transform value",
propTypes: ["(value: string) => string"],
},
],
},
...SHARED_FORM_PROPS_DATA,
Expand Down
Loading