diff --git a/src/app/components/cms/Feedback/Feedback.tsx b/src/app/components/cms/Feedback/Feedback.tsx index 95746ca47..54be63377 100644 --- a/src/app/components/cms/Feedback/Feedback.tsx +++ b/src/app/components/cms/Feedback/Feedback.tsx @@ -10,6 +10,7 @@ import { FormField } from '@/api/models/cms/Page/FormFields' import { handler } from '../utils/handler' import CheckboxField from './Fields/Checkbox/CheckboxField' import CheckboxesField from './Fields/Checkboxes/CheckboxesField' +import DateField from './Fields/Date/DateField' import DropdownField from './Fields/Dropdown/DropdownField' import EmailField from './Fields/Email/EmailField' import MultilineField from './Fields/Multiline/MultilineField' @@ -181,6 +182,10 @@ export const renderFormFields = ( )} + {fieldType === 'date' && ( + + )} + {fieldType === 'dropdown' && ( ) { + const [hiddenDateInput, setHiddenDateInput] = useState('') + const [dateData, setDateData] = useState({ + day: '', + month: '', + year: '', + }) + + const handleChange = (e: React.ChangeEvent) => { + const { name, value } = e.target + + setDateData((prev) => ({ + ...prev, + [name]: value, + })) + } + + useEffect(() => { + if (dateData.day && dateData.month && dateData.year) { + setHiddenDateInput(`${dateData.day}-${dateData.month}-${dateData.year}`) + } else { + setHiddenDateInput('') + } + }, [dateData]) + + return ( +
+ {/* Hidden input field for collecting date in format dd-mm-yyyy */} + +
+ +

+ +

+ {helpText.length > 0 ?
{helpText}
: null} +
+ + {fieldHasError ? ( +

+ Error: Please enter a valid date +

+ ) : null} + +
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+
+ ) +} diff --git a/src/app/components/cms/Feedback/Fields/Date/Datefield.spec.tsx b/src/app/components/cms/Feedback/Fields/Date/Datefield.spec.tsx new file mode 100644 index 000000000..207ec9c36 --- /dev/null +++ b/src/app/components/cms/Feedback/Fields/Date/Datefield.spec.tsx @@ -0,0 +1,82 @@ +import { render, screen } from '@/config/test-utils' + +import DateField from './DateField' + +describe('DateField component', () => { + const mockProps = { + label: 'Date of Birth', + helpText: 'Enter a memorable date', + cleanName: 'dob', + } + + test('renders DateField component with label, helpText, and cleanName props', () => { + render() + expect(screen.getByText('Date of Birth')).toBeInTheDocument() + expect(screen.getByText('Enter a memorable date')).toBeInTheDocument() + }) + + test('renders the correct label text', () => { + render() + const label = screen.getByText('Date of Birth') + expect(label).toBeInTheDocument() + expect(label).toHaveAttribute('for', 'dob') + }) + + test('renders help text when provided', () => { + render() + expect(screen.getByText('Enter a memorable date')).toBeInTheDocument() + }) + + test('does not render help text when empty', () => { + render() + const helpText = screen.queryByText('Enter a valid date') + expect(helpText).not.toBeInTheDocument() + }) + + test('renders day, month, and year input fields', () => { + render() + const dayInput = screen.getByLabelText(/day/i) + const monthInput = screen.getByLabelText(/month/i) + const yearInput = screen.getByLabelText(/year/i) + + expect(dayInput).toBeInTheDocument() + expect(monthInput).toBeInTheDocument() + expect(yearInput).toBeInTheDocument() + }) + + test('renders labels for Day, Month, and Year', () => { + render() + + // Check for the presence of each label + expect(screen.getByLabelText(/day/i)).toBeInTheDocument() + expect(screen.getByLabelText(/month/i)).toBeInTheDocument() + expect(screen.getByLabelText(/year/i)).toBeInTheDocument() + }) + + test('input fields have correct attributes', () => { + render() + + const dayInput = screen.getByLabelText(/day/i) + const monthInput = screen.getByLabelText(/month/i) + const yearInput = screen.getByLabelText(/year/i) + + expect(dayInput).toHaveAttribute('id', 'day') + expect(monthInput).toHaveAttribute('id', 'month') + expect(yearInput).toHaveAttribute('id', 'year') + + expect(dayInput).toHaveAttribute('name', 'day') + expect(monthInput).toHaveAttribute('name', 'month') + expect(yearInput).toHaveAttribute('name', 'year') + + expect(dayInput).toHaveAttribute('type', 'number') + expect(monthInput).toHaveAttribute('type', 'number') + expect(yearInput).toHaveAttribute('type', 'number') + + expect(dayInput).toHaveAttribute('min', '1') + expect(dayInput).toHaveAttribute('max', '31') + expect(monthInput).toHaveAttribute('min', '1') + expect(monthInput).toHaveAttribute('max', '12') + expect(yearInput).toHaveAttribute('min', '1900') + expect(yearInput).toHaveAttribute('max', '2100') + }) +}) diff --git a/src/mock-server/handlers/cms/pages/fixtures/page/feedback.ts b/src/mock-server/handlers/cms/pages/fixtures/page/feedback.ts index 58cc37f64..22957e11e 100644 --- a/src/mock-server/handlers/cms/pages/fixtures/page/feedback.ts +++ b/src/mock-server/handlers/cms/pages/fixtures/page/feedback.ts @@ -160,6 +160,19 @@ export const feedbackMock: PageResponse = { choices: '', default_value: 'true', }, + { + id: 11, + meta: { + type: 'forms.FormField', + }, + clean_name: 'enter_a_memorable_date', + label: 'Enter a memorable date', + field_type: 'date', + help_text: 'For example, 31 3 1980', + required: false, + choices: '', + default_value: '', + }, ], confirmation_slug: 'confirmation', confirmation_panel_title: 'Form submitted',