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(app-sys): Shared display field #17007

Merged
merged 7 commits into from
Nov 26, 2024
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
32 changes: 32 additions & 0 deletions libs/application/core/src/lib/fieldBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
SliderField,
MaybeWithApplication,
MaybeWithApplicationAndFieldAndLocale,
DisplayField,
FieldsRepeaterField,
} from '@island.is/application/types'
import { Locale } from '@island.is/shared/types'
Expand Down Expand Up @@ -1009,3 +1010,34 @@ export const buildSliderField = (
saveAsString,
}
}

export const buildDisplayField = (
data: Omit<DisplayField, 'type' | 'component' | 'children'>,
): DisplayField => {
const {
title,
titleVariant,
label,
variant,
marginTop,
marginBottom,
value,
suffix,
rightAlign,
} = data
return {
...extractCommonFields(data),
title,
titleVariant,
label,
variant,
marginTop,
marginBottom,
type: FieldTypes.DISPLAY,
component: FieldComponents.DISPLAY,
children: undefined,
value,
suffix,
rightAlign,
}
}
18 changes: 17 additions & 1 deletion libs/application/types/src/lib/Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
StaticText,
} from './Form'
import { ApolloClient } from '@apollo/client'
import { Application } from './Application'
import { Application, FormValue } from './Application'
import { CallToAction } from './StateMachine'
import { Colors, theme } from '@island.is/island-ui/theme'
import { Condition } from './Condition'
Expand Down Expand Up @@ -262,6 +262,7 @@ export enum FieldTypes {
ACCORDION = 'ACCORDION',
BANK_ACCOUNT = 'BANK_ACCOUNT',
SLIDER = 'SLIDER',
DISPLAY = 'DISPLAY',
}

export enum FieldComponents {
Expand Down Expand Up @@ -298,6 +299,7 @@ export enum FieldComponents {
ACCORDION = 'AccordionFormField',
BANK_ACCOUNT = 'BankAccountFormField',
SLIDER = 'SliderFormField',
DISPLAY = 'DisplayFormField',
}

export interface CheckboxField extends InputField {
Expand Down Expand Up @@ -796,6 +798,19 @@ export interface SliderField extends BaseField {
saveAsString?: boolean
}

export interface DisplayField extends BaseField {
readonly type: FieldTypes.DISPLAY
component: FieldComponents.DISPLAY
marginTop?: ResponsiveProp<Space>
marginBottom?: ResponsiveProp<Space>
titleVariant?: TitleVariants
suffix?: MessageDescriptor | string
rightAlign?: boolean
variant?: TextFieldVariant
label?: MessageDescriptor | string
value: (answers: FormValue) => string
}

export type Field =
| CheckboxField
| CustomField
Expand Down Expand Up @@ -832,3 +847,4 @@ export type Field =
| AccordionField
| BankAccountField
| SliderField
| DisplayField
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { formatTextWithLocale } from '@island.is/application/core'
import { DisplayField, FieldBaseProps } from '@island.is/application/types'
import { Box, Text } from '@island.is/island-ui/core'
import { useLocale } from '@island.is/localization'
import { InputController } from '@island.is/shared/form-fields'
import { useEffect, useState } from 'react'
import { useFormContext } from 'react-hook-form'
import { Locale } from '@island.is/shared/types'

interface Props extends FieldBaseProps {
field: DisplayField
}

export const DisplayFormField = ({ field, application }: Props) => {
const {
value,
id,
title,
titleVariant = 'h4',
label,
variant,
suffix,
rightAlign = false,
} = field
const { watch, setValue } = useFormContext()
const allValues = watch()
const { formatMessage, lang: locale } = useLocale()
const [displayValue, setDisplayValue] = useState(allValues[id])

useEffect(() => {
const newDisplayValue = value(allValues)
setDisplayValue(newDisplayValue)
setValue(id, newDisplayValue)
}, [allValues])
jonnigs marked this conversation as resolved.
Show resolved Hide resolved

return (
<Box paddingY={3}>
{title ? (
<Text variant={titleVariant} paddingBottom={1}>
{formatTextWithLocale(
title,
application,
locale as Locale,
formatMessage,
)}
</Text>
) : null}

<InputController
id={id}
name={id}
label={
label &&
formatTextWithLocale(
label,
application,
locale as Locale,
formatMessage,
)
}
rightAlign={rightAlign}
readOnly
backgroundColor="blue"
currency={variant === 'currency'}
suffix={
suffix &&
formatTextWithLocale(
suffix,
application,
locale as Locale,
formatMessage,
)
}
type={
variant === 'currency' || variant === 'textarea' ? undefined : variant
}
/>
</Box>
)
}
1 change: 1 addition & 0 deletions libs/application/ui-fields/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export { StaticTableFormField } from './StaticTableFormField/StaticTableFormFiel
export { AccordionFormField } from './AccordionFormField/AccordionFormField'
export { BankAccountFormField } from './BankAccountFormField/BankAccountFormField'
export { SliderFormField } from './SliderFormField/SliderFormField'
export { DisplayFormField } from './DisplayFormField/DisplayFormField'