package Shiar_Sheet::FormRow {
sub input {
- my ($row, $col, $type, $label) = @_;
+ my ($row, $col, $attr) = @_;
my $val = $row->{$col} // '';
$val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
- if (ref $type eq 'ARRAY') {
+ my $html = '';
+ $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
+
+ if (my $options = $attr->{-select}) {
return (
sprintf('<select id="%s" name="%1$s">', $col),
(map { sprintf('<option value="%s"%s>%s</option>',
- $_, $val eq $_ && ' selected', $type->[$_]
- ) } 0 .. $#{$type}),
+ $_, $val eq $_ && ' selected', $options->[$_]
+ ) } 0 .. $#{$options}),
'</select>',
);
}
- elsif ($type eq 'checkbox') {
+ elsif ($attr->{type} eq 'checkbox') {
+ $html .= ' checked' if $val;
return sprintf(
join('',
'<label>',
- '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
- '<input id="%s" name="%1$s" value="1" type="%s"%s>',
+ '<input name="%1$s" value="0" type="hidden" />',
+ '<input id="%s" name="%1$s" value="1"%s>',
' %s</label>',
- ), $col, $type, !!$val && ' checked', $label
+ ), $col, $html, $attr->{-label}
);
}
else {
- my $html = '';
- $html .= qq( $_="$type->{$_}") for sort keys %{$type // {}};
return (
- $label ? sprintf('<label for="%s">%s</label>', $col, $label) : (),
+ (map {
+ sprintf('<label for="%s">%s</label>', $col, $_)
+ } $attr->{-label} // ()),
sprintf('<input id="%s" name="%1$s" value="%s"%s />',
$col, PLP::Functions::EscapeHTML($val), $html
),
printf '<li><label for="%s">%s</label><p>', $col, $title;
printf '<span class=inline>';
if ($col eq 'prio') {
- print $row->input($col => \@prioenum);
- print $row->input(cover => 'checkbox', 'Highlighted');
- print $row->input(grade => {type => 'number'}, 'Order');
+ print $row->input($col => {-select => \@prioenum});
+ print $row->input(cover => {type => 'checkbox', -label => 'Highlighted'});
+ print $row->input(grade => {type => 'number', -label => 'Order'});
}
else {
print $row->input($col);
- print $row->input(ref => {}, 'Reference') if $col eq 'cat';
+ print $row->input(ref => {-label => 'Reference'}) if $col eq 'cat';
}
print '</span>';
say '</p></li>';