mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-29 16:39:34 +08:00
Clamp selected_ on list resize for Radiobox/Menu/Toggle (#298)
fix: https://github.com/ArthurSonzogni/FTXUI/issues/296#issue-1092343846 When the list in Radiobox/Menu/Toggle is resized, clamp the |selected_| values so that it stays within bounds. Clamping is executed in Render() and in OnEvent() Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -4,10 +4,12 @@
|
||||
#include <string> // for string, basic_string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
||||
#include "ftxui/component/component.hpp" // for Radiobox
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
||||
#include "ftxui/component/component.hpp" // for Radiobox
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/component/component_options.hpp" // for RadioboxOption
|
||||
#include "ftxui/component/event.hpp" // for Event, Event::Return, Event::ArrowDown, Event::ArrowUp, Event::Tab, Event::TabReverse
|
||||
#include "ftxui/util/ref.hpp" // for Ref
|
||||
#include "gtest/gtest_pred_impl.h" // for EXPECT_EQ, Test, TEST
|
||||
|
||||
using namespace ftxui;
|
||||
@@ -114,6 +116,35 @@ TEST(RadioboxTest, Navigation) {
|
||||
radiobox->OnEvent(Event::Return);
|
||||
}
|
||||
|
||||
TEST(RadioboxTest, RemoveEntries) {
|
||||
int focused_entry = 0;
|
||||
int selected = 0;
|
||||
std::vector<std::string> entries = {"1", "2", "3"};
|
||||
RadioboxOption option;
|
||||
option.focused_entry = &focused_entry;
|
||||
auto radiobox = Radiobox(&entries, &selected, option);
|
||||
|
||||
EXPECT_EQ(selected, 0);
|
||||
EXPECT_EQ(focused_entry, 0);
|
||||
|
||||
radiobox->OnEvent(Event::ArrowDown);
|
||||
radiobox->OnEvent(Event::ArrowDown);
|
||||
radiobox->OnEvent(Event::Return);
|
||||
|
||||
EXPECT_EQ(selected, 2);
|
||||
EXPECT_EQ(focused_entry, 2);
|
||||
|
||||
entries.resize(2);
|
||||
|
||||
EXPECT_EQ(selected, 2);
|
||||
EXPECT_EQ(focused_entry, 2);
|
||||
|
||||
(void)radiobox->Render();
|
||||
|
||||
EXPECT_EQ(selected, 1);
|
||||
EXPECT_EQ(focused_entry, 1);
|
||||
}
|
||||
|
||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
||||
|
Reference in New Issue
Block a user