Skip to content

Conversation

@ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Mar 3, 2025

Resolves #5015

Test code

import flet as ft


class Test(ft.TextField):
    def __init__(self, page: ft.Page):
        super().__init__()
        self.page = page
        self.len_date = 0
        self.on_change = self.scadenza

    def scadenza(self, e):
        text = e.control.value
        if len(text) > self.len_date:
            if len(text) == 2:
                e.control.value = text + "-"
            if len(text) == 5:
                e.control.value = text + "-"
            if len(text) > 10:
                e.control.value = text[0:10]
        else:
            if len(text) == 5:
                e.control.value = text[0:4]
            if len(text) == 2:
                e.control.value = text[0:1]

        self.len_date = len(e.control.value)
        self.page.update()


def main(page: ft.Page):
    page.theme_mode = ft.ThemeMode.LIGHT
    test = Test(page)
    page.add(test)


ft.app(target=main)

Summary by Sourcery

Bug Fixes:

  • Fixes an issue where the cursor position in a Textfield would change when the field content was modified in the on_change event.

Copy link

@HappyCoder256 HappyCoder256 left a comment

Choose a reason for hiding this comment

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

Clean fix.

@FeodorFitsner FeodorFitsner merged commit 33c9d62 into main Mar 6, 2025
3 checks passed
@FeodorFitsner FeodorFitsner deleted the ndonkoHenri/fix-textfield-cursor branch March 6, 2025 17:44
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.

TextField: Auto-selection of text after modifying value in on_change (Regression in 0.26.0)

4 participants