Skip to content

Commit 89b83b7

Browse files
committed
enhance autocomplete
1 parent ecdba75 commit 89b83b7

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-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: 9 additions & 4 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,14 +46,17 @@ function SelectInfiniteScroll({
4646

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

5355
// Add new option if it doesn't exist and `creatable` is enabled
5456
if (!isValueExist && !isOptionExist) {
5557
return [{
56-
[labelKey]: `${newOptionPromptText} "${query}"`, value: query,
58+
[labelKey]: `${newOptionPromptText} "${query}"`,
59+
[valueKey]: query,
5760
result: { [valueKey]: query, [labelKey]: query }
5861
}].concat(newOptions);
5962
}
@@ -142,6 +145,8 @@ function SelectInfiniteScroll({
142145
{...props}
143146
isLoading={loading}
144147
options={options}
148+
labelKey={labelKey}
149+
valueKey={valueKey}
145150
onOpen={() => setOpen(true)}
146151
onClose={() => setOpen(false)}
147152
filterOptions={filterOptions}

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ const SchemaField = (props) => {
2929
uiSchema
3030
} = props;
3131
const autocomplete = uiSchema?.['ui:options']?.['geonode-ui:autocomplete'];
32-
const isMultiSelect = schema?.type === 'array' && (schema?.items?.type === 'string' ||
33-
(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))
3436
);
3537
const isSingleSelect = schema?.type === 'object' && !isEmpty(schema?.properties);
3638

@@ -67,10 +69,14 @@ const SchemaField = (props) => {
6769
if (result === undefined) {
6870
return option;
6971
}
70-
return Object.fromEntries(
71-
Object.keys(schema.items.properties)
72-
.map((key) => [key, result[key]])
73-
);
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+
);
7480
});
7581
}
7682
onChange(_selected);
@@ -91,8 +97,8 @@ const SchemaField = (props) => {
9197
return {
9298
selectOption: {
9399
result,
94-
value: result[valueKey],
95-
label: result[labelKey]
100+
[valueKey]: isString(result) ? result : result[valueKey],
101+
[labelKey]: isString(result) ? result : result[labelKey]
96102
}
97103
};
98104
})

0 commit comments

Comments
 (0)