Skip to content

Commit cbfc8f8

Browse files
authored
Autocomplete enhancement with additional schema types (#1910)
1 parent 45a7fd5 commit cbfc8f8

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

geonode_mapstore_client/client/js/components/Autocomplete/Autocomplete.jsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import React from 'react';
1010
import isArray from 'lodash/isArray';
1111
import isEmpty from 'lodash/isEmpty';
12+
import isString from 'lodash/isString';
1213
import PropTypes from 'prop-types';
1314

1415
import SelectInfiniteScroll from '@js/components/SelectInfiniteScroll/SelectInfiniteScroll';
@@ -22,31 +23,26 @@ const Autocomplete = ({
2223
description,
2324
helpTitleIcon,
2425
id,
25-
labelKey,
26+
labelKey = "label",
2627
name,
2728
title,
2829
value,
29-
valueKey,
30+
valueKey = "value",
3031
...props
3132
}) => {
3233
const getValue = () => {
33-
if (value && isArray(value) && valueKey && labelKey) {
34+
if (value && isArray(value)) {
3435
return value.map((entry) => {
3536
return {
3637
result: entry,
37-
value: entry[valueKey],
38-
label: entry[labelKey]
38+
[valueKey]: isString(entry) ? entry : entry[valueKey],
39+
[labelKey]: isString(entry) ? entry : entry[labelKey]
3940
};
4041
});
4142
}
4243
return value;
4344
};
4445

45-
const defaultNewOptionCreator = (option) => ({
46-
[valueKey]: option.label,
47-
[labelKey]: option.label
48-
});
49-
5046
return (
5147
<div className={`autocomplete${className ? " " + className : ""}`}>
5248
<div className="title-container">
@@ -59,9 +55,6 @@ const Autocomplete = ({
5955
value={getValue()}
6056
valueKey={valueKey}
6157
labelKey={labelKey}
62-
{...props.creatable && {
63-
newOptionCreator: props.newOptionCreator ?? defaultNewOptionCreator
64-
}}
6558
/>
6659
</div>
6760
);

geonode_mapstore_client/client/js/components/SelectInfiniteScroll/SelectInfiniteScroll.jsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function SelectInfiniteScroll({
1919
loadOptions,
2020
pageSize = 20,
2121
debounceTime = 500,
22-
labelKey,
23-
valueKey,
22+
labelKey = "label",
23+
valueKey = "value",
2424
newOptionPromptText = "Create option",
2525
...props
2626
}) {
@@ -46,13 +46,17 @@ function SelectInfiniteScroll({
4646

4747
const updateNewOption = (newOptions, query) => {
4848
if (props.creatable && !isEmpty(query)) {
49-
const isValueExist = props.value?.some(v => v[labelKey] === query);
50-
const isOptionExist = newOptions.some((o) => o[labelKey] === query);
49+
const compareValue = (option) =>
50+
option?.[labelKey]?.toLowerCase() === query.toLowerCase();
51+
52+
const isValueExist = props.value?.some(compareValue);
53+
const isOptionExist = newOptions.some(compareValue);
5154

5255
// Add new option if it doesn't exist and `creatable` is enabled
5356
if (!isValueExist && !isOptionExist) {
5457
return [{
55-
[labelKey]: `${newOptionPromptText} "${query}"`, value: query,
58+
[labelKey]: `${newOptionPromptText} "${query}"`,
59+
[valueKey]: query,
5660
result: { [valueKey]: query, [labelKey]: query }
5761
}].concat(newOptions);
5862
}
@@ -141,6 +145,8 @@ function SelectInfiniteScroll({
141145
{...props}
142146
isLoading={loading}
143147
options={options}
148+
labelKey={labelKey}
149+
valueKey={valueKey}
144150
onOpen={() => setOpen(true)}
145151
onClose={() => setOpen(false)}
146152
filterOptions={filterOptions}

geonode_mapstore_client/client/js/plugins/MetadataEditor/components/_fields/SchemaField.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ const SchemaField = (props) => {
2929
uiSchema
3030
} = props;
3131
const autocomplete = uiSchema?.['ui:options']?.['geonode-ui:autocomplete'];
32-
const isMultiSelect = schema?.items?.type === 'object' && !isEmpty(schema?.items?.properties);
32+
const isSchemaItemString = schema?.items?.type === 'string';
33+
const isSchemaItemObject = schema?.items?.type === 'object';
34+
const isMultiSelect = schema?.type === 'array' && (isSchemaItemString ||
35+
(isSchemaItemObject && !isEmpty(schema?.items?.properties))
36+
);
3337
const isSingleSelect = schema?.type === 'object' && !isEmpty(schema?.properties);
3438

3539
if (autocomplete && (isMultiSelect || isSingleSelect)) {
@@ -65,10 +69,14 @@ const SchemaField = (props) => {
6569
if (result === undefined) {
6670
return option;
6771
}
68-
return Object.fromEntries(
69-
Object.keys(schema.items.properties)
70-
.map((key) => [key, result[key]])
71-
);
72+
return isString(result)
73+
? result
74+
: isSchemaItemString
75+
? result[valueKey]
76+
: Object.fromEntries(
77+
Object.keys(schema.items?.properties)
78+
.map((key) => [key, result[key]])
79+
);
7280
});
7381
}
7482
onChange(_selected);
@@ -89,8 +97,8 @@ const SchemaField = (props) => {
8997
return {
9098
selectOption: {
9199
result,
92-
value: result[valueKey],
93-
label: result[labelKey]
100+
[valueKey]: isString(result) ? result : result[valueKey],
101+
[labelKey]: isString(result) ? result : result[labelKey]
94102
}
95103
};
96104
})

0 commit comments

Comments
 (0)