From 21a2e6a9988f65c331066caa525fbb1146cf3a9f Mon Sep 17 00:00:00 2001 From: Miroslav Petrik Date: Thu, 14 Mar 2024 11:35:59 +0100 Subject: [PATCH] feat(DatepickerField): control placeholder --- .../DatepickerField.stories.tsx | 15 +++--- .../datepicker-field/DatepickerField.test.tsx | 53 ++++++++++++------- .../datepicker-field/DatepickerField.tsx | 9 ++++ 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/components/datepicker-field/DatepickerField.stories.tsx b/src/components/datepicker-field/DatepickerField.stories.tsx index 5d1b88a..a9ee5a1 100644 --- a/src/components/datepicker-field/DatepickerField.stories.tsx +++ b/src/components/datepicker-field/DatepickerField.stories.tsx @@ -8,24 +8,21 @@ export default { ...meta, }; -const birthday = dateField({ +const dueDate = dateField({ schema: (s) => { - const date = new Date(); - date.setFullYear(date.getFullYear() - 15); - - return s.max(date); + return s.min(new Date()); }, }); export const Required: FormStory = { args: { - fields: { birthday }, + fields: { dueDate }, children: ({ required }) => ( ), }, diff --git a/src/components/datepicker-field/DatepickerField.test.tsx b/src/components/datepicker-field/DatepickerField.test.tsx index bb9b26e..3c3b291 100644 --- a/src/components/datepicker-field/DatepickerField.test.tsx +++ b/src/components/datepicker-field/DatepickerField.test.tsx @@ -1,7 +1,7 @@ import { dateField } from "@form-atoms/field"; import { act, render, renderHook, screen } from "@testing-library/react"; import { userEvent } from "@testing-library/user-event"; -import { formAtom, useFormSubmit } from "form-atoms"; +import { formAtom, useFieldActions, useFormSubmit } from "form-atoms"; import { describe, expect, it } from "vitest"; import { DatepickerField } from "./DatepickerField"; @@ -33,7 +33,7 @@ describe("", () => { result.current(onSubmit)(); }); - render(); + render(); expect(screen.getByRole("dialog")).toBeInvalid(); expect(screen.getByText("This field is required")).toBeInTheDocument(); @@ -42,20 +42,12 @@ describe("", () => { it("submits without error when valid", async () => { const value = new Date(); - const field = dateField(); - const form = formAtom({ - field, - }); + const form = formAtom({ field }); const { result } = renderHook(() => useFormSubmit(form)); render( - , + , ); const input = screen.getByRole("dialog"); @@ -74,16 +66,14 @@ describe("", () => { describe("with optional field", () => { it("submits with undefined", async () => { const field = dateField().optional(); - const form = formAtom({ - field, - }); + const form = formAtom({ field }); const { result } = renderHook(() => useFormSubmit(form)); - render(); + render(); - const textarea = screen.getByRole("dialog"); + const dateInput = screen.getByRole("dialog"); - expect(textarea).toBeValid(); + expect(dateInput).toBeValid(); const onSubmit = vi.fn(); await act(async () => { @@ -93,4 +83,31 @@ describe("", () => { expect(onSubmit).toHaveBeenCalledWith({ field: undefined }); }); }); + + describe("placeholder", () => { + it("renders", () => { + const field = dateField(); + + render(); + + expect(screen.getByPlaceholderText("Pick a date")).toBeInTheDocument(); + }); + + it("appears when the field is cleared", async () => { + const field = dateField({ value: new Date() }); + const { result: fieldActions } = renderHook(() => useFieldActions(field)); + + render(); + + expect( + screen.queryByPlaceholderText("Pick a date"), + ).not.toBeInTheDocument(); + + await act(async () => { + fieldActions.current.setValue(undefined); + }); + + expect(screen.queryByPlaceholderText("Pick a date")).toBeInTheDocument(); + }); + }); }); diff --git a/src/components/datepicker-field/DatepickerField.tsx b/src/components/datepicker-field/DatepickerField.tsx index f91a3d6..6804eb9 100644 --- a/src/components/datepicker-field/DatepickerField.tsx +++ b/src/components/datepicker-field/DatepickerField.tsx @@ -12,6 +12,7 @@ export const DatepickerField = ({ helperText, required, initialValue, + placeholder = "Please select a date", ...uiProps }: DatepickerFIeldProps) => { const { @@ -25,6 +26,13 @@ export const DatepickerField = ({ initialValue, }); + const emptyProps = !value + ? { + value: "", + placeholder, + } + : {}; + return ( { onChange({