1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
import { Fragment, VNode, h } from "preact";
import { useEffect, useState } from "preact/hooks";
import { FormProvider, InputArrayFieldState } from "./FormProvider.js";
import { LabelWithTooltipMaybeRequired, UIFormProps } from "./InputLine.js";
import { RenderAllFieldsByUiConfig, UIFormField } from "./forms.js";
import { useField } from "./useField.js";
import { TranslatedString } from "@gnu-taler/taler-util";
function Option({
label,
disabled,
isFirst,
isLast,
isSelected,
onClick,
}: {
label: TranslatedString;
isFirst?: boolean;
isLast?: boolean;
isSelected?: boolean;
disabled?: boolean;
onClick: () => void;
}): VNode {
let clazz = "relative flex border p-4 focus:outline-none disabled:text-grey";
if (isFirst) {
clazz += " rounded-tl-md rounded-tr-md ";
}
if (isLast) {
clazz += " rounded-bl-md rounded-br-md ";
}
if (isSelected) {
clazz += " z-10 border-indigo-200 bg-indigo-50 ";
} else {
clazz += " border-gray-200";
}
if (disabled) {
clazz +=
" cursor-not-allowed bg-gray-50 text-gray-500 ring-gray-200 text-gray";
} else {
clazz += " cursor-pointer";
}
return (
<label class={clazz}>
<input
type="radio"
name="privacy-setting"
checked={isSelected}
disabled={disabled}
onClick={onClick}
class="mt-0.5 h-4 w-4 shrink-0 text-indigo-600 disabled:cursor-not-allowed disabled:bg-gray-50 disabled:text-gray-500 disabled:ring-gray-200 focus:ring-indigo-600"
aria-labelledby="privacy-setting-0-label"
aria-describedby="privacy-setting-0-description"
/>
<span class="ml-3 flex flex-col">
<span
id="privacy-setting-0-label"
disabled
class="block text-sm font-medium"
>
{label}
</span>
{/* <!-- Checked: "text-indigo-700", Not Checked: "text-gray-500" --> */}
{/* <span
id="privacy-setting-0-description"
class="block text-sm"
>
This project would be available to anyone who has the link
</span> */}
</span>
</label>
);
}
export function InputArray(
props: {
fields: UIFormField[];
labelField: string;
} & UIFormProps<Array<{}>>,
): VNode {
const { fields, labelField, name, label, required, tooltip } = props;
const { value, onChange, state } = useField<{ [s: string]: Array<any> }>(
name,
);
const list = value ?? [];
const [selectedIndex, setSelected] = useState<number | undefined>(undefined);
const selected =
selectedIndex === undefined ? undefined : list[selectedIndex];
return (
<div class="sm:col-span-6">
<LabelWithTooltipMaybeRequired
label={label}
required={required}
tooltip={tooltip}
/>
<div class="-space-y-px rounded-md bg-white ">
{list.map((v, idx) => {
return (
<Option
label={v[labelField]}
isSelected={selectedIndex === idx}
isLast={idx === list.length - 1}
disabled={selectedIndex !== undefined && selectedIndex !== idx}
isFirst={idx === 0}
onClick={() => {
setSelected(selectedIndex === idx ? undefined : idx);
}}
/>
);
})}
<div class="pt-2">
<Option
label={"Add..." as TranslatedString}
isSelected={selectedIndex === list.length}
isLast
isFirst
disabled={
selectedIndex !== undefined && selectedIndex !== list.length
}
onClick={() => {
setSelected(
selectedIndex === list.length ? undefined : list.length,
);
}}
/>
</div>
</div>
{selectedIndex !== undefined && (
/**
* This form provider act as a substate of the parent form
* Consider creating an InnerFormProvider since not every feature is expected
*/
<FormProvider
initialValue={selected}
computeFormState={(v) => {
// current state is ignored
// the state is defined by the parent form
// elements should be present in the state object since this is expected to be an array
//@ts-ignore
return state.elements[selectedIndex];
}}
onUpdate={(v) => {
const newValue = [...list];
newValue.splice(selectedIndex, 1, v);
onChange(newValue);
}}
>
<div class="px-4 py-6">
<div class="grid grid-cols-1 gap-y-8 ">
<RenderAllFieldsByUiConfig fields={fields} />
</div>
</div>
</FormProvider>
)}
{selectedIndex !== undefined && (
<div class="flex items-center pt-3">
<div class="flex-auto">
{selected !== undefined && (
<button
type="button"
onClick={() => {
const newValue = [...list];
newValue.splice(selectedIndex, 1);
onChange(newValue);
setSelected(undefined);
}}
class="block rounded-md bg-red-600 px-3 py-2 text-center text-sm text-white shadow-sm hover:bg-red-500 "
>
Remove
</button>
)}
</div>
</div>
)}
</div>
);
}
|