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

Conversation

jonnigs
Copy link
Member

@jonnigs jonnigs commented Nov 25, 2024

What

Field that looks like an input but is not editable and updates with changes to the application object

Why

This functionality is needed in a few places

Screenshots / Gifs

Screenshot 2024-11-26 at 12 57 36

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Formatting passes locally with my changes
  • I have rebased against main before asking for a review

Summary by CodeRabbit

  • New Features

    • Introduced a new DisplayField type for enhanced form field capabilities.
    • Added a DisplayFormField component for rendering display fields in forms.
    • Expanded the set of exported form field components to include DisplayFormField.
  • Bug Fixes

    • Improved handling of form state and localization within the new component.

Copy link
Contributor

coderabbitai bot commented Nov 25, 2024

Walkthrough

The changes introduce a new DisplayField type along with its builder function buildDisplayField in the fieldBuilders.ts file. Additionally, a new React component, DisplayFormField, is created to render this display field in forms. The DisplayField interface is defined in Fields.ts, including new properties and updates to existing enums. The DisplayFormField component manages form state and localization, enhancing the form handling capabilities of the application.

Changes

File Path Change Summary
libs/application/core/src/lib/fieldBuilders.ts Added DisplayField type and buildDisplayField function for creating display field configurations.
libs/application/types/src/lib/Fields.ts Introduced DisplayField interface, updated Field type union, and added DISPLAY to FieldTypes and FieldComponents enums.
libs/application/ui-fields/src/lib/DisplayFormField/DisplayFormField.tsx Created DisplayFormField React component for rendering display fields, managing state and localization.
libs/application/ui-fields/src/lib/index.ts Exported DisplayFormField component to make it available for use in other parts of the application.

Possibly related PRs

Suggested labels

automerge, deploy-feature

Suggested reviewers

  • Toti91
  • fjandakornid
  • eirikurn

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 25, 2024

Codecov Report

Attention: Patch coverage is 15.38462% with 22 lines in your changes missing coverage. Please review.

Project coverage is 35.56%. Comparing base (ffb273c) to head (b76b215).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...elds/src/lib/DisplayFormField/DisplayFormField.tsx 5.00% 19 Missing ⚠️
libs/application/core/src/lib/fieldBuilders.ts 25.00% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #17007      +/-   ##
==========================================
+ Coverage   35.54%   35.56%   +0.01%     
==========================================
  Files        6929     6929              
  Lines      146664   146611      -53     
  Branches    41693    41671      -22     
==========================================
+ Hits        52135    52139       +4     
+ Misses      94529    94472      -57     
Flag Coverage Δ
air-discount-scheme-web 0.00% <ø> (ø)
api 3.34% <ø> (ø)
application-api-files 62.11% <100.00%> (+0.06%) ⬆️
application-core 70.91% <50.00%> (-0.10%) ⬇️
application-system-api 38.73% <50.00%> (-0.01%) ⬇️
application-template-api-modules 27.78% <50.00%> (+<0.01%) ⬆️
application-templates-accident-notification 28.97% <25.00%> (-0.01%) ⬇️
application-templates-car-recycling 3.12% <ø> (ø)
application-templates-criminal-record 25.86% <25.00%> (-0.01%) ⬇️
application-templates-driving-license 18.18% <25.00%> (+0.01%) ⬆️
application-templates-estate 12.16% <25.00%> (+<0.01%) ⬆️
application-templates-example-payment 24.80% <25.00%> (+<0.01%) ⬆️
application-templates-financial-aid 15.80% <25.00%> (+0.01%) ⬆️
application-templates-general-petition 23.09% <25.00%> (+<0.01%) ⬆️
application-templates-inheritance-report 6.58% <25.00%> (+0.02%) ⬆️
application-templates-marriage-conditions 15.12% <25.00%> (+0.03%) ⬆️
application-templates-mortgage-certificate 43.27% <50.00%> (+0.02%) ⬆️
application-templates-parental-leave 29.81% <25.00%> (-0.13%) ⬇️
application-types 6.51% <0.00%> (-0.03%) ⬇️
application-ui-components 1.27% <ø> (ø)
application-ui-shell 20.17% <8.33%> (-0.04%) ⬇️
clients-charge-fjs-v2 24.11% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
libs/application/types/src/lib/Fields.ts 100.00% <100.00%> (ø)
libs/application/core/src/lib/fieldBuilders.ts 40.50% <25.00%> (-0.32%) ⬇️
...elds/src/lib/DisplayFormField/DisplayFormField.tsx 5.00% <5.00%> (ø)

... and 6 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ffb273c...b76b215. Read the comment docs.

@datadog-island-is
Copy link

datadog-island-is bot commented Nov 25, 2024

Datadog Report

All test runs 6a2b4df 🔗

21 Total Test Services: 0 Failed, 20 Passed
🔻 Test Sessions change in coverage: 4 decreased, 11 increased, 77 no change

Test Services
This report shows up to 10 services
Service Name Failed Known Flaky New Flaky Passed Skipped Total Time Code Coverage Change Test Service View
air-discount-scheme-web 0 0 0 2 0 7.16s 1 no change Link
api 0 0 0 4 0 3.1s 1 no change Link
application-api-files 0 0 0 2 0 4.21s 1 increased (+0.07%) Link
application-core 0 0 0 97 0 14.54s 1 decreased (-0.09%) Link
application-system-api 0 0 0 40 0 2m 15.78s 1 no change Link
application-template-api-modules 0 0 0 118 0 2m 3.48s 1 no change Link
application-templates-accident-notification 0 0 0 148 0 15.66s 1 no change Link
application-templates-criminal-record 0 0 0 2 0 10.17s 1 no change Link
application-templates-driving-license 0 0 0 13 0 13.35s 1 increased (+0.03%) Link
application-templates-example-payment 0 0 0 2 0 10.33s 1 increased (+0.01%) Link

🔻 Code Coverage Decreases vs Default Branch (4)

  • application-templates-parental-leave - jest 34.94% (-0.1%) - Details
  • application-core - jest 78.55% (-0.09%) - Details
  • application-ui-shell - jest 26.31% (-0.03%) - Details
  • application-types - jest 5.38% (-0.03%) - Details

@jonnigs jonnigs changed the title feat: start of display field feat(app-sys): Shared display field Nov 26, 2024
@jonnigs jonnigs marked this pull request as ready for review November 26, 2024 14:16
@jonnigs jonnigs requested a review from a team as a code owner November 26, 2024 14:16
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (6)
libs/application/ui-fields/src/lib/DisplayFormField/DisplayFormField.tsx (3)

14-24: Add JSDoc documentation for better component reusability.

Consider adding JSDoc documentation to describe the component's purpose, props, and usage examples. This will improve reusability across different NextJS apps.

Example addition:

+/**
+ * DisplayFormField - A read-only form field component that displays formatted values
+ * @param {Props} props - Component props
+ * @param {DisplayField} props.field - Display field configuration
+ * @param {object} props.application - Application context
+ * @returns {JSX.Element} Rendered form field
+ */
export const DisplayFormField = ({ field, application }: Props) => {

25-28: Consider safer state initialization and type handling.

Two potential improvements for robustness:

  1. Initialize displayValue with a type-safe default
  2. Validate locale type at runtime instead of using type assertion

Consider this safer implementation:

-const [displayValue, setDisplayValue] = useState(allValues[id])
+const [displayValue, setDisplayValue] = useState<string | number | null>(allValues[id] ?? null)

-const { formatMessage, lang: locale } = useLocale()
+const { formatMessage, lang } = useLocale()
+const locale = isValidLocale(lang) ? lang : defaultLocale

36-79: Extract hardcoded values and simplify conditional rendering.

A few suggestions for improvement:

  1. The "blue" background color should be configurable via props
  2. The variant type checking could be more type-safe

Consider these improvements:

+const VALID_VARIANTS = ['currency', 'textarea'] as const
+type ValidVariant = typeof VALID_VARIANTS[number]

+interface Props extends FieldBaseProps {
+  field: DisplayField
+  backgroundColor?: string
+}

-backgroundColor="blue"
+backgroundColor={props.backgroundColor ?? 'blue'}

-variant === 'currency' || variant === 'textarea'
+VALID_VARIANTS.includes(variant as ValidVariant)
libs/application/ui-fields/src/lib/index.ts (1)

34-34: LGTM! Consider organizing exports alphabetically.

The new export follows the established patterns and coding guidelines, making the DisplayFormField component reusable across NextJS apps while enabling effective tree-shaking. However, for better maintainability, consider organizing all exports alphabetically.

Here's how the new export would fit alphabetically (between DescriptionFormField and DividerFormField):

export { DescriptionFormField } from './DescriptionFormField/DescriptionFormField'
+export { DisplayFormField } from './DisplayFormField/DisplayFormField'
export { DividerFormField } from './DividerFormField/DividerFormField'
libs/application/core/src/lib/fieldBuilders.ts (1)

1014-1043: LGTM! The implementation follows the established patterns.

The buildDisplayField function:

  • Correctly utilizes the extractCommonFields utility
  • Properly handles display-specific properties
  • Maintains consistency with other field builders
  • Aligns with the PR objective of creating a non-editable display field

Consider documenting the function with JSDoc comments to maintain consistency with other documented builders like buildHiddenInputWithWatchedValue and buildHiddenInput. This would help developers understand the purpose and usage of the display field.

Example:

+/**
+ * Constructs a display field configuration object.
+ * This function creates a non-editable display field that automatically updates with changes to the application object.
+ *
+ * @param {Omit<DisplayField, 'type' | 'component' | 'children'>} data
+ * - `title`: Optional title for the display field
+ * - `value`: The value to be displayed
+ * - `suffix`: Optional suffix to be displayed after the value
+ * - `rightAlign`: Optional flag to align the value to the right
+ */
 export const buildDisplayField = (
   data: Omit<DisplayField, 'type' | 'component' | 'children'>,
 ): DisplayField => {
libs/application/types/src/lib/Fields.ts (1)

801-812: Consider adding JSDoc comments for the DisplayField interface.

The interface implementation looks good and aligns with the PR objective of creating a non-editable display field. However, adding JSDoc comments would improve maintainability and help other developers understand the purpose and usage of this field type.

+/**
+ * Represents a read-only display field that automatically updates with changes to the application object.
+ * This field type resembles an input field but is non-editable.
+ */
 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
+  /** Function that derives the display value from form answers */
   value: (answers: FormValue) => string
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 050a2eb and 2084185.

📒 Files selected for processing (4)
  • libs/application/core/src/lib/fieldBuilders.ts (2 hunks)
  • libs/application/types/src/lib/Fields.ts (5 hunks)
  • libs/application/ui-fields/src/lib/DisplayFormField/DisplayFormField.tsx (1 hunks)
  • libs/application/ui-fields/src/lib/index.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
libs/application/core/src/lib/fieldBuilders.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/types/src/lib/Fields.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/DisplayFormField/DisplayFormField.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/application/ui-fields/src/lib/index.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🔇 Additional comments (7)
libs/application/ui-fields/src/lib/DisplayFormField/DisplayFormField.tsx (2)

1-12: LGTM! Well-structured imports and type definitions.

The code demonstrates good TypeScript practices with proper type definitions and modular imports from shared libraries.


1-80: Verify component's reusability across NextJS apps.

The component follows most best practices for reusability, but let's verify its integration capabilities.

libs/application/core/src/lib/fieldBuilders.ts (1)

49-49: LGTM!

The import of DisplayField type is correctly added to the existing imports from @island.is/application/types.

libs/application/types/src/lib/Fields.ts (4)

265-265: LGTM! Enum value follows naming convention.

The DISPLAY enum value in FieldTypes follows the established pattern of uppercase values.


302-302: LGTM! Component name follows convention.

The DISPLAY enum value in FieldComponents follows the established pattern of PascalCase component names with 'FormField' suffix.


850-850: LGTM! Field type union updated correctly.

The DisplayField type has been properly added to the Field union type, ensuring type safety throughout the application.


801-812: Verify the reusability of the DisplayField component.

The implementation follows TypeScript best practices and the coding guidelines for reusability across different NextJS apps. However, let's verify that this component is being used consistently across the codebase.

✅ Verification successful

Let me gather more information about the implementation and usage patterns.


The DisplayField component is well-implemented and consistently used across the application

The verification shows that the DisplayField component follows a consistent pattern and is properly integrated into the application's form field system:

  • It's correctly typed in libs/application/types/src/lib/Fields.ts
  • The implementation in DisplayFormField.tsx follows best practices:
    • Uses React Hook Form for form state management
    • Properly handles localization
    • Supports responsive styling
    • Implements consistent UI components from the design system
  • It's properly exported and integrated into the field builders system
  • The field builder implementation (buildDisplayField) maintains consistency with other field types
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of DisplayFormField component across the codebase
echo "Searching for DisplayFormField component usage..."
rg -l "DisplayFormField"

# Search for DisplayField type usage
echo "Searching for DisplayField type usage..."
ast-grep --pattern 'DisplayField'

Length of output: 633


Script:

#!/bin/bash
# Check the implementation of DisplayFormField component
echo "DisplayFormField implementation:"
cat libs/application/ui-fields/src/lib/DisplayFormField/DisplayFormField.tsx

# Check how it's being used in fieldBuilders
echo "Field builders usage:"
cat libs/application/core/src/lib/fieldBuilders.ts

# Check for any tests
echo "Looking for related tests:"
fd "DisplayFormField" -e test.tsx -e spec.tsx

Length of output: 25174

Copy link
Member

@Toti91 Toti91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@jonnigs jonnigs added the automerge Merge this PR as soon as all checks pass label Nov 26, 2024
@kodiakhq kodiakhq bot merged commit 3315456 into main Nov 26, 2024
79 checks passed
@kodiakhq kodiakhq bot deleted the feat/inao-shared-display-value-component branch November 26, 2024 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Merge this PR as soon as all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants