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

fix : clearable select and add a story in storybook #546

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JeanneGrenet
Copy link
Contributor

@JeanneGrenet JeanneGrenet commented Nov 7, 2024

Describe your changes

Fix the select with the clearable props. The value was not set to null when the field was cleared

Screenshots

Screen.Recording.2024-11-07.at.17.54.35.mov

Summary by CodeRabbit

  • New Features

    • Introduced a clearable color selection option in the form, allowing users to reset their choice.
    • Added a new documentation story demonstrating the clearable functionality.
  • Bug Fixes

    • Enhanced the handling of the select input's clear action to ensure proper state management.

Copy link

vercel bot commented Nov 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
start-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 7, 2024 4:57pm

Copy link
Contributor

coderabbitai bot commented Nov 7, 2024

Walkthrough

The changes in this pull request involve modifications to the zFormSchema function, where the color field is updated from a required enum to a nullable enum, allowing null values. A new story named Clearable is introduced in the documentation for the FieldSelect component, demonstrating a clearable selection option. Additionally, the onChange event handler of the Select component is updated to handle the clear action by setting the value to null when the action type is 'clear'.

Changes

File Path Change Summary
src/components/Form/FieldSelect/docs.stories.tsx Updated zFormSchema to make color nullable. Added new story Clearable demonstrating clearable selection.
src/components/Form/FieldSelect/index.tsx Modified onChange handler to set value to null on clear action; added comment regarding TypeScript error on selectProps.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant FieldSelect
    participant zFormSchema

    User->>FieldSelect: Select color option
    FieldSelect->>zFormSchema: Validate color selection
    zFormSchema-->>FieldSelect: Return selected color
    FieldSelect-->>User: Display selected color

    User->>FieldSelect: Clear selection
    FieldSelect->>FieldSelect: Check action type
    FieldSelect->>FieldSelect: Set value to null
    FieldSelect-->>User: Display no selection
Loading

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.

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.

Copy link

sonarqubecloud bot commented Nov 7, 2024

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 (1)
src/components/Form/FieldSelect/docs.stories.tsx (1)

148-175: Consider enhancing the Clearable story documentation

The implementation correctly demonstrates the clearable functionality and follows the established story pattern. Consider these enhancements:

 export const Clearable = () => {
+  // Demonstrates a select field that can be cleared, setting its value to null
   const form = useForm<FormSchema>({
     ...formOptions,
+    defaultValues: {
+      color: 'red', // Shows the clear button when a value is selected
+    },
   });

   return (
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2e44832 and c5a0ae0.

📒 Files selected for processing (2)
  • src/components/Form/FieldSelect/docs.stories.tsx (2 hunks)
  • src/components/Form/FieldSelect/index.tsx (1 hunks)
🔇 Additional comments (2)
src/components/Form/FieldSelect/index.tsx (1)

64-69: LGTM! Clear action is now properly handled.

The implementation correctly sets the value to null when the select is cleared, which fixes the original issue. The early return pattern keeps the code clean and maintainable.

src/components/Form/FieldSelect/docs.stories.tsx (1)

15-15: LGTM: Schema update correctly supports clearable functionality

The modification to make the color field nullable is appropriate and necessary to support the clearable select feature.

Comment on lines +70 to +73
// @ts-expect-error TODO should fix the typing. This error pops when
// we propagate the `selectProps`
onChange(newValue?.value);
}}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

The typing issue can be fixed by properly typing the onChange handler

The @ts-expect-error occurs because the component is using chakra-react-select's types but not properly handling the onChange event typing. The fix would involve:

  • The base Select component uses generics from chakra-react-select
  • FieldSelect and FieldMultiSelect need to properly type their onChange handlers to match the generic type parameters

Example locations that need attention:

  • src/components/Form/FieldSelect/index.tsx: Add proper typing for onChange handler
  • src/components/Form/FieldMultiSelect/index.tsx: Same typing issue exists here
🔗 Analysis chain

Consider addressing the typing issue.

The @ts-expect-error indicates a typing issue with selectProps propagation. While the functionality works, improving type safety would prevent potential runtime issues.

Let's check for similar typing patterns in other components:


Let me gather more context about the Select components and their type definitions.


Let me check the base Select component implementation to understand the typing issue better.


Let me check the Chakra UI Select implementation since the code seems to be using Chakra's wrapped version of react-select.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for similar Select component usage patterns to understand the typing approach
ast-grep --pattern 'onChange={(newValue, actionMeta) => { $$$ }}'

# Look for other @ts-expect-error occurrences that might provide insights
rg "@ts-expect-error" -A 2

Length of output: 1100


Script:

#!/bin/bash
# Check the type definitions and imports in both Select components
rg -A 10 "interface.*Props" src/components/Form/FieldSelect/
rg -A 10 "interface.*Props" src/components/Form/FieldMultiSelect/

# Look for Select component imports and their types
rg "import.*Select" src/components/Form/Field*

# Check the component definitions
ast-grep --pattern 'const FieldSelect: $_ = ($_) => { $$$ }'
ast-grep --pattern 'const FieldMultiSelect: $_ = ($_) => { $$$ }'

Length of output: 914


Script:

#!/bin/bash
# Check the base Select component implementation and its types
rg -A 10 "interface.*Props" src/components/Select/
rg -A 10 "type.*Props" src/components/Select/

# Look for the Select component implementation
ast-grep --pattern 'const Select = ($_) => { $$$ }'
ast-grep --pattern 'export interface SelectProps { $$$ }'

# Check how the value and onChange props are typed
rg "onChange.*:" src/components/Select/

Length of output: 2048


Script:

#!/bin/bash
# Check for Chakra Select component imports and types
rg "import.*chakra.*select" -i src/components/Select/
rg "ChakraReactSelect" -A 5 src/components/Select/

# Look for the value type definitions
rg "type.*Value" src/components/Select/
rg "interface.*Value" src/components/Select/

# Check for any type exports or re-exports
rg "export type" src/components/Select/

Length of output: 991

@@ -12,7 +12,7 @@ export default {
type FormSchema = z.infer<ReturnType<typeof zFormSchema>>;
const zFormSchema = () =>
z.object({
color: z.enum(['red', 'green', 'blue']),
Copy link
Member

Choose a reason for hiding this comment

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

We don't want it to be nullish for most of the stories. For the change you made, please create a new schema

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants