Skip to content

Commit 002b8f5

Browse files
Merge pull request #154 from CivicDataLab/151-dropdown-component-with-dynamic-creation-of-options
Add creatable option to tags and categories dropdowns
2 parents e783e05 + bdf9eff commit 002b8f5

File tree

3 files changed

+92
-17
lines changed

3 files changed

+92
-17
lines changed

app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/EditMetadata.tsx

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
Form,
1919
FormLayout,
2020
Input,
21+
Select,
2122
Text,
2223
toast,
2324
} from 'opub-ui';
@@ -61,6 +62,7 @@ const datasetMetadataQueryDoc: any = graphql(`
6162
metadataItem {
6263
id
6364
label
65+
dataType
6466
}
6567
id
6668
value
@@ -178,8 +180,17 @@ export function EditMetadata({ id }: { id: string }) {
178180
} = {};
179181

180182
dataset.metadata?.map((field) => {
181-
defaultVal[field.metadataItem.id] = field.value;
183+
if (field.metadataItem.dataType === 'MULTISELECT') {
184+
// Convert comma-separated string to array of {label, value} objects
185+
defaultVal[field.metadataItem.id] = field.value.split(', ').map((value: string) => ({
186+
label: value,
187+
value: value,
188+
}));
189+
} else {
190+
defaultVal[field.metadataItem.id] = field.value;
191+
}
182192
});
193+
183194

184195
defaultVal['description'] = dataset.description || '';
185196

@@ -240,6 +251,65 @@ export function EditMetadata({ id }: { id: string }) {
240251
);
241252
}
242253

254+
if (metadataFormItem.dataType === 'SELECT') {
255+
return (
256+
<div
257+
key={metadataFormItem.id}
258+
className="w-full py-4 pr-4 sm:w-1/2 md:w-1/2 lg:w-1/2 xl:w-1/2"
259+
>
260+
261+
<Select
262+
name={metadataFormItem.id}
263+
options={metadataFormItem.options.map((option: string) => ({
264+
value: option,
265+
label: option,
266+
}))}
267+
label={metadataFormItem.label}
268+
/>
269+
</div>
270+
);
271+
}
272+
if (metadataFormItem.dataType === 'MULTISELECT') {
273+
274+
const prefillData = metadataFormItem.value ? metadataFormItem.value : [];
275+
276+
return (
277+
<div
278+
key={metadataFormItem.id}
279+
className="w-full py-4 pr-4 sm:w-1/2 md:w-1/2 lg:w-1/2 xl:w-1/2"
280+
>
281+
282+
<Combobox
283+
name={metadataFormItem.id}
284+
list={[...metadataFormItem.options.map((option: string) => ({
285+
label: option,
286+
value: option,
287+
}))]}
288+
label={metadataFormItem.label}
289+
displaySelected
290+
selectedValue={prefillData}
291+
/>
292+
</div>
293+
);
294+
}
295+
if (metadataFormItem.dataType === 'URL') {
296+
return (
297+
<div
298+
key={metadataFormItem.id}
299+
className="w-full py-4 pr-4 sm:w-1/2 md:w-1/2 lg:w-1/2 xl:w-1/2"
300+
>
301+
<Input
302+
name={metadataFormItem.id}
303+
type='url'
304+
label={metadataFormItem.label}
305+
disabled={
306+
getMetaDataListQuery.isLoading || !metadataFormItem.enabled
307+
}
308+
/>
309+
</div>
310+
);
311+
}
312+
243313
// Add more conditions if there are other data types you want to handle
244314
return null;
245315
}
@@ -248,20 +318,28 @@ export function EditMetadata({ id }: { id: string }) {
248318
<>
249319
<Form
250320
onSubmit={(values) => {
321+
322+
const transformedValues = Object.keys(values).reduce((acc:any, key) => {
323+
acc[key] = Array.isArray(values[key])
324+
? values[key].map((item:any) => item.value || item).join(', ')
325+
: values[key];
326+
return acc;
327+
}, {});
328+
251329
// Call the mutation to save both the static and dynamic metadata
252330
updateMetadataMutation.mutate({
253331
UpdateMetadataInput: {
254332
dataset: id,
255333
metadata: [
256-
...Object.keys(values)
334+
...Object.keys(transformedValues)
257335
.filter(
258336
(valueItem) =>
259337
!['categories', 'description', 'tags'].includes(valueItem)
260338
)
261339
.map((key) => {
262340
return {
263341
id: key,
264-
value: values[key] || '',
342+
value: transformedValues[key] || '',
265343
};
266344
}),
267345
],
@@ -312,6 +390,7 @@ export function EditMetadata({ id }: { id: string }) {
312390
}) || []
313391
}
314392
label="Tags"
393+
creatable
315394
/>
316395
</div>
317396
<div className="w-full py-4 pr-4 sm:w-1/2 md:w-1/2 lg:w-1/2 xl:w-1/2">

package-lock.json

Lines changed: 9 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"next-auth": "^4.24.7",
3636
"next-intl": "^3.4.0",
3737
"next-usequerystate": "^1.17.2",
38-
"opub-ui": "latest",
38+
"opub-ui": "0.3.47",
3939
"react": "^18.2.0",
4040
"react-aria": "3.22.0",
4141
"react-dom": "^18.2.0",

0 commit comments

Comments
 (0)