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(form-v2): add MyInfo badge to form builder for MyInfo fields #4538

Merged
merged 6 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
augmentWithMyInfoDisplayValue,
extractPreviewValue,
hasExistingFieldValue,
isMyInfo,
} from '~features/myinfo/utils'

import { useBuilderAndDesignContext } from '../../BuilderAndDesignContext'
Expand Down Expand Up @@ -100,6 +101,8 @@ export const FieldRowContainer = ({

const colorTheme = useDesignColorTheme()

const isMyInfoField = useMemo(() => isMyInfo(field), [field])

const defaultFieldValues = useMemo(() => {
if (field.fieldType === BasicField.Table) {
return {
Expand Down Expand Up @@ -281,7 +284,11 @@ export const FieldRowContainer = ({
opacity={isActive || !isHiddenByLogic ? '100%' : '30%'}
>
<FormProvider {...formMethods}>
<MemoFieldRow field={field} colorTheme={colorTheme} />
<MemoFieldRow
field={field}
colorTheme={colorTheme}
showMyInfoBadge={isMyInfoField}
/>
</FormProvider>
</Box>
<Collapse in={isActive} style={{ width: '100%' }}>
Expand Down Expand Up @@ -339,6 +346,7 @@ export const FieldRowContainer = ({
type MemoFieldRowProps = {
field: FormFieldDto
colorTheme?: FormColorTheme
showMyInfoBadge?: boolean
}

const MemoFieldRow = memo(({ field, ...rest }: MemoFieldRowProps) => {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/templates/Field/Date/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface DateFieldProps extends BaseFieldProps {
export const DateField = ({
schema,
colorTheme = FormColorTheme.Blue,
...fieldContainerProps
}: DateFieldProps): JSX.Element => {
const validationRules = useMemo(
() => createDateValidationRules(schema),
Expand Down Expand Up @@ -56,7 +57,7 @@ export const DateField = ({
const { control } = useFormContext<SingleAnswerFieldInput>()

return (
<FieldContainer schema={schema}>
<FieldContainer schema={schema} {...fieldContainerProps}>
<Controller
control={control}
name={schema._id}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/templates/Field/Dropdown/DropdownField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface DropdownFieldProps extends BaseFieldProps {
export const DropdownField = ({
schema,
colorTheme = FormColorTheme.Blue,
...fieldContainerProps
}: DropdownFieldProps): JSX.Element => {
const validationRules = useMemo(
() => createDropdownValidationRules(schema),
Expand All @@ -28,7 +29,7 @@ export const DropdownField = ({
const { control } = useFormContext<SingleAnswerFieldInput>()

return (
<FieldContainer schema={schema}>
<FieldContainer schema={schema} {...fieldContainerProps}>
<Controller
control={control}
rules={validationRules}
Expand Down
34 changes: 26 additions & 8 deletions frontend/src/templates/Field/FieldContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* provides.
*/
import { FieldError, useFormState } from 'react-hook-form'
import { FormControl } from '@chakra-ui/react'
import { Box, FormControl, Grid } from '@chakra-ui/react'
import { get } from 'lodash'

import { FormColorTheme } from '~shared/types/form'

import Badge from '~components/Badge'
import FormErrorMessage from '~components/FormControl/FormErrorMessage'
import FormLabel from '~components/FormControl/FormLabel'

Expand All @@ -34,6 +35,11 @@ export type BaseFieldProps = {
* Whether or not the field was prefilled.
*/
isPrefilled?: boolean

/**
* Whether the MyInfo badge should be shown next to the question name.
*/
showMyInfoBadge?: boolean
}

export interface FieldContainerProps extends BaseFieldProps {
Expand All @@ -44,6 +50,7 @@ export const FieldContainer = ({
schema,
children,
errorKey,
showMyInfoBadge,
}: FieldContainerProps): JSX.Element => {
const { errors, isSubmitting, isValid } = useFormState({ name: schema._id })

Expand All @@ -57,14 +64,25 @@ export const FieldContainer = ({
isInvalid={!!error}
id={schema._id}
>
<FormLabel
questionNumber={
schema.questionNumber ? `${schema.questionNumber}.` : undefined
}
description={schema.description}
<Grid
justynoh marked this conversation as resolved.
Show resolved Hide resolved
gridTemplateAreas={"'formlabel myinfobadge'"}
gridTemplateColumns={'1fr auto'}
>
{schema.title}
</FormLabel>
<FormLabel
gridArea="formlabel"
questionNumber={
schema.questionNumber ? `${schema.questionNumber}.` : undefined
}
description={schema.description}
>
{schema.title}
</FormLabel>
<Box hidden={!showMyInfoBadge} gridArea="myinfobadge">
<Badge variant="subtle" colorScheme="secondary">
MyInfo
</Badge>
</Box>
</Grid>
{children}
<FormErrorMessage>{error?.message}</FormErrorMessage>
</FormControl>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/templates/Field/ShortText/ShortTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ShortTextFieldProps extends BaseFieldProps {
export const ShortTextField = ({
schema,
isPrefilled,
...fieldContainerProps
}: ShortTextFieldProps): JSX.Element => {
const validationRules = useMemo(
() => createTextValidationRules(schema),
Expand All @@ -26,7 +27,7 @@ export const ShortTextField = ({
const { register } = useFormContext<SingleAnswerFieldInput>()

return (
<FieldContainer schema={schema}>
<FieldContainer schema={schema} {...fieldContainerProps}>
<Input
isPrefilled={isPrefilled}
aria-label={schema.title}
Expand Down