-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-sys): Shared display field (#17007)
* feat: start of display field * feat: display field * fix: revert changes to another template * feat: simplify useEffect logic and undo example form changes * chore: undo example form changes --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ffb273c
commit 3315456
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
libs/application/ui-fields/src/lib/DisplayFormField/DisplayFormField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
|
||
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters