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: StudioCodeListEditor - display Numberfield or Checkbox based on value type #14398

Merged
merged 43 commits into from
Feb 11, 2025

Conversation

ErlingHauan
Copy link
Contributor

@ErlingHauan ErlingHauan commented Jan 10, 2025

Description

  • Renders textfield, numberfield or checkbox depending on if the codelist item's value is a string, number or boolean.
  • Adapted addEmptyCodeListItem to return '', 0 or false, depending on the previous item's type.

bilde


bilde

Not in scope for this PR:

  • Choosing type when creating a new codelist.
  • Dropdown for switching types when editing an existing codelist.
  • Replacing checkboxes with a switch/tab.

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced code list editor to support multiple data types (string, number, boolean).
    • Improved input handling for different value types in code list items.
    • Added new error messages for validation checks in the code list editor.
  • Tests

    • Added comprehensive test cases for type-specific input handling.
    • Expanded test coverage for validation functions related to multiple types and undefined values.
    • Introduced new test data for various scenarios, including multiple types and undefined values.
  • Refactor

    • Updated input cell components to dynamically render based on value type.
    • Improved type safety for code list item management.
    • Enhanced logic for adding new code list items based on existing values.
    • Renamed functions and variables for clarity and consistency.

@github-actions github-actions bot added solution/studio/designer Issues related to the Altinn Studio Designer solution. frontend labels Jan 10, 2025
Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.75%. Comparing base (75e989a) to head (98f0a8c).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14398      +/-   ##
==========================================
+ Coverage   95.73%   95.75%   +0.01%     
==========================================
  Files        1908     1912       +4     
  Lines       24878    24924      +46     
  Branches     2846     2851       +5     
==========================================
+ Hits        23818    23865      +47     
  Misses        799      799              
+ Partials      261      260       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

coderabbitai bot commented Jan 17, 2025

📝 Walkthrough

Walkthrough

The pull request introduces significant enhancements to the StudioCodeListEditor component, focusing on improving type handling for code list items. It adds new test cases for various data types, reorganizes input components to dynamically render based on value types, and updates utility functions for managing empty items. Additionally, new test data files are created to cover scenarios involving multiple types and undefined values, while modifications are made to error handling and validation logic throughout the component.

Changes

File Change Summary
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditor.test.tsx Added new test cases for type handling, verifying rendering and saving of different data types.
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx Introduced TypedInputCell, NumberfieldCell, and CheckboxCell to support dynamic input types; updated handleValueChange to accept CodeListItemValue.
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/utils.ts Updated changeValue function to accept CodeListItemValue.
frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithBooleanValues.ts Added test data for boolean values with CodeListItem instances.
frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithNumberValues.ts Added test data for numerical values with CodeListItem instances.
frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.test.ts Enhanced test cases for addNewCodeListItem function to handle different last item types.
frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.ts Added constants for empty items and helper functions for determining appropriate empty items based on type.
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditor.tsx Renamed addEmptyCodeListItem to addNewCodeListItem in the component.
frontend/libs/studio-components/src/components/StudioCodelistEditor/types/TypeofResult.ts Introduced new type alias TypeofResult for structured type representation.
frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithMultipleTypes.ts Added test data for code list containing multiple types of items.
frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithUndefinedValues.ts Added test data for code list containing items with undefined values.
frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/texts.ts Updated error messages to include new validation checks.
frontend/libs/studio-components/src/components/StudioCodelistEditor/types/ValueError.ts Expanded ValueError type to include additional error conditions.
frontend/libs/studio-components/src/components/StudioCodelistEditor/validation/validation.test.ts Added test cases for new validation scenarios involving multiple types and undefined values.
frontend/libs/studio-components/src/components/StudioCodelistEditor/validation/validation.ts Enhanced findValueError function to handle additional validation checks.
frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/hooks/useCodeListEditorTexts.ts Updated error message keys in valueErrors object for clarity.
frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListEditor.test.tsx Updated test data and renamed input retrieval function for clarity.
frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/hooks/useOptionListEditorTexts.ts Updated error message keys in valueErrors object for consistency.
frontend/libs/studio-pure-functions/src/ArrayUtils/ArrayUtils.test.ts Added tests for new utility functions and corrected describe block names.
frontend/libs/studio-pure-functions/src/ArrayUtils/ArrayUtils.ts Introduced new static methods for type extraction and validation.

Possibly related PRs

Suggested labels

quality/testing, skip-releasenotes, skip-documentation

Suggested reviewers

  • github-actions
  • Konrad-Simso

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.test.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-storybook".

(The package "eslint-plugin-storybook" was not found when loaded as a Node module from the directory "/frontend/libs/studio-components".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-storybook@latest --save-dev

The plugin "eslint-plugin-storybook" was referenced from the config file in "frontend/libs/studio-components/.eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@ErlingHauan ErlingHauan changed the title Add numberfield to code list editor Add type selector to code list editor Jan 20, 2025
@ErlingHauan ErlingHauan changed the title Add type selector to code list editor feat: StudioCodeListEditor - display Numberfield or Checkbox based on value type Jan 20, 2025
@ErlingHauan ErlingHauan marked this pull request as ready for review January 22, 2025 12:08
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: 0

🧹 Nitpick comments (6)
frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.ts (1)

38-41: Consider adding explicit array bounds check.

The function assumes the array has elements when accessing the last item. While the current implementation is protected by the check in getEmptyItem, it would be more robust to add an explicit check here.

 function getTypeOfLastValue(codeList: CodeList) {
+  if (codeList.length === 0) return 'string';
   const lastCodeListItem = codeList[codeList.length - 1];
   return typeof lastCodeListItem.value as 'string' | 'number' | 'boolean';
 }
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx (3)

128-129: Consider explicit undefined handling for numberfield.

The comment about empty numberfield producing undefined is implementation-specific. Consider making this behavior more explicit:

-const shouldRenderNumberfield = typeof value === 'number' || value === undefined; // Empty numberfield produces undefined
+const shouldRenderNumberfield = typeof value === 'number' || value === undefined;
+const isEmptyNumberfield = value === undefined;

166-189: Consider adding number validation constraints.

The NumberfieldCell could benefit from input validation to ensure data integrity:

 const NumberfieldCell = forwardRef<HTMLInputElement, InputCellProps>(
   ({ label, value, onChange, onFocus, autoComplete }, ref) => {
     const handleNumberChange = useCallback(
       (numberValue: number): void => {
+        // Add validation if needed
+        if (isNaN(numberValue)) return;
         onChange(numberValue);
       },
       [onChange],
     );

     return (
       <StudioInputTable.Cell.Numberfield
         ref={ref}
         aria-label={label}
         autoComplete={autoComplete}
         className={classes.textfieldCell}
+        min={0} // Add if applicable
+        max={100} // Add if applicable
         onChange={handleNumberChange}
         onFocus={onFocus}
         value={value as number}
       />
     );
   },
 );

191-215: Optimize checkbox value handling.

The CheckboxCell duplicates the string value. Consider simplifying:

 const CheckboxCell = forwardRef<HTMLInputElement, InputCellProps>(
   ({ label, value, onChange, onFocus }, ref) => {
     const handleBooleanChange = useCallback(
       (event: React.ChangeEvent<HTMLInputElement>): void => {
         onChange(event.target.checked);
       },
       [onChange],
     );

+    const stringValue = String(value);
     return (
       <StudioInputTable.Cell.Checkbox
         ref={ref}
         aria-label={label}
         onChange={handleBooleanChange}
         onFocus={onFocus}
         checked={value as boolean}
-        value={String(value)}
+        value={stringValue}
       >
-        {String(value)}
+        {stringValue}
       </StudioInputTable.Cell.Checkbox>
     );
   },
 );
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditor.test.tsx (1)

484-546: Consider adding edge case tests.

The type handling tests are comprehensive but could benefit from additional edge cases:

  • Test handling of invalid number inputs
  • Test handling of empty/null values
  • Test handling of type conversion edge cases (e.g., "true"/"false" strings vs booleans)

Example test case:

it('Handles invalid number input gracefully', async () => {
  const user = userEvent.setup();
  renderCodeListEditor({ codeList: codeListWithNumberValues });

  const valueInput = screen.getByRole('textbox', { name: texts.itemValue(1) });
  await user.type(valueInput, 'not-a-number');
  await user.tab();

  expect(onBlurAny).not.toHaveBeenCalled();
});
frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithNumberValues.ts (1)

4-23: Enhance test data coverage with diverse scenarios.

While the current test items are well-structured, consider adding more diverse test cases to cover:

  • Edge cases (e.g., very large numbers, decimals, negative values)
  • More meaningful labels/descriptions that reflect the specific test scenarios
  • Comments explaining what each test case validates

Example enhancement:

 const item1: CodeListItem = {
-  description: 'Test 1 description',
-  helpText: 'Test 1 help text',
-  label: 'Test 1',
-  value: 1,
+  description: 'Basic positive integer',
+  helpText: 'Tests handling of simple positive numbers',
+  label: 'Positive Integer',
+  value: 42,
 };

 const item2: CodeListItem = {
-  description: 'Test 2 description',
-  helpText: 'Test 2 help text',
-  label: 'Test 2',
-  value: 2,
+  description: 'Decimal number',
+  helpText: 'Tests handling of decimal numbers',
+  label: 'Decimal',
+  value: 3.14,
 };

 const item3: CodeListItem = {
-  description: 'Test 3 description',
-  helpText: 'Test 3 help text',
-  label: 'Test 3',
-  value: 3,
+  description: 'Negative number',
+  helpText: 'Tests handling of negative numbers',
+  label: 'Negative',
+  value: -1,
 };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 941cd60 and 3f0cdc3.

📒 Files selected for processing (7)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditor.test.tsx (2 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx (5 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/utils.ts (2 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithBooleanValues.ts (1 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithNumberValues.ts (1 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.test.ts (2 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Testing
  • GitHub Check: Build environment and run e2e test
🔇 Additional comments (8)
frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithBooleanValues.ts (1)

4-16: LGTM! Well-structured test data.

The test data provides good coverage with both true and false boolean values, and includes all necessary fields (description, helpText, label, value) for comprehensive testing.

frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/utils.ts (1)

12-14: LGTM! Good type safety improvement.

The change from string to CodeListItemValue type enhances type safety while maintaining immutability. This aligns well with the PR's objective to support multiple value types.

frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.ts (1)

5-18: LGTM! Well-structured empty item templates.

The empty item templates are well-defined with appropriate default values for each type.

frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.test.ts (1)

30-60: LGTM! Excellent test coverage.

The test cases thoroughly cover all scenarios:

  • Empty code list handling
  • Type-specific empty item addition for string, number, and boolean
  • Mixed type sequences
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx (2)

Line range hint 52-56: LGTM! Type-safe value handling implemented.

The function now correctly handles multiple value types (string, number, boolean) in a type-safe manner.


257-257: LGTM! Consistent use of TypedInputCell.

The TextResourceIdCell correctly uses the new TypedInputCell component, maintaining consistency across the codebase.

frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/codeListWithNumberValues.ts (2)

1-2: LGTM! Clean type imports.

The imports follow TypeScript best practices by using the 'type' keyword for better tree-shaking.


25-25: Verify CodeList type compatibility with number values.

The export is properly typed, but let's verify that the CodeList type definition supports number values for the value property.

✅ Verification successful

Number values are fully supported by CodeList type

The type system explicitly supports number values through CodeListItemValue = string | boolean | number union type. The implementation is type-safe.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check CodeList and CodeListItem type definitions for number value support

# Search for CodeList type definition
echo "Searching for CodeList type definition..."
ast-grep --pattern 'type CodeList = $_'

# Search for CodeListItem type definition
echo "Searching for CodeListItem type definition..."
ast-grep --pattern 'type CodeListItem = {
  $$$
  value: $_
  $$$
}'

Length of output: 3187


Script:

#!/bin/bash
# Search for CodeListItem definition with a simpler pattern
echo "Searching for CodeListItem definition..."
rg -A 10 "type CodeListItem" || rg -A 10 "interface CodeListItem"

# Also check the file where we found CodeList type
echo "Checking CodeList.ts file content..."
cat frontend/libs/studio-components/src/components/StudioCodelistEditor/types/CodeList.ts

Length of output: 1240

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: 0

♻️ Duplicate comments (1)
frontend/libs/studio-pure-functions/src/ArrayUtils/ArrayUtils.test.ts (1)

293-303: Add more test cases for better coverage.

The test suite for extractUniqueTypes needs additional test cases to ensure robust behavior.

Apply this diff to add more test cases:

   describe('extractUniqueTypes', () => {
     it('returns array with one occurence of every type from input array', () => {
       const array = ['hello', 'world', 1, 2, true, false, undefined];
       expect(ArrayUtils.extractUniqueTypes(array)).toEqual([
         'string',
         'number',
         'boolean',
         'undefined',
       ]);
     });
+
+    it('returns empty array for empty input', () => {
+      expect(ArrayUtils.extractUniqueTypes([])).toEqual([]);
+    });
+
+    it('returns single type for array with duplicate types', () => {
+      expect(ArrayUtils.extractUniqueTypes([1, 2, 3])).toEqual(['number']);
+    });
+
+    it('handles null values', () => {
+      expect(ArrayUtils.extractUniqueTypes([null])).toEqual(['object']);
+    });
   });
🧹 Nitpick comments (1)
frontend/libs/studio-pure-functions/src/ArrayUtils/ArrayUtils.test.ts (1)

305-321: Add edge cases for better test coverage.

The test suite for hasSingleType would benefit from additional edge cases.

Apply this diff to add more test cases:

   describe('hasSingleType', () => {
     it('returns true for an array with all strings', () => {
       expect(ArrayUtils.hasSingleType(['a', 'b', 'c'])).toBe(true);
     });

     it('returns true for an array with all numbers', () => {
       expect(ArrayUtils.hasSingleType([1, 2, 3])).toBe(true);
     });

     it('returns false for an array with mixed types', () => {
       expect(ArrayUtils.hasSingleType([1, 'a', true])).toBe(false);
     });

     it('returns false for an empty array', () => {
       expect(ArrayUtils.hasSingleType([])).toBe(false);
     });
+
+    it('returns true for an array with all booleans', () => {
+      expect(ArrayUtils.hasSingleType([true, false, true])).toBe(true);
+    });
+
+    it('returns true for an array with all nulls', () => {
+      expect(ArrayUtils.hasSingleType([null, null])).toBe(true);
+    });
+
+    it('returns true for an array with all undefined values', () => {
+      expect(ArrayUtils.hasSingleType([undefined, undefined])).toBe(true);
+    });
+
+    it('returns true for a single-element array', () => {
+      expect(ArrayUtils.hasSingleType([1])).toBe(true);
+    });
   });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5745260 and adae170.

📒 Files selected for processing (1)
  • frontend/libs/studio-pure-functions/src/ArrayUtils/ArrayUtils.test.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build environment and run e2e test
  • GitHub Check: Testing
  • GitHub Check: CodeQL
🔇 Additional comments (2)
frontend/libs/studio-pure-functions/src/ArrayUtils/ArrayUtils.test.ts (2)

60-60: LGTM! Improved test suite organization.

The rename from ArrayUtils.intersection to intersection improves consistency with other test suite names.


241-241: LGTM! Fixed typo in test suite name.

The rename from rplaceItemsByValue to replaceItemsByValue fixes the typo in the test suite name.

@ErlingHauan ErlingHauan assigned TomasEng and unassigned ErlingHauan Feb 10, 2025
@TomasEng TomasEng assigned ErlingHauan and unassigned TomasEng Feb 10, 2025
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: 0

🧹 Nitpick comments (2)
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx (1)

162-185: Remove unnecessary string conversion.

The value={String(value)} prop appears unnecessary since the checkbox's value isn't used in the component's functionality.

-        value={String(value)}
frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.ts (1)

6-19: Add JSDoc comments to document the purpose of these constants.

The constants are well-defined with appropriate default values for each type. Consider adding JSDoc comments to explain their purpose and usage.

+/**
+ * Default empty item for string values in a code list.
+ */
 export const emptyStringItem: CodeListItem = {
   value: '',
   label: '',
 };

+/**
+ * Default empty item for number values in a code list.
+ */
 export const emptyNumberItem: CodeListItem = {
   value: 0,
   label: '',
 };

+/**
+ * Default empty item for boolean values in a code list.
+ */
 export const emptyBooleanItem: CodeListItem = {
   value: false,
   label: '',
 };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between adae170 and f26f5be.

📒 Files selected for processing (5)
  • frontend/language/src/nb.json (1 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx (5 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/texts.ts (1 hunks)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.ts (1 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/hooks/useCodeListEditorTexts.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/hooks/useCodeListEditorTexts.ts
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/texts.ts
  • frontend/language/src/nb.json
🧰 Additional context used
🧠 Learnings (1)
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx (2)
Learnt from: ErlingHauan
PR: Altinn/altinn-studio#14398
File: frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx:128-136
Timestamp: 2025-02-10T11:28:12.444Z
Learning: The StudioCodeListEditor component's handling of undefined values (when StudioDecimalField is cleared) is verified through the test case that checks if a numberfield is rendered with inputMode='decimal'.
Learnt from: ErlingHauan
PR: Altinn/altinn-studio#14398
File: frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx:0-0
Timestamp: 2025-02-10T11:46:29.320Z
Learning: The Numberfield component handles number validation internally and only calls onChange with valid number or undefined values, making additional validation in parent components unnecessary.
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build environment and run e2e test
  • GitHub Check: Testing
  • GitHub Check: CodeQL
🔇 Additional comments (9)
frontend/libs/studio-components/src/components/StudioCodelistEditor/StudioCodeListEditorRow/StudioCodeListEditorRow.tsx (5)

51-57: LGTM! Type-safe value handling implemented.

The updated handleValueChange function now correctly accepts the union type CodeListItemValue, enabling type-safe handling of string, number, and boolean values.


108-137: Well-structured type-safe input handling!

The TypedInputCell implementation:

  • Uses generic types for type-safety
  • Handles all value types cleanly with a switch case
  • Effectively uses the rest pattern for prop forwarding
  • Properly handles the undefined case from StudioDecimalField (verified by tests)

139-161: Clean NumberfieldCell implementation!

The component:

  • Correctly implements forwardRef pattern
  • Uses rest pattern for clean prop forwarding
  • Relies on Numberfield's built-in validation (per previous discussion)

186-208: LGTM! Clean TextfieldCell implementation.

The component follows React best practices with forwardRef and clean prop forwarding.


219-226: LGTM! Consistent use of TypedInputCell.

The component correctly uses TypedInputCell with a safe empty string fallback for currentId.

frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.ts (4)

1-4: LGTM!

The imports are well-organized and include all necessary types and utilities.


21-24: LGTM!

The function is well-named and correctly composes the helper functions.


39-45: LGTM!

The function includes proper error handling and uses ArrayUtils.last as suggested in the past review comments.


26-37: Consider adding type validation for the switch cases.

The switch statement handles the basic types but might need to handle additional TypeScript types that could appear in the typeof result.

❌ Verification inconclusive

🏁 Script executed:

#!/bin/bash
# Search for usage of CodeListItem.value to verify all possible types
ast-grep --pattern 'interface CodeListItem {
  $$$
  value: $_
  $$$
}'

Length of output: 73


Subject: Manual Verification Needed on Type Handling in createNewEmptyItem

Our initial search for the CodeListItem interface did not return any results, so we couldn’t confirm whether its value property might receive types other than those handled in the switch statement. Given this, it’s not clear if additional typeof outcomes (for example, "string", "bigint", "symbol", etc.) are possible.

Please manually verify:

  • The definition of and expected types for the CodeListItem.value property.
  • The possible outcomes from getTypeOfLastValue.

This manual check will ensure whether explicit type validation is necessary.

@ErlingHauan ErlingHauan assigned TomasEng and unassigned ErlingHauan Feb 10, 2025
@ErlingHauan ErlingHauan requested a review from TomasEng February 10, 2025 14:06
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: 0

🧹 Nitpick comments (2)
frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.test.ts (2)

31-44: LGTM! Consider adding documentation about type handling.

The tests are well-structured and cover the main scenarios. Consider adding a comment explaining the type inference strategy for empty lists and mixed-type scenarios.


71-76: Add test cases for non-empty codelists.

While testing the empty codelist case is important, consider adding test cases for:

  • Last value is a string
  • Last value is a number
  • Last value is a boolean

Would you like me to help draft these additional test cases?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f26f5be and 07ade2a.

📒 Files selected for processing (1)
  • frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.test.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Typechecking and linting
  • GitHub Check: Testing
  • GitHub Check: Build environment and run e2e test
  • GitHub Check: CodeQL
🔇 Additional comments (2)
frontend/libs/studio-components/src/components/StudioCodelistEditor/utils.test.ts (2)

2-11: LGTM! Clear and descriptive imports.

The renamed function and new imports clearly indicate their purpose and align well with the feature's objectives.


55-62: Consider documenting boolean codelist limitations.

Based on past discussions, boolean codelists typically only contain true and false values. While the current implementation allows flexibility for single-value boolean lists (as seen in the example here), it might be worth adding a comment explaining this design decision.

Would you like me to help draft documentation that explains the rationale behind allowing flexible boolean codelists?

Copy link
Contributor

@Konrad-Simso Konrad-Simso left a comment

Choose a reason for hiding this comment

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

Tested ok

@ErlingHauan ErlingHauan merged commit 880e30e into main Feb 11, 2025
17 checks passed
@ErlingHauan ErlingHauan deleted the add-numberfield-to-code-list-editor branch February 11, 2025 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ui-editor Area: Related to the designer tool for assembling app UI in Altinn Studio. frontend solution/studio/designer Issues related to the Altinn Studio Designer solution. team/studio-domain1
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

4 participants