Skip to content

Commit

Permalink
fix: check max length for description
Browse files Browse the repository at this point in the history
  • Loading branch information
Chisomchima committed Nov 28, 2024
1 parent babb167 commit 231645c
Showing 1 changed file with 11 additions and 56 deletions.
67 changes: 11 additions & 56 deletions src/components/form/fields/DescriptionField.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,8 @@
import i18n from '@dhis2/d2-i18n'
import { TextAreaFieldFF } from '@dhis2/ui'
import React, { useMemo } from 'react'
import { Field as FieldRFF, useField } from 'react-final-form'
import { useParams } from 'react-router-dom'
import {
composeAsyncValidators,
required,
useCheckMaxLengthFromSchema,
useIsFieldValueUnique,
SchemaSection,
} from '../../../lib'

function useValidator({
schemaSection,
fieldName,
}: {
schemaSection: SchemaSection
fieldName: string
}) {
const params = useParams()
const modelId = params.id as string
const checkIsValueTaken = useIsFieldValueUnique({
model: schemaSection.namePlural,
field: fieldName,
id: modelId,
})

const checkMaxLength = useCheckMaxLengthFromSchema(
schemaSection.name,
fieldName
)

return useMemo(
() =>
composeAsyncValidators<string>([
checkIsValueTaken,
checkMaxLength,
required,
]),
[checkIsValueTaken, checkMaxLength]
)
}
import React from 'react'
import { Field as FieldRFF } from 'react-final-form'
import { SchemaSection, useCheckMaxLengthFromSchema } from '../../../lib'

export function DescriptionField({
helpText,
Expand All @@ -49,27 +11,20 @@ export function DescriptionField({
helpText?: string
schemaSection: SchemaSection
}) {
const validator = useValidator({ schemaSection, fieldName: 'description' })
const { meta } = useField('description', {
subscription: { validating: true },
})

const helpString =
helpText ||
i18n.t('A description should be clear and provide necessary details.')
const validate = useCheckMaxLengthFromSchema(
schemaSection.name,
'description'
)

return (
<FieldRFF<string | undefined>
loading={meta.validating}
<FieldRFF
component={TextAreaFieldFF}
dataTest="formfields-description"
inputWidth="400px"
name="description"
label={i18n.t('Description (required)', {
fieldLabel: i18n.t('Description'),
})}
helpText={helpString}
validate={(description?: string) => validator(description)}
label={i18n.t('Description')}
helpText={helpText}
validate={validate}
validateFields={[]}
/>
)
Expand Down

0 comments on commit 231645c

Please sign in to comment.