Skip to content

Commit 88ef3a6

Browse files
committed
Rename WidgetTag to WidgetTagger
Variations: - Select one or add (new) - Select multiple or add - Add multiple
1 parent 989da5e commit 88ef3a6

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

packages/data-widgets/lib/config/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { WidgetArray } from '../widgets/array';
99
import { WidgetArrayInput } from '../widgets/array-input';
1010
import { WidgetSelect } from '../widgets/select';
1111
import { WidgetJSON } from '../widgets/json';
12-
import { WidgetTags } from '../widgets/tags';
12+
import { WidgetTagger } from '../widgets/tagger';
1313

1414
export const defaultPluginWidgetConfig = extendPluginConfig({
1515
'ui:widget': {
@@ -19,7 +19,7 @@ export const defaultPluginWidgetConfig = extendPluginConfig({
1919
radio: WidgetRadio,
2020
checkbox: WidgetCheckbox,
2121
select: WidgetSelect,
22-
tags: WidgetTags,
22+
tagger: WidgetTagger,
2323
array: WidgetArray,
2424
'array:string': WidgetArrayInput,
2525
json: WidgetJSON

packages/data-widgets/lib/widgets/tags.tsx renamed to packages/data-widgets/lib/widgets/tagger.tsx

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,31 @@ interface Option {
2424
readonly value: string;
2525
}
2626

27-
export function WidgetTags(props: WidgetProps) {
27+
export function WidgetTagger(props: WidgetProps) {
2828
const { field } = props;
2929

30-
if (field.type !== 'array') {
31-
throw new Error('WidgetTags only supports array fields');
30+
if (field.type === 'string') {
31+
if (!field.enum) {
32+
throw new Error("WidgetTagger: 'enum' is required for string fields");
33+
}
34+
35+
return <WidgetTaggerWithOptions {...props} />;
3236
}
3337

34-
if (field.items.type === 'string' && field.items.enum) {
35-
return <WidgetTagsWithOptions {...props} />;
38+
if (field.type === 'array' && field.items.type === 'string') {
39+
return field.items.enum ? (
40+
<WidgetTaggerWithOptions {...props} isMulti />
41+
) : (
42+
<WidgetTaggerNoOptions {...props} />
43+
);
3644
}
3745

38-
return <WidgetTagsNoOptions {...props} />;
46+
throw new Error(
47+
`WidgetTagger: Field type must be 'string' or 'array'. Got: ${field.type}`
48+
);
3949
}
4050

41-
function WidgetTagsNoOptions(props: WidgetProps) {
51+
function WidgetTaggerNoOptions(props: WidgetProps) {
4252
const { pointer, isRequired, field } = props;
4353

4454
const [inputValue, setInputValue] = useState('');
@@ -89,17 +99,26 @@ function WidgetTagsNoOptions(props: WidgetProps) {
8999
);
90100
}
91101

92-
function WidgetTagsWithOptions(props: WidgetProps) {
93-
const { pointer, isRequired } = props;
94-
const field = props.field as SchemaFieldArray<SchemaFieldString>;
102+
function WidgetTaggerWithOptions(props: WidgetProps & { isMulti?: boolean }) {
103+
const { pointer, isRequired, isMulti, field } = props;
95104

96105
const [inputValue, setInputValue] = useState('');
97106

98107
const [{ value }, , { setValue }] = useField(pointer);
99-
const selectedValues = Array.isArray(value) ? value.map(createOption) : [];
108+
const selectedValues = value
109+
? isMulti
110+
? value.map(createOption)
111+
: createOption(value)
112+
: isMulti
113+
? []
114+
: undefined;
100115

101116
const options = useMemo(() => {
102-
return field.items.enum!.map<Option>(([value, label]) => ({
117+
const enums = isMulti
118+
? (field as SchemaFieldArray<SchemaFieldString>).items.enum
119+
: (field as SchemaFieldString).enum;
120+
121+
return enums!.map<Option>(([value, label]) => ({
103122
value,
104123
label
105124
}));
@@ -128,14 +147,20 @@ function WidgetTagsWithOptions(props: WidgetProps) {
128147
<CreatableSelect
129148
inputValue={inputValue}
130149
isClearable
131-
isMulti
150+
isMulti={isMulti}
132151
onChange={(newValue) => {
133-
setValue(newValue.map((option) => option.value));
152+
if (isMulti) {
153+
setValue(
154+
((newValue as Option[]) || []).map((option) => option.value)
155+
);
156+
} else {
157+
setValue(newValue?.value);
158+
}
134159
}}
135160
onInputChange={(newValue) => setInputValue(newValue)}
136161
onKeyDown={handleKeyDown}
137162
options={options}
138-
placeholder='Select or type to add options'
163+
placeholder='Select or type to add'
139164
value={selectedValues}
140165
/>
141166
</FormControl>

0 commit comments

Comments
 (0)