Skip to content

Update the displayed value when the format of ui.number changes - #6553

Open
falkoschindler wants to merge 4 commits into
mainfrom
number-format-property
Open

falkoschindler wants to merge 4 commits into
mainfrom
number-format-property

Conversation

@falkoschindler

@falkoschindler falkoschindler commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

ui.number's format was a plain attribute, unlike min, max, precision, prefix and suffix, which are properties that update the element when set. Assigning number.format = '%.1f' at runtime therefore changed nothing in the browser until the next value change or sanitize(). Found while reviewing #6550, which made format display-only; this is the counterpart on the other side: changing the format only touches the display.

Implementation

format becomes a property. Its setter stores the new format and re-derives the model-value prop through _value_to_model_value, so the browser shows the new formatting right away. Like min and max, it returns early when the same format is assigned again. It does not call sanitize(): since #6550, the format has no effect on the value, so there is nothing to clamp or round.

Elements without loopback remember the text the client typed and ask the client to keep it whenever a serialized model-value equals it. That memory was only cleared in _handle_value_change, so a setter that rewrites the prop directly left it stale, and switching the format twice (e.g. back to the original one) made the browser keep the previous formatting. ValueElement._to_dict now clears the memory whenever it does not preserve the prop, since the client is about to show the new value either way; this covers every path that writes the prop. A new test_changing_format types a value, changes the format on its own and switches it back to None.

Progress

  • The PR title is a short phrase starting with a verb like "Add ...", "Fix ...", "Update ...", "Remove ...", etc.
  • The implementation is complete.
  • This PR does not address a security issue.
  • Pytests: added/updated
  • Documentation: added/updated
  • Public API: no breaking changes

※

`format` was a plain attribute, so `number.format = '%.1f'` at runtime changed nothing in the browser until the next value change or `sanitize()`. It is now a property whose setter re-derives the displayed value, like `prefix` and `suffix`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgKVNb2aAsju6ryUWTWzky
@falkoschindler falkoschindler added this to the 4.2 milestone Dec 1, 2026
@falkoschindler falkoschindler added the feature Type/scope: New or intentionally changed behavior label Dec 1, 2026
@falkoschindler falkoschindler self-assigned this Dec 1, 2026
falkoschindler and others added 2 commits December 1, 2026 23:47
With LOOPBACK disabled, ValueElement remembers the text the client typed
and keeps it on the client whenever a serialized model-value equals it.
That memory was only cleared in _handle_value_change, so a setter that
rewrites the prop directly, like the new format setter, left it stale:
switching the format twice (e.g. back to the original one) made the
browser keep the previous formatting.

_to_dict now clears the memory whenever it does not preserve the prop,
because the client is about to show the new value either way. This
covers every path that writes the prop, so _handle_value_change no
longer needs its own reset. test_prefix_and_suffix is back to its
original form, and a new test_changing_format types a value, changes
the format on its own and switches it back to None.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgKVNbGnC9XfoAR9gu5RP6
Like the min and max setters, the format setter now returns early when
the same format is assigned again. Before, every assignment rewrote
model-value and enqueued an update, and with a format active that update
replaces the text the client is typing.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgKVNZr5cJy66ZNxJJ22Qf
Both sides appended a test to tests/test_number.py; the merge keeps
test_format_does_not_round_the_value_below_the_minimum from #6550
followed by test_changing_format.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgKVNZ3csUJiybGywqbjcv
@falkoschindler
falkoschindler requested a review from evnchn December 2, 2026 15:14
@falkoschindler falkoschindler added the review Status: PR is open and needs review label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Type/scope: New or intentionally changed behavior review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants