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
17 changes: 8 additions & 9 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 @@ -417,8 +417,7 @@ def __init__(
```
"""
self._reactive_valid_empty = valid_empty
self._valid = True

self._valid = not (input and not valid_empty)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find the negation requires a bit of mental parsing. Maybe self._valid = valid_empty and not input ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More importantly, I presume you meant value rather than input here?

@holdenweb holdenweb Jun 24, 2025

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.

I take your point. However, I feel we really should use an equivalent expression :)

>>> for valid_empty in (True, False):
...   for input in (True, False):
...     print(f"{valid_empty=} {input=} {(not (input and not valid_empty), valid_empty and not input)}")
...
valid_empty=True input=True (True, False)
valid_empty=True input=False (True, True)
valid_empty=False input=True (False, False)
valid_empty=False input=False (True, False)

I'll use valid_empty or not input. de Morgan's law FTW!

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.

Clearly this PR still needs a bit of work ...

@TomJGooding TomJGooding Jun 24, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry you've lost me. My point is that input is just an in-built Python function and has no relation to the Input in Textual.

self.restrict = restrict
if type not in _RESTRICT_TYPES:
raise ValueError(
Expand Down
20 changes: 19 additions & 1 deletion tests/input/test_input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ async def test_none_validate_on_means_all_validations_happen():
await pilot.pause()
assert input.has_class("-valid")


async def test_valid_empty():
app = InputApp(None)
async with app.run_test() as pilot:
Expand All @@ -222,3 +221,22 @@ async def test_valid_empty():

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


class InvalidEmptyApp(App):

def compose(self) -> ComposeResult:
yield Input(valid_empty=False, value="x")

async def test_invalid_empty():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You'll need an async with here.

app = InvalidEmptyApp()
with app.run_test() as pilot:
input = app.query_one(Input)

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

await pilot.press('backspace')
assert input.has_class('-invalid')
assert not input.has_class('-valid')

Loading