Skip to content

Commit 217e6f9

Browse files
fix: allow to select entities (#530) (#531)
* fix: allow to select entities * fix: add missing dependency * fix: combobox for related types * change email * fix: fix build * fix: add package json missing script * fix: bump version
1 parent 70378c3 commit 217e6f9

File tree

4 files changed

+679
-503
lines changed

4 files changed

+679
-503
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ComboboxOption } from "@strapi/design-system"
2+
import { Combobox } from "@strapi/design-system"
3+
import { useEffect, useState } from "react";
4+
import { FormChangeEvent } from "src/types";
5+
6+
type ControllableComboboxProps = {
7+
name: string;
8+
onClear: () => void;
9+
onChange: (eventOrPath: FormChangeEvent) => void;
10+
options: {
11+
key: string;
12+
value: string;
13+
label: string;
14+
}[];
15+
value: string | undefined;
16+
disabled: boolean;
17+
};
18+
19+
export const ControllableCombobox: React.FC<ControllableComboboxProps> = ({
20+
name,
21+
onClear,
22+
onChange,
23+
options,
24+
value,
25+
disabled,
26+
}) => {
27+
const [textValue, setTextValue] = useState('');
28+
29+
const handleTextValueChange = (textValue: string) => {
30+
setTextValue(textValue);
31+
};
32+
33+
useEffect(() => {
34+
if (!value) {
35+
handleTextValueChange('');
36+
}
37+
}, [value]);
38+
39+
return (
40+
<Combobox
41+
name={name}
42+
autocomplete="list"
43+
onClear={onClear}
44+
onChange={onChange}
45+
onTextValueChange={handleTextValueChange}
46+
value={value}
47+
textValue={textValue}
48+
options={options}
49+
disabled={disabled}
50+
width="100%"
51+
>
52+
{options.map(({ key, label, value }) => (
53+
<ComboboxOption key={key} value={value}>
54+
{label}
55+
</ComboboxOption>
56+
))}
57+
</Combobox>
58+
);
59+
};

admin/src/pages/HomePage/components/NavigationItemForm/index.tsx

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { Grid, Modal, Toggle } from '@strapi/design-system';
99
import {
1010
Box,
1111
Button,
12-
Combobox,
13-
ComboboxOption,
1412
Divider,
1513
MultiSelect,
1614
MultiSelectOption,
@@ -45,6 +43,7 @@ import {
4543
} from './utils/form';
4644
import { useSlug } from './utils/hooks';
4745
import { generatePreviewPath, generateUiRouterKey } from './utils/properties';
46+
import { ControllableCombobox } from '../ControllableCombobox';
4847

4948
export { ContentTypeEntity, GetContentTypeEntitiesPayload } from './types';
5049
export { NavigationItemFormSchema } from './utils/form';
@@ -118,7 +117,9 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
118117
fieldValue = isNil(fieldValue) ? targetValue : fieldValue;
119118
}
120119

121-
if (isString(fieldName)) {
120+
if (isObject(fieldValue)) {
121+
setFormValuesItems(fieldValue);
122+
} else if (isString(fieldName)) {
122123
setFormValueItem(fieldName, fieldValue);
123124
}
124125

@@ -142,6 +143,18 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
142143
);
143144
};
144145

146+
const setFormValuesItems = (values: any) =>
147+
setFormValue(
148+
{
149+
...formValue,
150+
additionalFields: {
151+
...formValue.additionalFields,
152+
},
153+
updated: true,
154+
...values
155+
},
156+
);
157+
145158
const encodePayload = (values: NavigationItemFormSchema): NavigationItemFormSchema => {
146159
return {
147160
...values,
@@ -548,7 +561,6 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
548561
<Modal.Body>
549562
<Form width="auto" height="auto" method="POST" initialValues={formValue}>
550563
{({ values, onChange }) => {
551-
console.log('values', values);
552564
const internalValues =
553565
values.type === 'INTERNAL'
554566
? values
@@ -743,34 +755,32 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
743755
: undefined
744756
}
745757
>
746-
<Combobox
758+
<ControllableCombobox
747759
name="relatedType"
748-
autocomplete="both"
749-
onClear={() => {
750-
handleChange('relatedType', undefined, onChange);
751-
handleChange('related', undefined, onChange);
752-
if (values.autoSync) {
753-
handleChange('title', undefined, onChange);
754-
}
755-
}}
756-
onChange={(eventOrPath: FormChangeEvent) => {
757-
handleChange('relatedType', eventOrPath, onChange);
758-
handleChange('related', undefined, onChange);
759-
if (values.autoSync) {
760-
handleChange('title', undefined, onChange);
761-
}
760+
onClear={() =>
761+
handleChange(
762+
"",
763+
{
764+
related: undefined,
765+
title: undefined,
766+
relatedType: undefined,
767+
},
768+
onChange)
762769
}
770+
onChange={(eventOrPath: FormChangeEvent) =>
771+
handleChange(
772+
eventOrPath,
773+
{
774+
related: undefined,
775+
title: values.autoSync ? '' : values.title,
776+
relatedType: eventOrPath,
777+
},
778+
onChange)
763779
}
764780
value={values.relatedType}
781+
options={configQuery.data?.contentTypes.map(contentType => ({ key: contentType.uid, value: contentType.uid, label: contentType.contentTypeName })) ?? []}
765782
disabled={!configQuery.data?.contentTypes.length || !canUpdate}
766-
width="100%"
767-
>
768-
{configQuery.data?.contentTypes.map((contentType) => (
769-
<ComboboxOption key={contentType.uid} value={contentType.uid}>
770-
{contentType.contentTypeName}
771-
</ComboboxOption>
772-
))}
773-
</Combobox>
783+
/>
774784
</Field>
775785
</Grid.Item>
776786

@@ -792,24 +802,18 @@ export const NavigationItemForm: React.FC<NavigationItemFormProps> = ({
792802
: undefined
793803
}
794804
>
795-
<Combobox
805+
<ControllableCombobox
796806
name="related"
797-
autocomplete="both"
798-
onClear={() => handleChange('related', undefined, onChange)}
807+
onClear={() =>
808+
handleChange('related', undefined, onChange)
809+
}
799810
onChange={(eventOrPath: FormChangeEvent) =>
800811
handleChange('related', eventOrPath, onChange)
801812
}
802813
value={values.related}
803814
options={relatedSelectOptions}
804815
disabled={isLoading || thereAreNoMoreContentTypes || !canUpdate}
805-
width="100%"
806-
>
807-
{relatedSelectOptions.map(({ key, label, value }) => (
808-
<ComboboxOption key={key} value={value}>
809-
{label}
810-
</ComboboxOption>
811-
))}
812-
</Combobox>
816+
/>
813817
</Field>
814818
</Grid.Item>
815819
)}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strapi-plugin-navigation",
3-
"version": "3.0.8",
3+
"version": "3.0.9",
44
"description": "Strapi - Navigation plugin",
55
"strapi": {
66
"name": "navigation",

0 commit comments

Comments
 (0)