mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-16 16:08:08 +08:00
Add support for password for input element. (#158)
This fixes: https://github.com/ArthurSonzogni/FTXUI/issues/139 CC:@Creapermann
This commit is contained in:
@@ -30,13 +30,18 @@ class InputBase : public ComponentBase {
|
||||
|
||||
// Component implementation:
|
||||
Element Render() override {
|
||||
std::wstring password_content;
|
||||
if (option_->password)
|
||||
password_content = std::wstring(content_->size(), U'•');
|
||||
std::wstring& content = option_->password ? password_content : *content_;
|
||||
|
||||
cursor_position() =
|
||||
std::max(0, std::min<int>(content_->size(), cursor_position()));
|
||||
std::max(0, std::min<int>(content.size(), cursor_position()));
|
||||
auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
|
||||
bool is_focused = Focused();
|
||||
|
||||
// placeholder.
|
||||
if (content_->size() == 0) {
|
||||
if (content.size() == 0) {
|
||||
if (is_focused)
|
||||
return text(*placeholder_) | focus | dim | inverted | main_decorator |
|
||||
reflect(input_box_);
|
||||
@@ -46,15 +51,15 @@ class InputBase : public ComponentBase {
|
||||
|
||||
// Not focused.
|
||||
if (!is_focused)
|
||||
return text(*content_) | main_decorator | reflect(input_box_);
|
||||
return text(content) | main_decorator | reflect(input_box_);
|
||||
|
||||
std::wstring part_before_cursor = content_->substr(0, cursor_position());
|
||||
std::wstring part_at_cursor = cursor_position() < (int)content_->size()
|
||||
? content_->substr(cursor_position(), 1)
|
||||
std::wstring part_before_cursor = content.substr(0, cursor_position());
|
||||
std::wstring part_at_cursor = cursor_position() < (int)content.size()
|
||||
? content.substr(cursor_position(), 1)
|
||||
: L" ";
|
||||
std::wstring part_after_cursor =
|
||||
cursor_position() < (int)content_->size() - 1
|
||||
? content_->substr(cursor_position() + 1)
|
||||
cursor_position() < (int)content.size() - 1
|
||||
? content.substr(cursor_position() + 1)
|
||||
: L"";
|
||||
auto focused = is_focused ? focus : select;
|
||||
|
||||
|
@@ -35,6 +35,34 @@ TEST(InputTest, Type) {
|
||||
input->OnEvent(Event::Character('b'));
|
||||
EXPECT_EQ(content, L"ab");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
|
||||
auto document = input->Render();
|
||||
auto screen = Screen::Create(Dimension::Fit(document));
|
||||
Render(screen, document);
|
||||
EXPECT_EQ(screen.PixelAt(0, 0).character, L"a");
|
||||
EXPECT_EQ(screen.PixelAt(1, 0).character, L"b");
|
||||
}
|
||||
|
||||
TEST(InputTest, TypePassword) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
auto option = InputOption();
|
||||
option.password = true;
|
||||
Component input = Input(&content, &placeholder, &option);
|
||||
|
||||
input->OnEvent(Event::Character('a'));
|
||||
EXPECT_EQ(content, L"a");
|
||||
EXPECT_EQ(option.cursor_position(), 1u);
|
||||
|
||||
input->OnEvent(Event::Character('b'));
|
||||
EXPECT_EQ(content, L"ab");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
|
||||
auto document = input->Render();
|
||||
auto screen = Screen::Create(Dimension::Fit(document));
|
||||
Render(screen, document);
|
||||
EXPECT_EQ(screen.PixelAt(0, 0).character, L"•");
|
||||
EXPECT_EQ(screen.PixelAt(1, 0).character, L"•");
|
||||
}
|
||||
|
||||
TEST(InputTest, Arrow) {
|
||||
|
Reference in New Issue
Block a user