Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Input(ScrollView):
| ctrl+k | Delete everything to the right of the cursor. |
| ctrl+x | Cut selected text. |
| ctrl+c | Copy selected text. |
| ctrl+v | Paste text from the clipboard. |
| ctrl+v | Paste text from the clipboard. |
"""

COMPONENT_CLASSES: ClassVar[set[str]] = {
Expand Down Expand Up @@ -187,9 +187,9 @@ class Input(ScrollView):
}

&:focus {
border: tall $border;
border: tall $border;
background-tint: $foreground 5%;

}
&>.input--cursor {
background: $input-cursor-background;
Expand All @@ -207,12 +207,12 @@ class Input(ScrollView):
}
&.-invalid:focus {
border: tall $error;
}
}

&:ansi {
background: ansi_default;
color: ansi_default;
&>.input--cursor {
&>.input--cursor {
text-style: reverse;
}
&>.input--placeholder, &>.input--suggestion {
Expand All @@ -224,8 +224,8 @@ class Input(ScrollView):
}
&.-invalid:focus {
border: tall ansi_red;
}
}

}
}

Expand Down Expand Up @@ -571,13 +571,14 @@ def set_classes() -> None:
self.set_class(not valid, "-invalid")
self.set_class(valid, "-valid")

# If no validators are supplied, and therefore no validation occurs, we return None.
if not self.validators:
# Empty inputs are always OK if valid_empty is set.
if self.valid_empty and not value:
self._valid = True
set_classes()
return None

if self.valid_empty and not value:
# If no validators are supplied, and therefore no validation occurs, we return None.
if not self.validators:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since we here have two conditions with the same action, would it overcomplicate the code to elide them? At present I suspect the sequence improves readability.

self._valid = True
set_classes()
return None
Expand Down
5 changes: 3 additions & 2 deletions tests/input/test_input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,15 @@ async def test_none_validate_on_means_all_validations_happen():
async def test_valid_empty():
app = InputApp(None)
async with app.run_test() as pilot:
input = app.query_one(Input)
input = app.query_one(Input) # valid_empty=False

await pilot.press("1", "backspace")
assert input.value == '' # Empty value: invalid

assert not input.has_class("-valid")
assert input.has_class("-invalid")

input.valid_empty = True

assert input.has_class("-valid")
assert input.has_class("-valid") # Empty value: now acceptble
assert not input.has_class("-invalid")
Loading