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

Add focus, onFocus, onBlur to SearchBar (#3417) #3752

Conversation

chaotic-dev
Copy link
Contributor

@chaotic-dev chaotic-dev commented Aug 1, 2024

Description

Added focus method and the onFocus and onBlur events to the SearchBar control based on those from the TextField control

Fixes #3417

Test Code

# Test code for the review of this PR
import flet as ft

def main(page: ft.Page):
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    def close_anchor(e):
        text = f"Color {e.control.data}"
        anchor.value = text
        anchor.focus()
        anchor.update()
        # print(f"closing view from {text}")
        # anchor.close_view(text)

    anchor = ft.SearchBar(
        view_elevation=4,
        divider_color=ft.colors.AMBER,
        bar_hint_text="Search colors...",
        view_hint_text="Choose a color from the suggestions...",
        controls=[
            ft.ListTile(title=ft.Text(f"Color {i}"), on_click=close_anchor, data=i)
            for i in range(10)
        ],
    )

    page.add(
        ft.Row(
            alignment=ft.MainAxisAlignment.CENTER,
            controls=[
                ft.OutlinedButton(
                    "Open Search View",
                    on_click=lambda _: anchor.open_view(),
                ),
            ],
        ),
        anchor,
    )


ft.app(main)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I signed the CLA.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing tests pass locally with my changes
  • I have made corresponding changes to the documentation (if applicable)

Screenshots (if applicable):

Additional details

Summary by Sourcery

This pull request adds new functionality to the SearchBar control by introducing a focus method and onFocus and onBlur events. These changes enhance the interactivity and usability of the SearchBar component.

  • New Features:
    • Added focus method to the SearchBar control.
    • Introduced onFocus and onBlur events to the SearchBar control.

@CLAassistant
Copy link

CLAassistant commented Aug 1, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

sourcery-ai bot commented Aug 1, 2024

Reviewer's Guide by Sourcery

This pull request introduces focus management to the SearchBar control by adding a focus method and onFocus and onBlur event handlers. The changes include updates to the Python and Dart codebases to support these new features, ensuring that the SearchBar can now handle focus events and trigger corresponding actions.

File-Level Changes

Files Changes
sdk/python/packages/flet-core/src/flet_core/search_bar.py
packages/flet/lib/src/controls/search_anchor.dart
Added focus management and event handling (onFocus, onBlur) to the SearchBar control.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @chaotic-dev - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding brief documentation for the new focus-related methods and properties in the Python code to help users understand how to use these new capabilities.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@@ -173,6 +177,10 @@ def _get_children(self):
return children

# Public methods
def focus(self):
self._set_attr_json("focus", str(time.time()))
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Use a more unique identifier for focus events

Using the current time as an identifier could potentially lead to issues if multiple focus events occur within the same second. Consider using a more unique identifier, such as a UUID or a monotonically increasing counter.

Suggested change
self._set_attr_json("focus", str(time.time()))
import uuid
self._set_attr_json("focus", str(uuid.uuid4()))

@@ -33,18 +33,33 @@ class SearchAnchorControl extends StatefulWidget {

class _SearchAnchorControlState extends State<SearchAnchorControl> {
late final SearchController _controller;
bool _focused = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Unused state variable _focused

The _focused state variable is set but never used in the current implementation. Is it intended for future use, or can it be removed to keep the code clean?

setState(() {
_focused = _focusNode.hasFocus;
});
widget.backend.triggerControlEvent(
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider adding error handling for focus/blur events

While focus/blur operations are unlikely to fail, it's good practice to handle potential errors. Consider adding try-catch blocks or at least documenting any possible exceptions that might occur during these operations.

Suggested change
widget.backend.triggerControlEvent(
try {
widget.backend.triggerControlEvent(
} catch (e) {
// Handle the error or log it
}

@FeodorFitsner FeodorFitsner merged commit 55ea817 into flet-dev:main Aug 5, 2024
2 checks passed
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.

Unable to focus the text field of a SearchBar programatically
3 participants