Add example to filter characters inputted in an input field (#763)

Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
Clément Roblot 2023-11-05 16:26:12 +07:00 committed by GitHub
parent d548a18658
commit 0631c3ab3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View File

@ -633,6 +633,26 @@ Produced by: `ftxui::Input()` from "ftxui/component/component.hpp"
<script id="asciicast-223719" src="https://asciinema.org/a/223719.js" async></script> <script id="asciicast-223719" src="https://asciinema.org/a/223719.js" async></script>
@endhtmlonly @endhtmlonly
### Filtered input
On can filter out the characters received by the input component, using
`ftxui::CatchEvent`.
```cpp
std::string phone_number;
Component input = Input(&phone_number, "phone number");
// Filter out non-digit characters.
input |= CatchEvent([&](Event event) {
return event.is_character() && !std::isdigit(event.character()[0]);
});
// Filter out characters past the 10th one.
input |= CatchEvent([&](Event event) {
return event.is_character() && phone_number.size() >= 10;
});
```
## Menu {#component-menu} ## Menu {#component-menu}
Defines a menu object. It contains a list of entries, one of them is selected. Defines a menu object. It contains a list of entries, one of them is selected.

View File

@ -15,30 +15,50 @@
int main() { int main() {
using namespace ftxui; using namespace ftxui;
// The data:
std::string first_name; std::string first_name;
std::string last_name; std::string last_name;
std::string password; std::string password;
std::string phoneNumber;
// The basic input components:
Component input_first_name = Input(&first_name, "first name"); Component input_first_name = Input(&first_name, "first name");
Component input_last_name = Input(&last_name, "last name"); Component input_last_name = Input(&last_name, "last name");
// The password input component:
InputOption password_option; InputOption password_option;
password_option.password = true; password_option.password = true;
Component input_password = Input(&password, "password", password_option); Component input_password = Input(&password, "password", password_option);
// The phone number input component:
// We are using `CatchEvent` to filter out non-digit characters.
Component input_phone_number = Input(&phoneNumber, "phone number");
input_phone_number |= CatchEvent([&](Event event) {
return event.is_character() && !std::isdigit(event.character()[0]);
});
input_phone_number |= CatchEvent([&](Event event) {
return event.is_character() && phoneNumber.size() > 10;
});
// The component tree:
auto component = Container::Vertical({ auto component = Container::Vertical({
input_first_name, input_first_name,
input_last_name, input_last_name,
input_password, input_password,
input_phone_number,
}); });
// Tweak how the component tree is rendered:
auto renderer = Renderer(component, [&] { auto renderer = Renderer(component, [&] {
return vbox({ return vbox({
text("Hello " + first_name + " " + last_name),
separator(),
hbox(text(" First name : "), input_first_name->Render()), hbox(text(" First name : "), input_first_name->Render()),
hbox(text(" Last name : "), input_last_name->Render()), hbox(text(" Last name : "), input_last_name->Render()),
hbox(text(" Password : "), input_password->Render()), hbox(text(" Password : "), input_password->Render()),
hbox(text(" Phone num : "), input_phone_number->Render()),
separator(),
text("Hello " + first_name + " " + last_name),
text("Your password is " + password),
text("Your phone number is " + phoneNumber),
}) | }) |
border; border;
}); });