Skip to content

Commit 2429c30

Browse files
authored
Merge pull request #4783 from amottola/masked_input
MaskedInput
2 parents e54db0e + 5140b4e commit 2429c30

File tree

11 files changed

+1247
-0
lines changed

11 files changed

+1247
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Added `MaskedInput` widget https://github.com/Textualize/textual/pull/4783
1213
- Input validation for floats and integers accept embedded underscores, e.g., "1_234_567" is valid. https://github.com/Textualize/textual/pull/4784
1314

1415
### Changed
@@ -107,6 +108,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
107108

108109
- Fixed issue with Enter events causing unresponsive UI https://github.com/Textualize/textual/pull/4833
109110

111+
110112
## [0.75.0] - 2024-08-01
111113

112114
### Added
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from textual.app import App, ComposeResult
2+
from textual.widgets import Label, MaskedInput
3+
4+
5+
class MaskedInputApp(App):
6+
# (1)!
7+
CSS = """
8+
MaskedInput.-valid {
9+
border: tall $success 60%;
10+
}
11+
MaskedInput.-valid:focus {
12+
border: tall $success;
13+
}
14+
MaskedInput {
15+
margin: 1 1;
16+
}
17+
Label {
18+
margin: 1 2;
19+
}
20+
"""
21+
22+
def compose(self) -> ComposeResult:
23+
yield Label("Enter a valid credit card number.")
24+
yield MaskedInput(
25+
template="9999-9999-9999-9999;0", # (2)!
26+
)
27+
28+
29+
app = MaskedInputApp()
30+
31+
if __name__ == "__main__":
32+
app.run()

docs/widget_gallery.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ Display a markdown document.
168168
```{.textual path="docs/examples/widgets/markdown.py"}
169169
```
170170

171+
## MaskedInput
172+
173+
A control to enter input according to a template mask.
174+
175+
[MaskedInput reference](./widgets/masked_input.md){ .md-button .md-button--primary }
176+
177+
178+
```{.textual path="docs/examples/widgets/masked_input.py"}
179+
```
180+
171181
## OptionList
172182

173183
Display a vertical list of options (options may be Rich renderables).

docs/widgets/masked_input.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# MaskedInput
2+
3+
!!! tip "Added in version 0.80.0"
4+
5+
A masked input derived from `Input`, allowing to restrict user input and give visual aid via a simple template mask, which also acts as an implicit *[validator][textual.validation.Validator]*.
6+
7+
- [x] Focusable
8+
- [ ] Container
9+
10+
## Example
11+
12+
The example below shows a masked input to ease entering a credit card number.
13+
14+
=== "Output"
15+
16+
```{.textual path="docs/examples/widgets/masked_input.py"}
17+
```
18+
19+
=== "checkbox.py"
20+
21+
```python
22+
--8<-- "docs/examples/widgets/masked_input.py"
23+
```
24+
25+
## Reactive Attributes
26+
27+
| Name | Type | Default | Description |
28+
| ---------- | ----- | ------- | ------------------------- |
29+
| `template` | `str` | `""` | The template mask string. |
30+
31+
### The template string format
32+
33+
A `MaskedInput` template length defines the maximum length of the input value. Each character of the mask defines a regular expression used to restrict what the user can insert in the corresponding position, and whether the presence of the character in the user input is required for the `MaskedInput` value to be considered valid, according to the following table:
34+
35+
| Mask character | Regular expression | Required? |
36+
| -------------- | ------------------ | --------- |
37+
| `A` | `[A-Za-z]` | Yes |
38+
| `a` | `[A-Za-z]` | No |
39+
| `N` | `[A-Za-z0-9]` | Yes |
40+
| `n` | `[A-Za-z0-9]` | No |
41+
| `X` | `[^ ]` | Yes |
42+
| `x` | `[^ ]` | No |
43+
| `9` | `[0-9]` | Yes |
44+
| `0` | `[0-9]` | No |
45+
| `D` | `[1-9]` | Yes |
46+
| `d` | `[1-9]` | No |
47+
| `#` | `[0-9+\-]` | No |
48+
| `H` | `[A-Fa-f0-9]` | Yes |
49+
| `h` | `[A-Fa-f0-9]` | No |
50+
| `B` | `[0-1]` | Yes |
51+
| `b` | `[0-1]` | No |
52+
53+
There are some special characters that can be used to control automatic case conversion during user input: `>` converts all subsequent user input to uppercase; `<` to lowercase; `!` disables automatic case conversion. Any other character that appears in the template mask is assumed to be a separator, which is a character that is automatically inserted when user reaches its position. All mask characters can be escaped by placing `\` in front of them, allowing any character to be used as separator.
54+
The mask can be terminated by `;c`, where `c` is any character you want to be used as placeholder character. The `placeholder` parameter inherited by `Input` can be used to override this allowing finer grain tuning of the placeholder string.
55+
56+
## Messages
57+
58+
- [MaskedInput.Changed][textual.widgets.MaskedInput.Changed]
59+
- [MaskedInput.Submitted][textual.widgets.MaskedInput.Submitted]
60+
61+
## Bindings
62+
63+
The masked input widget defines the following bindings:
64+
65+
::: textual.widgets.MaskedInput.BINDINGS
66+
options:
67+
show_root_heading: false
68+
show_root_toc_entry: false
69+
70+
## Component Classes
71+
72+
The masked input widget provides the following component classes:
73+
74+
::: textual.widgets.MaskedInput.COMPONENT_CLASSES
75+
options:
76+
show_root_heading: false
77+
show_root_toc_entry: false
78+
79+
---
80+
81+
82+
::: textual.widgets.MaskedInput
83+
options:
84+
heading_level: 2

mkdocs-nav.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ nav:
157157
- "widgets/log.md"
158158
- "widgets/markdown_viewer.md"
159159
- "widgets/markdown.md"
160+
- "widgets/masked_input.md"
160161
- "widgets/option_list.md"
161162
- "widgets/placeholder.md"
162163
- "widgets/pretty.md"

src/textual/widgets/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from ._loading_indicator import LoadingIndicator
2929
from ._log import Log
3030
from ._markdown import Markdown, MarkdownViewer
31+
from ._masked_input import MaskedInput
3132
from ._option_list import OptionList
3233
from ._placeholder import Placeholder
3334
from ._pretty import Pretty
@@ -68,6 +69,7 @@
6869
"Log",
6970
"Markdown",
7071
"MarkdownViewer",
72+
"MaskedInput",
7173
"OptionList",
7274
"Placeholder",
7375
"Pretty",

0 commit comments

Comments
 (0)