Skip to content

Commit

Permalink
preserve integer representation in ui.number (fixes #2454)
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Jan 29, 2024
1 parent d6ac451 commit 8d91ca3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nicegui/elements/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def _value_to_model_value(self, value: Any) -> Any:
if value is None:
return None
if self.format is None:
old_value = float(self._props.get(self.VALUE_PROP) or 0)
if old_value == int(old_value) and value == int(value):
return str(int(value)) # preserve integer representation
return str(value)
if value == '':
return 0
Expand Down
21 changes: 21 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from nicegui import ui
from nicegui.testing import Screen
Expand Down Expand Up @@ -86,3 +87,23 @@ def test_rounding(precision: int, screen: Screen):
screen.should_contain('number=_12.3_')
elif precision == -1:
screen.should_contain('number=_10.0_')


def test_int_float_conversion_on_error1(screen: Screen):
ui.number('Number', validation={'Error': lambda value: value == 1}, value=1)

screen.open('/')
element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Number"]')
element.send_keys('2')
screen.should_contain('Error')
assert element.get_attribute('value') == '12'


def test_int_float_conversion_on_error2(screen: Screen):
ui.number('Number', validation={'Error': lambda value: value == 1.02}, value=1.02)

screen.open('/')
element = screen.selenium.find_element(By.XPATH, '//*[@aria-label="Number"]')
element.send_keys(Keys.BACKSPACE)
screen.should_contain('Error')
assert element.get_attribute('value') == '1.0'

0 comments on commit 8d91ca3

Please sign in to comment.