-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMultipleChoiceItemInput.tsx
More file actions
224 lines (187 loc) · 6.44 KB
/
MultipleChoiceItemInput.tsx
File metadata and controls
224 lines (187 loc) · 6.44 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { useMemo, useCallback, ChangeEvent } from 'react';
import classNames from 'classnames';
import { ChoiceSet } from '@neinteractiveliterature/litform';
import CaptionLegend from './CaptionLegend';
import { CommonFormItemInputProps } from './CommonFormItemInputProps';
import { FormItemValueType, MultipleChoiceFormItem } from '../../FormAdmin/FormItemUtils';
import { VisibilityDisclosureCard } from './PermissionDisclosures';
import assertNever from 'assert-never';
const OTHER_VALUE = '_OTHER_VALUE';
function castSingleValue(value: FormItemValueType<MultipleChoiceFormItem> | null | undefined): string | null {
if (value == null) {
return null;
}
if (typeof value === 'string' || typeof value === 'boolean') {
return value.toString();
}
if (Array.isArray(value)) {
return value.length > 0 ? value[0].toString() : null;
}
assertNever(value);
}
function castMultipleValue(value: FormItemValueType<MultipleChoiceFormItem> | null | undefined): string[] {
let arrayValue: string[];
if (Array.isArray(value)) {
arrayValue = value.map((item) => item.toString());
} else if (value == null) {
arrayValue = [];
} else {
arrayValue = [value.toString()];
}
return arrayValue;
}
export type MultipleChoiceItemInputProps = CommonFormItemInputProps<MultipleChoiceFormItem>;
function MultipleChoiceItemInput({
formItem,
formTypeIdentifier,
onChange,
value: uncastValue,
valueInvalid,
onInteract,
}: MultipleChoiceItemInputProps): React.JSX.Element {
const isMultiple = useMemo(
() => ['checkbox_horizontal', 'checkbox_vertical'].includes(formItem.rendered_properties.style),
[formItem.rendered_properties.style],
);
const value = isMultiple ? castMultipleValue(uncastValue) : castSingleValue(uncastValue);
const otherIsSelected = useMemo(() => {
if (!formItem.rendered_properties.other) {
return false;
}
const choiceValues = formItem.rendered_properties.choices.map((choice) => choice.value);
if (Array.isArray(value)) {
return !value.every((selectedChoiceValue) => choiceValues.includes(selectedChoiceValue));
}
return value != null && !choiceValues.includes(value);
}, [formItem.rendered_properties.choices, formItem.rendered_properties.other, value]);
const otherValue = useMemo(() => {
if (otherIsSelected) {
if (isMultiple) {
const choiceValues = formItem.rendered_properties.choices.map((choice) => choice.value);
return castMultipleValue(value).find((selectedChoiceValue) => !choiceValues.includes(selectedChoiceValue));
}
return castSingleValue(value);
}
return '';
}, [formItem.rendered_properties.choices, isMultiple, otherIsSelected, value]);
const choicesForChoiceSet = useMemo(() => {
const providedChoices = formItem.rendered_properties.choices.map((choice) => ({
label: choice.caption,
value: choice.value,
}));
if (formItem.rendered_properties.other) {
const otherCaption = formItem.rendered_properties.other_caption || 'Other';
return [
...providedChoices,
{
label: otherCaption,
value: OTHER_VALUE,
},
];
}
return providedChoices;
}, [
formItem.rendered_properties.choices,
formItem.rendered_properties.other,
formItem.rendered_properties.other_caption,
]);
const valueForChoiceSet = useMemo(() => {
if (Array.isArray(value)) {
if (otherIsSelected) {
return [...value, OTHER_VALUE];
}
return value;
}
if (otherIsSelected) {
return OTHER_VALUE;
}
return castSingleValue(value);
}, [otherIsSelected, value]);
const userDidInteract = useCallback(() => onInteract(formItem.identifier), [formItem.identifier, onInteract]);
const valueDidChangeMultiple = (newValue: string[] | null) => {
userDidInteract();
const choiceValues = formItem.rendered_properties.choices.map((choice) => choice.value);
const providedValues = (newValue ?? []).filter((choiceValue) =>
choiceValues.some((providedValue) => providedValue === choiceValue),
);
if (otherValue && newValue?.includes(OTHER_VALUE)) {
onChange([...providedValues, otherValue]);
} else {
onChange(providedValues);
}
};
const valueDidChangeSingle = (newValue: string | null) => {
userDidInteract();
if (newValue === OTHER_VALUE) {
onChange(otherValue);
} else {
onChange(newValue);
}
};
const otherValueDidChange = (event: ChangeEvent<HTMLInputElement>) => {
userDidInteract();
if (typeof valueForChoiceSet === 'string') {
onChange(event.target.value);
} else {
onChange(
(valueForChoiceSet ?? []).map((singleValue) => {
if (singleValue === OTHER_VALUE) {
return event.target.value;
}
return singleValue;
}),
);
}
};
const renderOtherInput = () => {
if (!otherIsSelected) {
return null;
}
return (
<input
aria-label={`${formItem.rendered_properties.other_caption ?? 'Other'}: please specify`}
type="text"
className="form-control"
value={otherValue ?? ''}
onChange={otherValueDidChange}
placeholder="Please specify"
/>
);
};
const choiceClassName = classNames({
'form-check-inline': ['radio_horizontal', 'checkbox_horizontal'].includes(formItem.rendered_properties.style),
});
return (
<fieldset className="mb-3">
<VisibilityDisclosureCard formItem={formItem} formTypeIdentifier={formTypeIdentifier}>
<div
className={classNames({
'border-0': !valueInvalid,
'border rounded border-danger': valueInvalid,
})}
>
<CaptionLegend formItem={formItem} />
{Array.isArray(valueForChoiceSet) ? (
<ChoiceSet
choices={choicesForChoiceSet}
value={valueForChoiceSet}
onChange={valueDidChangeMultiple}
multiple
choiceClassName={choiceClassName}
/>
) : (
<ChoiceSet
choices={choicesForChoiceSet}
value={valueForChoiceSet}
onChange={valueDidChangeSingle}
choiceClassName={choiceClassName}
/>
)}
{renderOtherInput()}
{valueInvalid ? <span className="text-danger">This field is required.</span> : null}
</div>
</VisibilityDisclosureCard>
</fieldset>
);
}
export default MultipleChoiceItemInput;