Skip to content

Commit

Permalink
Merge pull request #4783 from amottola/masked_input
Browse files Browse the repository at this point in the history
MaskedInput
  • Loading branch information
willmcgugan authored Sep 11, 2024
2 parents e54db0e + 5140b4e commit 2429c30
Show file tree
Hide file tree
Showing 11 changed files with 1,247 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

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

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

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


## [0.75.0] - 2024-08-01

### Added
Expand Down
32 changes: 32 additions & 0 deletions docs/examples/widgets/masked_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from textual.app import App, ComposeResult
from textual.widgets import Label, MaskedInput


class MaskedInputApp(App):
# (1)!
CSS = """
MaskedInput.-valid {
border: tall $success 60%;
}
MaskedInput.-valid:focus {
border: tall $success;
}
MaskedInput {
margin: 1 1;
}
Label {
margin: 1 2;
}
"""

def compose(self) -> ComposeResult:
yield Label("Enter a valid credit card number.")
yield MaskedInput(
template="9999-9999-9999-9999;0", # (2)!
)


app = MaskedInputApp()

if __name__ == "__main__":
app.run()
10 changes: 10 additions & 0 deletions docs/widget_gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ Display a markdown document.
```{.textual path="docs/examples/widgets/markdown.py"}
```

## MaskedInput

A control to enter input according to a template mask.

[MaskedInput reference](./widgets/masked_input.md){ .md-button .md-button--primary }


```{.textual path="docs/examples/widgets/masked_input.py"}
```

## OptionList

Display a vertical list of options (options may be Rich renderables).
Expand Down
84 changes: 84 additions & 0 deletions docs/widgets/masked_input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# MaskedInput

!!! tip "Added in version 0.80.0"

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]*.

- [x] Focusable
- [ ] Container

## Example

The example below shows a masked input to ease entering a credit card number.

=== "Output"

```{.textual path="docs/examples/widgets/masked_input.py"}
```

=== "checkbox.py"

```python
--8<-- "docs/examples/widgets/masked_input.py"
```

## Reactive Attributes

| Name | Type | Default | Description |
| ---------- | ----- | ------- | ------------------------- |
| `template` | `str` | `""` | The template mask string. |

### The template string format

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:

| Mask character | Regular expression | Required? |
| -------------- | ------------------ | --------- |
| `A` | `[A-Za-z]` | Yes |
| `a` | `[A-Za-z]` | No |
| `N` | `[A-Za-z0-9]` | Yes |
| `n` | `[A-Za-z0-9]` | No |
| `X` | `[^ ]` | Yes |
| `x` | `[^ ]` | No |
| `9` | `[0-9]` | Yes |
| `0` | `[0-9]` | No |
| `D` | `[1-9]` | Yes |
| `d` | `[1-9]` | No |
| `#` | `[0-9+\-]` | No |
| `H` | `[A-Fa-f0-9]` | Yes |
| `h` | `[A-Fa-f0-9]` | No |
| `B` | `[0-1]` | Yes |
| `b` | `[0-1]` | No |

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.
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.

## Messages

- [MaskedInput.Changed][textual.widgets.MaskedInput.Changed]
- [MaskedInput.Submitted][textual.widgets.MaskedInput.Submitted]

## Bindings

The masked input widget defines the following bindings:

::: textual.widgets.MaskedInput.BINDINGS
options:
show_root_heading: false
show_root_toc_entry: false

## Component Classes

The masked input widget provides the following component classes:

::: textual.widgets.MaskedInput.COMPONENT_CLASSES
options:
show_root_heading: false
show_root_toc_entry: false

---


::: textual.widgets.MaskedInput
options:
heading_level: 2
1 change: 1 addition & 0 deletions mkdocs-nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ nav:
- "widgets/log.md"
- "widgets/markdown_viewer.md"
- "widgets/markdown.md"
- "widgets/masked_input.md"
- "widgets/option_list.md"
- "widgets/placeholder.md"
- "widgets/pretty.md"
Expand Down
2 changes: 2 additions & 0 deletions src/textual/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from ._loading_indicator import LoadingIndicator
from ._log import Log
from ._markdown import Markdown, MarkdownViewer
from ._masked_input import MaskedInput
from ._option_list import OptionList
from ._placeholder import Placeholder
from ._pretty import Pretty
Expand Down Expand Up @@ -68,6 +69,7 @@
"Log",
"Markdown",
"MarkdownViewer",
"MaskedInput",
"OptionList",
"Placeholder",
"Pretty",
Expand Down
Loading

0 comments on commit 2429c30

Please sign in to comment.