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

feat(textarea): add maxlength via strapi defaulting to 5000 #1534

Merged
merged 12 commits into from
Dec 10, 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
4 changes: 3 additions & 1 deletion app/components/inputs/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TextareaProps = Readonly<{
content: string;
};
placeholder?: string;
maxLength?: number;
errorMessages?: ErrorMessageProps[];
formId?: string;
classNameLabel?: string;
Expand All @@ -33,6 +34,7 @@ const Textarea = ({
label,
details,
placeholder,
maxLength = TEXTAREA_CHAR_LIMIT,
errorMessages,
classNameLabel,
role,
Expand Down Expand Up @@ -60,8 +62,8 @@ const Textarea = ({
{...getInputProps({
id: name,
placeholder,
maxLength: TEXTAREA_CHAR_LIMIT,
})}
maxLength={maxLength}
rows={TEXT_AREA_ROWS}
className={classNames(
"ds-textarea forced-color-adjust-none placeholder-gray-600",
Expand Down
32 changes: 32 additions & 0 deletions app/components/inputs/__test__/Textarea.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRemixStub } from "@remix-run/testing";
import { render, screen, fireEvent } from "@testing-library/react";
import Textarea, { TEXT_AREA_ROWS } from "~/components/inputs/Textarea";
import { TEXTAREA_CHAR_LIMIT } from "~/services/validation/inputlimits";

const getInputProps = vi.fn();
let error: string | undefined = undefined;
Expand All @@ -27,10 +28,12 @@ afterEach(() => {
describe("Textarea component", () => {
it("renders without errors", () => {
const componentName = "test-textarea";

getInputProps.mockImplementationOnce(() => ({
id: componentName,
placeholder: "Test Placeholder",
}));

const RemixStub = createRemixStub([
{
path: "",
Expand All @@ -51,6 +54,9 @@ describe("Textarea component", () => {
expect(elementByLabel).not.toHaveClass("ds-heading-03-reg");

expect(screen.getByPlaceholderText("Test Placeholder")).toBeInTheDocument();
expect(element.getAttribute("maxLength")).toBe(
TEXTAREA_CHAR_LIMIT.toString(),
);
});

it("renders without errors when description is provided", () => {
Expand Down Expand Up @@ -154,4 +160,30 @@ describe("Textarea component", () => {

expect(textarea.getAttribute("rows")).toEqual(TEXT_AREA_ROWS.toString());
});

it("should set the maxLength", () => {
const maxLength = 10;
getInputProps.mockImplementationOnce(() => ({
id: "componentName",
}));

const RemixStub = createRemixStub([
{
path: "",
Component: () => (
<Textarea
name="componentName"
label="Test Label"
formId="formId"
maxLength={maxLength}
/>
),
},
]);

render(<RemixStub />);
const textarea = screen.getByRole("textbox");

expect(textarea.getAttribute("maxLength")).toBe(maxLength.toString());
});
});
12 changes: 9 additions & 3 deletions app/services/cms/components/StrapiTextarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const StrapiTextareaSchema = z
label: z.string().nullable(),
placeholder: z.string().nullable(),
errors: StrapiErrorRelationSchema,
maxLength: z.number().nullable(),
})
.merge(HasOptionalStrapiIdSchema);

Expand All @@ -25,6 +26,11 @@ export const StrapiTextareaComponentSchema = StrapiTextareaSchema.extend({
__component: z.literal("form-elements.textarea"),
});

export const StrapiTextarea = ({ errors, ...props }: StrapiTextarea) => (
<Textarea {...omitNull(props)} errorMessages={flattenStrapiErrors(errors)} />
);
export const StrapiTextarea = ({ errors, ...props }: StrapiTextarea) => {
return (
<Textarea
{...omitNull(props)}
errorMessages={flattenStrapiErrors(errors)}
/>
);
};
Loading