1 package Shiar_Sheet::FormRow;
5 use PLP::Functions 'EscapeHTML';
10 my ($row, $col, $attr) = @_;
11 my $val = $row->{$col} // '';
13 $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
15 if (my $options = $attr->{-select}) {
16 $options = $options->(@_) if ref $options eq 'CODE';
17 $options->{$val} //= "unknown ($val)"; # preserve current
19 sprintf('<select id="%s" name="%1$s">', $col),
20 (map { sprintf('<option value="%s"%s>%s</option>',
21 $_, $val eq $_ && ' selected', $options->{$_}
22 ) } sort keys %{$options}),
26 elsif ($attr->{type} eq 'textarea') {
29 sprintf('<label for="%s">%s</label>', $col, $_)
30 } $attr->{-label} // ()),
31 sprintf('<textarea id="%s" name="%1$s"%s>%s</textarea>',
32 $col, $html, EscapeHTML($val)
36 elsif ($attr->{type} eq 'checkbox') {
37 $html .= ' checked' if $val;
41 '<input name="%1$s" value="0" type="hidden" />',
42 '<input id="%s" name="%1$s" value="1"%s>',
44 ), $col, $html, $attr->{-label}
48 my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple};
51 sprintf('<label for="%s">%s</label>', $col, $_)
52 } $attr->{-label} // ()),
53 $multiple ? '<span class="inline multiinput">' : (),
55 sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
56 } ref $val eq 'ARRAY' ? @{$val} : ()),
57 sprintf('<input id="%s" name="%1$s" value="%s"%s />',
58 $col, $multiple ? '' : EscapeHTML($val), $html
60 $multiple ? '</span>' : (),