Skip to content

Commit

Permalink
Introduce Form non-visual container
Browse files Browse the repository at this point in the history
  • Loading branch information
japhb committed Jul 23, 2022
1 parent def8772 commit 7cfbb66
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 22 additions & 8 deletions examples/form.raku
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions lib/Terminal/Widgets/Form.rakumod
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 6 additions & 1 deletion lib/Terminal/Widgets/Input.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Terminal::Widgets::Widget;
use Terminal::Widgets::Utils;
use Terminal::Widgets::Form;


role Terminal::Widgets::Input
Expand All @@ -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),
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/Terminal/Widgets/Simple.rakumod
Original file line number Diff line number Diff line change
@@ -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;


Expand Down
2 changes: 2 additions & 0 deletions t/00-use.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7cfbb66

Please sign in to comment.