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(web): Forms - datepicker field #17009

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Changes from 1 commit
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
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}
/>
)
}
RunarVestmann marked this conversation as resolved.
Show resolved Hide resolved
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
Loading