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: [Clear Button is active when no input is selected in Dropdown] #9264

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

Conversation

ANIR1604
Copy link
Contributor

@ANIR1604 ANIR1604 commented Dec 1, 2024

Proposed Changes

  • Fixes Clear button is active when no input is selected in dropdown #9263
  • Added a state const [hasTyped, setHasTyped] = useState(false);.
  • Modified handleInputChange to set hasTyped(true) when the user types
  • In handleSelectionChange, set setQuery(optionLabel(selectedOption)); to update query after selection.
  • Set hasTyped(true) after selection to ensure the "Clear Selection" button is shown.
  • In the ComboboxButton rendering logic, updated the condition for showing the "Clear Selection" button:
  • Added setHasTyped(false) in the onClick handler for clearing selection to reset the hasTyped state.

Behaviour after the changes

  • The website allows users to search and select options from a dropdown, with the input dynamically filtering results as the user types.
  • The "Clear Selection" button only appears after the user types or selects an option, making it easy to clear the selected value.
  • The input field reflects the current query, and the dropdown updates based on the user's input, offering a smooth and interactive search experience.

Screen Records

Screencast.from.2024-12-02.00-14-08.webm

Summary by CodeRabbit

  • New Features

    • Enhanced interactivity in the AutoComplete component with improved input tracking and selection handling.
    • Introduced a clear button that dynamically appears based on user input or selection.
  • Bug Fixes

    • Refined control flow for input handling to ensure accurate state updates.

@ANIR1604 ANIR1604 requested a review from a team as a code owner December 1, 2024 18:56
Copy link
Contributor

coderabbitai bot commented Dec 1, 2024

Walkthrough

The changes in the AutoCompleteAsync.tsx file introduce new state management for user input and selection handling. A state variable hasTyped is added to track user input, and new functions are created to manage input changes and selection updates. The logic for displaying the clear button is refined to ensure it only appears when appropriate, based on user interaction. The onChange prop is updated to enhance the component’s interactivity and control flow.

Changes

File Change Summary
src/components/Form/AutoCompleteAsync.tsx Added hasTyped state variable, implemented handleInputChange and handleSelectionChange functions, modified clear button logic, and updated onChange prop handling.

Assessment against linked issues

Objective Addressed Explanation
Clear button state shouldn't be shown when no input field is selected (9263)

Suggested labels

needs review, tested

Poem

🐇 In the meadow where bunnies play,
A clear button shines bright as day.
With typed words and choices made,
Our AutoComplete's a joyful upgrade!
Hopping along, we cheer and sway,
For better inputs are here to stay! 🌼✨


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

netlify bot commented Dec 1, 2024

Deploy Preview for care-ohc ready!

Name Link
🔨 Latest commit f24045e
🔍 Latest deploy log https://app.netlify.com/sites/care-ohc/deploys/674cb15ea1ee310008e2368c
😎 Deploy Preview https://deploy-preview-9264--care-ohc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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

🧹 Outside diff range and nitpick comments (2)
src/components/Form/AutoCompleteAsync.tsx (2)

95-99: Consider resetting hasTyped when input is cleared

The input handling looks good, but consider resetting hasTyped to false when the input becomes empty through backspace/delete to maintain consistency with the component's behavior.

 const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
   const newQuery = event.target.value;
   setQuery(newQuery);
-  setHasTyped(newQuery.length > 0); // Set hasTyped to true if input is not empty
+  setHasTyped(newQuery.length > 0); // Update hasTyped based on input presence

107-108: Consider simplifying the visibility condition

While the logic is correct, the condition could be more readable by grouping related checks.

-  const shouldShowClearButton = (hasSelection || hasTyped) && query !== ""; // Exclude placeholder when query is empty
+  const shouldShowClearButton = query !== "" && (hasSelection || hasTyped); // Show only when there's input and user interaction
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f76c625 and f24045e.

📒 Files selected for processing (1)
  • src/components/Form/AutoCompleteAsync.tsx (4 hunks)
🔇 Additional comments (4)
src/components/Form/AutoCompleteAsync.tsx (4)

67-67: LGTM: Clean state management addition

The new state variable hasTyped is well-named and appropriately initialized to track user interaction with the input field.


101-105: LGTM: Comprehensive selection handling

The selection handler properly manages all relevant states, ensuring the input field reflects the selected option while maintaining the typed state.


147-157: LGTM: Comprehensive clear button implementation

The clear button implementation properly resets all relevant states and maintains good UX with tooltips and accessibility features.


Line range hint 1-236: Verify similar patterns in other form components

Let's ensure consistency across other form components that might have similar clear button functionality.

✅ Verification successful

Clear button implementation is consistent with existing patterns

The implementation in AutoCompleteAsync.tsx follows the same clear button pattern found in other form components:

  • TextFormField.tsx uses CareIcon with l-times-circle for clearing input
  • ModelCrudEditor.tsx uses similar icon pattern for removal actions
  • The new implementation maintains consistency with these existing patterns, including:
    • Using the same icon (l-times-circle)
    • Proper positioning (absolute positioning on the right)
    • Similar interaction patterns (click to clear)
    • Consistent styling approach

The changes align well with the existing codebase patterns and maintain UI/UX consistency across form components.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for similar clear button patterns in other form components
echo "Searching for similar clear button implementations..."
rg -l "CareIcon.*times-circle" "src/components/Form"

# Search for other autocomplete implementations
echo "Checking for other autocomplete implementations..."
rg -l "Combobox.*clear.*selection" "src/components"

Length of output: 421


Script:

#!/bin/bash
# Let's examine the clear button implementations in the identified files
echo "Checking ModelCrudEditor.tsx implementation:"
rg -A 5 -B 5 "times-circle" "src/components/Form/ModelCrudEditor.tsx"

echo "\nChecking TextFormField.tsx implementation:"
rg -A 5 -B 5 "times-circle" "src/components/Form/FormFields/TextFormField.tsx"

# Let's also check for other Combobox components that might have similar patterns
echo "\nChecking for other Combobox components:"
ast-grep --pattern 'const $_ = ($_: Props) => {
  $$$
  Combobox
  $$$
}'

Length of output: 1218

@ANIR1604
Copy link
Contributor Author

ANIR1604 commented Dec 1, 2024

@Jacobjeevan I had not touched the other codes but its giving errors in Cypress Tests that are being shown in different components. And the clear button is working smoothly after making the code changes

@github-actions github-actions bot added needs-triage question Further information is requested labels Dec 1, 2024
@@ -128,14 +144,16 @@ const AutoCompleteAsync = (props: Props) => {
{!disabled && (
<ComboboxButton className="absolute inset-y-0 right-0 flex items-center pr-2">
<div className="absolute right-0 mr-2 flex items-center text-lg text-secondary-900">
{hasSelection && !loading && !required && (
{shouldShowClearButton && !loading && !required && (
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not change AutoCompleteAsync.

You will notice that issue doesn't persist across all FacilitySelect instances, but rather in ManagePatients and AssetsList; hasSelection normally takes care of this, so look into those two instances and fix those.

@Jacobjeevan Jacobjeevan added changes required and removed question Further information is requested needs-triage labels Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clear button is active when no input is selected in dropdown
2 participants