diff --git a/META6.json b/META6.json index a7ebeca..fd26211 100644 --- a/META6.json +++ b/META6.json @@ -16,6 +16,7 @@ "Terminal::Widgets": "lib/Terminal/Widgets.rakumod", "Terminal::Widgets::App": "lib/Terminal/Widgets/App.rakumod", "Terminal::Widgets::Events": "lib/Terminal/Widgets/Events.rakumod", + "Terminal::Widgets::Form": "lib/Terminal/Widgets/Form.rakumod", "Terminal::Widgets::Input": "lib/Terminal/Widgets/Input.rakumod", "Terminal::Widgets::Input::Boolean": "lib/Terminal/Widgets/Input/Boolean.rakumod", "Terminal::Widgets::Input::Button": "lib/Terminal/Widgets/Input/Button.rakumod", diff --git a/examples/form.raku b/examples/form.raku index a67082c..95cf5b1 100644 --- a/examples/form.raku +++ b/examples/form.raku @@ -5,16 +5,30 @@ use Terminal::Widgets::Simple; #| A top level UI container based on Terminal::Widgets::Simple::TopLevel class FormUI is TopLevel { + has Form:D $.form .= new; + method initial-layout($builder, $width, $height) { with $builder { - .checkbox( label => "It's a checkbox"), - .checkbox( label => "It's another checkbox"), - .radio-button(label => "It's a radio button", - group => 'my-radios'), - .radio-button(label => "It's a second radio button", - group => 'my-radios'), - .text-input( style => %(set-h => UInt)), - .button( label => 'quit', on-click => { $.terminal.quit }), + .checkbox( :$.form, label => "It's a checkbox"), + .checkbox( :$.form, label => "It's another checkbox"), + .radio-button(:$.form, label => "It's a radio button", + group => 'my-radios'), + .radio-button(:$.form, label => "It's a second radio button", + group => 'my-radios'), + .text-input( :$.form), + .node( + .button( :$.form, label => 'Show State', + process-input => { self.show-state }), + .button( :$.form, label => 'Quit', + process-input => { $.terminal.quit }), + ), + .node, # To soak up extra vertical space + } + } + + method show-state() { + for $.form.inputs { + .note; } } } diff --git a/lib/Terminal/Widgets/Form.rakumod b/lib/Terminal/Widgets/Form.rakumod new file mode 100644 index 0000000..121cc63 --- /dev/null +++ b/lib/Terminal/Widgets/Form.rakumod @@ -0,0 +1,9 @@ +# ABSTRACT: A (non-visual) container for form inputs + +class Terminal::Widgets::Form { + has @.inputs; + + method add-input($input) { + @!inputs.push($input); + } +} diff --git a/lib/Terminal/Widgets/Input.rakumod b/lib/Terminal/Widgets/Input.rakumod index c94475f..b003c5c 100644 --- a/lib/Terminal/Widgets/Input.rakumod +++ b/lib/Terminal/Widgets/Input.rakumod @@ -2,6 +2,7 @@ use Terminal::Widgets::Widget; use Terminal::Widgets::Utils; +use Terminal::Widgets::Form; role Terminal::Widgets::Input @@ -12,6 +13,9 @@ role Terminal::Widgets::Input has $.error; has %.color; + has Terminal::Widgets::Form $.form; + + # gist that doesn't pull in the widget grid method gist() { my @flags = ('enabled' if $!enabled), @@ -33,9 +37,10 @@ role Terminal::Widgets::Input } - # Make sure unset colors are defaulted + # Make sure unset colors are defaulted, and optionally add input to a form submethod TWEAK() { self.default-colors; + .add-input(self) with $!form; } # Set color defaults diff --git a/lib/Terminal/Widgets/Simple.rakumod b/lib/Terminal/Widgets/Simple.rakumod index 528a247..242dde5 100644 --- a/lib/Terminal/Widgets/Simple.rakumod +++ b/lib/Terminal/Widgets/Simple.rakumod @@ -1,10 +1,12 @@ # ABSTRACT: Core module to load all simplified classes/roles use Terminal::Widgets::App; +use Terminal::Widgets::Form; use Terminal::Widgets::Simple::TopLevel; # Re-export classes under shorter names +constant Form is export = Terminal::Widgets::Form; constant TopLevel is export = Terminal::Widgets::Simple::TopLevel; diff --git a/t/00-use.rakutest b/t/00-use.rakutest index 4fb0840..ac4ca27 100644 --- a/t/00-use.rakutest +++ b/t/00-use.rakutest @@ -19,6 +19,8 @@ use Terminal::Widgets::Input::Checkbox; use Terminal::Widgets::Input::RadioButton; use Terminal::Widgets::Input::Text; +use Terminal::Widgets::Form; + use Terminal::Widgets::StandardWidgetBuilder; use Terminal::Widgets::Simple::TopLevel; use Terminal::Widgets::Simple;