Dropdown: Fix title not updated.

A bug was introduced by:
https://github.com/ArthurSonzogni/FTXUI/pull/826

The checkbox label wasn't updated.

Bug:https://github.com/ArthurSonzogni/FTXUI/issues/861
This commit is contained in:
ArthurSonzogni 2024-05-13 10:51:57 +02:00
parent 4913379625
commit af49b57e60
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C

View File

@ -46,25 +46,35 @@ Component Dropdown(DropdownOption option) {
Element Render() override { Element Render() override {
radiobox.selected = radiobox.selected =
util::clamp(radiobox.selected(), 0, int(radiobox.entries.size()) - 1); util::clamp(radiobox.selected(), 0, int(radiobox.entries.size()) - 1);
checkbox.label = title_ = radiobox.entries[selected_()];
radiobox.entries[static_cast<size_t>(radiobox.selected())];
return transform(*open_, checkbox_->Render(), radiobox_->Render()); return transform(*open_, checkbox_->Render(), radiobox_->Render());
} }
// Switch focus in between the checkbox and the radiobox when selecting it. // Switch focus in between the checkbox and the radiobox when selecting it.
bool OnEvent(ftxui::Event event) override { bool OnEvent(ftxui::Event event) override {
const bool show_old = open_(); const bool open_old = open_();
const int selected_old = selected_(); const int selected_old = selected_();
const bool handled = ComponentBase::OnEvent(event); bool handled = ComponentBase::OnEvent(event);
if (!show_old && open_()) { // Transfer focus to the radiobox when the dropdown is opened.
if (!open_old && open_()) {
radiobox_->TakeFocus(); radiobox_->TakeFocus();
} }
if (selected_old != selected_()) { // Auto-close the dropdown when the user selects an item, even if the item
checkbox_->TakeFocus(); // it the same as the previous one.
open_ = false; if (open_old && open_()) {
const bool should_close = (selected_() != selected_old) || //
(event == Event::Return) || //
(event == Event::Character(' ')) || //
(event == Event::Escape); //
if (should_close) {
checkbox_->TakeFocus();
open_ = false;
handled = true;
}
} }
return handled; return handled;
@ -75,6 +85,7 @@ Component Dropdown(DropdownOption option) {
selected_ = radiobox.selected; selected_ = radiobox.selected;
checkbox.checked = &*open_; checkbox.checked = &*open_;
radiobox.selected = &*selected_; radiobox.selected = &*selected_;
checkbox.label = &title_;
if (!checkbox.transform) { if (!checkbox.transform) {
checkbox.transform = [](const EntryState& s) { checkbox.transform = [](const EntryState& s) {
@ -113,6 +124,7 @@ Component Dropdown(DropdownOption option) {
Ref<int> selected_; Ref<int> selected_;
Component checkbox_; Component checkbox_;
Component radiobox_; Component radiobox_;
std::string title_;
}; };
return Make<Impl>(option); return Make<Impl>(option);