Skip to content

Commit

Permalink
fix: add validations to datefield and small change to css of bulk upload
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminic committed Nov 25, 2024
1 parent e949388 commit daddeba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
38 changes: 26 additions & 12 deletions src/components/form/fields/DateField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CalendarInput, CalendarInputProps } from '@dhis2/ui'
import React from 'react'
import React, { useState } from 'react'
import { useField } from 'react-final-form'
import { selectedLocale, useSystemSetting } from '../../../lib'

Expand All @@ -12,6 +12,11 @@ type DateFieldProps = Omit<
label?: string
required?: boolean
}
type ValidationProps = {
error: boolean
validationText?: string
valid?: boolean
}
export function DateField({
name,
label,
Expand All @@ -20,35 +25,44 @@ export function DateField({
}: DateFieldProps) {
const calendar = useSystemSetting('keyCalendar')
const locale = selectedLocale
const { meta, input } = useField<string | undefined>(name, {
format: (value) => {
if (value) {
return value.slice(0, 10)
const [validation, setValidation] = useState<ValidationProps>({
error: false,
})

const { input } = useField<string | undefined>(name, {
validate: () => {
if (validation.error) {
return validation.validationText
}
return value
},
})

const handleChange: CalendarInputProps['onDateSelect'] = (payload) => {
input.onChange(payload?.calendarDateString)
const handleChange: CalendarInputProps['onDateSelect'] = (
payload: {
calendarDateString: string
validation?: ValidationProps
} | null
) => {
setValidation(payload?.validation || { error: false })
input.onChange(payload?.calendarDateString || '')
input.onBlur()
}

return (
<div style={{ width: '400px' }}>
{/* TODO: we can remove style above, once inputWidth for CalendarInput is fixed */}
<div>
<CalendarInput
inputWidth={'400px'}
date={input.value}
name={name}
calendar={calendar as CalendarInputProps['calendar']}
onDateSelect={handleChange}
timeZone={'utc'}
locale={locale}
error={!!(meta.touched && meta.invalid && meta.error)}
validationText={meta.touched ? meta.error : undefined}
onBlur={(_, e) => input.onBlur(e)}
clearable
label={required ? `${label} (required) *` : label}
{...validation}
valid={validation?.valid && input?.value !== ''}
{...calendarInputProps}
/>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/components/sectionList/bulk/Bulk.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
flex-shrink: 0;
flex-basis: 160px;
flex-grow: 1;
max-width: 250px;
min-width: 250px;
max-width: 300px;
}

.subtitle {
Expand All @@ -27,7 +28,7 @@
flex-basis: 160px;
flex-grow: 1;
flex-shrink: 0;
max-width: 250px;
max-width: 225px;
}

.addActionButton {
Expand Down

0 comments on commit daddeba

Please sign in to comment.