index: release v1.18 with only altgr index linked
[sheet.git] / Shiar_Sheet / FormRow.pm
1 package Shiar_Sheet::FormRow;
2
3 use 5.014;
4 use warnings;
5 use PLP::Functions 'EscapeHTML';
6
7 our $VERSION = '1.00';
8
9 sub input {
10         my ($row, $col, $attr) = @_;
11         my $val = $row->{$col} // '';
12         my $html = '';
13         $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
14
15         if (my $options = $attr->{-select}) {
16                 $options = $options->(@_) if ref $options eq 'CODE';
17                 $options->{$val} //= "unknown ($val)";  # preserve current
18                 return (
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}),
23                         '</select>',
24                 );
25         }
26         elsif ($attr->{type} eq 'textarea') {
27                 return (
28                         (map {
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)
33                         ),
34                 );
35         }
36         elsif ($attr->{type} eq 'checkbox') {
37                 $html .= ' checked' if $val;
38                 return sprintf(
39                         join('',
40                                 '<label>',
41                                 '<input name="%1$s" value="0" type="hidden" />',
42                                 '<input id="%s" name="%1$s" value="1"%s>',
43                                 ' %s</label>',
44                         ), $col, $html, $attr->{-label}
45                 );
46         }
47         else {
48                 my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple};
49                 return (
50                         (map {
51                                 sprintf('<label for="%s">%s</label>', $col, $_)
52                         } $attr->{-label} // ()),
53                         $multiple ? '<span class="inline multiinput">' : (),
54                         (map {
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
59                         ),
60                         $multiple ? '</span>' : (),
61                 );
62         }
63 }
64
65 1;