X-Git-Url: http://git.shiar.net/sheet.git/blobdiff_plain/4bd2308ef29a34a94eca8f71e924a868b9b7bb19..e26741ab236cbfaa89aa82d121c224137801a63b:/Shiar_Sheet/FormRow.pm?ds=sidebyside
diff --git a/Shiar_Sheet/FormRow.pm b/Shiar_Sheet/FormRow.pm
new file mode 100644
index 0000000..03f1c22
--- /dev/null
+++ b/Shiar_Sheet/FormRow.pm
@@ -0,0 +1,65 @@
+package Shiar_Sheet::FormRow;
+
+use 5.014;
+use warnings;
+use PLP::Functions 'EscapeHTML';
+
+our $VERSION = '1.00';
+
+sub input {
+ my ($row, $col, $attr) = @_;
+ my $val = $row->{$col} // '';
+ my $html = '';
+ $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
+
+ if (my $options = $attr->{-select}) {
+ $options = $options->(@_) if ref $options eq 'CODE';
+ $options->{$val} //= "unknown ($val)"; # preserve current
+ return (
+ sprintf('',
+ );
+ }
+ elsif ($attr->{type} eq 'textarea') {
+ return (
+ (map {
+ sprintf('', $col, $_)
+ } $attr->{-label} // ()),
+ sprintf('',
+ $col, $html, EscapeHTML($val)
+ ),
+ );
+ }
+ elsif ($attr->{type} eq 'checkbox') {
+ $html .= ' checked' if $val;
+ return sprintf(
+ join('',
+ '',
+ ), $col, $html, $attr->{-label}
+ );
+ }
+ else {
+ my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple};
+ return (
+ (map {
+ sprintf('', $col, $_)
+ } $attr->{-label} // ()),
+ $multiple ? '' : (),
+ (map {
+ sprintf('', $col, EscapeHTML($_))
+ } ref $val eq 'ARRAY' ? @{$val} : ()),
+ sprintf('',
+ $col, $multiple ? '' : EscapeHTML($val), $html
+ ),
+ $multiple ? '' : (),
+ );
+ }
+}
+
+1;