Skip to content

Commit

Permalink
Add datepicker field to forms
Browse files Browse the repository at this point in the history
  • Loading branch information
RunarVestmann committed Nov 25, 2024
1 parent fb6f69a commit 81c6d59
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion apps/web/components/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Box,
Button,
Checkbox,
DatePicker,
Input,
InputFileUpload,
RadioButton,
Expand All @@ -28,6 +29,7 @@ import {
} from '@island.is/web/graphql/schema'
import { useNamespace } from '@island.is/web/hooks'
import { useI18n } from '@island.is/web/i18n'
import { useDateUtils } from '@island.is/web/i18n/useDateUtils'
import { GET_NAMESPACE_QUERY } from '@island.is/web/screens/queries'
import { GENERIC_FORM_MUTATION } from '@island.is/web/screens/queries/Form'
import { isValidEmail } from '@island.is/web/utils/isValidEmail'
Expand Down Expand Up @@ -58,6 +60,7 @@ export enum FormFieldType {
TEXT = 'text',
DROPDOWN = 'dropdown',
RADIO = 'radio',
DATE = 'date',
}

interface FormFieldProps {
Expand Down Expand Up @@ -220,6 +223,23 @@ export const FormField = ({
)
case FormFieldType.INFORMATION:
return <Text>{field.informationText}</Text>
case FormFieldType.DATE: {
const currentYear = new Date().getFullYear()
return (
<DatePicker
required={field.required}
label={field.title}
placeholderText={field.placeholder}
handleChange={(date) => {
onChange(slug, date.toISOString())
}}
selected={value ? new Date(value) : null}
hasError={!!error}
minYear={currentYear - 82}
maxYear={currentYear + 2}
/>
)
}
default:
return null
}
Expand All @@ -232,6 +252,7 @@ type ErrorData = {

export const Form = ({ form }: FormProps) => {
const { activeLocale } = useI18n()
const { format } = useDateUtils()

const { data: namespaceResponse } = useQuery<Query, QueryGetNamespaceArgs>(
GET_NAMESPACE_QUERY,
Expand Down Expand Up @@ -465,6 +486,12 @@ export const Form = ({ form }: FormProps) => {
json.join(', ') ?? 'Ekkert svar'
}\n\n`
}
if (field.type === FormFieldType.DATE) {
const date = value
? format(new Date(value), 'do MMMM yyyy')
: 'Ekkert svar'
return `${field.title}\nSvar: ${date}\n\n`
}

return `${field.title}\nSvar: ${value ?? 'Ekkert svar'}\n\n`
})
Expand Down Expand Up @@ -600,7 +627,6 @@ export const Form = ({ form }: FormProps) => {
),
}
setData(_data)

submit({
variables: {
input: {
Expand Down

0 comments on commit 81c6d59

Please sign in to comment.