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

Event handlers for inputs and other widgets #68

Open
lilactown opened this issue Feb 18, 2023 · 1 comment
Open

Event handlers for inputs and other widgets #68

lilactown opened this issue Feb 18, 2023 · 1 comment

Comments

@lilactown
Copy link

When building my 7 Humble GUIs project, I ended up copying and modifying the text-field component to accept an on-change event handler in addition to the atom that would track the state, so that I could change other parts of a form when the user was typing into the input and ignore updates to the state that were caused by other things.

This made the code for the temperature converter quite simple to write:
https://github.com/lilactown/7-humble-guis/blob/a69a600a11373ca0dffe18ec5da887ee4120d45a/src/town/lilac/humble/app/gui_2.clj#L66-L84

What do you think of adopting this for the text-input/text-field and other stateful widgets?

Moving to a more event-oriented system could also allow for more customization of behavior. For example, if a user wanted to disallow anything but numbers entered into a text-field, they would again have to fork the widget and write that logic. If instead, the widget didn't mutate the atom when typing if an on-change handler was passed in, a user could encode this logic easily themselves:

(let [*num (atom {:text "23"})]
  (ui/text-field
    {:on-change #(when (Float/parseFloat %) (swap! *num assoc :text %))}
    *num))
@tonsky
Copy link
Contributor

tonsky commented Feb 21, 2023

Yes, on-change would be a great addition. I’m still not sure what to do with local state (rest of the stuff that text-input stores in the atom), but that’s another story. For now I can merge on-change if you make a PR

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

No branches or pull requests

2 participants