Skip to content

Commit 5e65b21

Browse files
committed
feat: display custom types
1 parent df0aa9c commit 5e65b21

2 files changed

Lines changed: 43 additions & 48 deletions

File tree

packages/slice-machine/src/features/customTypes/fields/ContentRelationshipFieldPicker.tsx

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ import {
55
TreeViewCheckbox,
66
TreeViewSection,
77
} from "@prismicio/editor-ui";
8+
import { useSelector } from "react-redux";
9+
10+
import {
11+
hasLocal,
12+
LocalOrRemoteCustomType,
13+
} from "@/legacy/lib/models/common/ModelData";
14+
import { selectAllCustomTypes } from "@/modules/availableCustomTypes";
815

916
export function ContentRelationshipFieldPicker() {
17+
const customTypes = useSelector(selectAllCustomTypes).filter(hasLocal);
18+
const simplifiedCustomTypes = simplifyCustomTypes(customTypes);
19+
1020
return (
1121
<Box overflow="hidden" flexDirection="column" border borderRadius={6}>
1222
<Box
@@ -24,32 +34,18 @@ export function ContentRelationshipFieldPicker() {
2434
</Text>
2535
</Box>
2636
<TreeView title="Exposed fields" subtitle="(3)">
27-
<TreeViewSection
28-
title="Custom Type A"
29-
subtitle="(3 fields exposed)"
30-
badge="Custom Type"
31-
>
32-
<TreeViewCheckbox title="Name" />
33-
<TreeViewCheckbox title="Biography" />
37+
{simplifiedCustomTypes.map((ct) => (
3438
<TreeViewSection
35-
title="Slice A"
36-
subtitle="(2 fields exposed)"
37-
badge="Slice"
39+
key={ct.id}
40+
title={ct.label}
41+
subtitle={`(${ct.fields.length} fields exposed)`}
42+
badge="Custom Type"
3843
>
39-
<TreeViewCheckbox title="Title" />
40-
<TreeViewCheckbox title="Subtitle" />
41-
<TreeViewCheckbox title="Image" />
44+
{ct.fields.map((field) => {
45+
return <TreeViewCheckbox key={field.id} title={field.label} />;
46+
})}
4247
</TreeViewSection>
43-
</TreeViewSection>
44-
<TreeViewSection title="Custom Type B" badge="Custom Type">
45-
Something else
46-
</TreeViewSection>
47-
<TreeViewSection title="Custom Type C" badge="Custom Type">
48-
Something else
49-
</TreeViewSection>
50-
<TreeViewSection title="Custom Type D" badge="Custom Type">
51-
Something else
52-
</TreeViewSection>
48+
))}
5349
</TreeView>
5450
</Box>
5551
<Box backgroundColor="white" flexDirection="column" padding={12}>
@@ -68,3 +64,25 @@ export function ContentRelationshipFieldPicker() {
6864
</Box>
6965
);
7066
}
67+
68+
type SimplifiedCustomType = {
69+
id: string;
70+
label: string;
71+
fields: { id: string; label: string }[];
72+
};
73+
74+
function simplifyCustomTypes(customTypes: LocalOrRemoteCustomType[]) {
75+
return customTypes.flatMap<SimplifiedCustomType>((ct) => {
76+
if (!("local" in ct)) return [];
77+
78+
const { id, label, tabs } = ct.local;
79+
const fields = tabs.flatMap((tab) => {
80+
return tab.value.map((field) => ({
81+
id: field.key,
82+
label: field.value.config?.label ?? field.key,
83+
}));
84+
});
85+
86+
return { id, label: label ?? id, fields };
87+
});
88+
}

packages/slice-machine/src/legacy/lib/models/common/widgets/ContentRelationship/Form.tsx

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import { FormikProps } from "formik";
2-
import { useSelector } from "react-redux";
3-
import Select from "react-select";
4-
import { Box, Label } from "theme-ui";
2+
import { Box } from "theme-ui";
53
import * as yup from "yup";
64

5+
import { ContentRelationshipFieldPicker } from "@/features/customTypes/fields/ContentRelationshipFieldPicker";
76
import { Col, Flex as FlexGrid } from "@/legacy/components/Flex";
87
import WidgetFormField from "@/legacy/lib/builders/common/EditModal/Field";
98
import { createFieldNameFromKey } from "@/legacy/lib/forms";
109
import { DefaultFields } from "@/legacy/lib/forms/defaults";
11-
import { selectAllCustomTypes } from "@/modules/availableCustomTypes";
12-
13-
import { hasLocal } from "../../ModelData";
14-
import { ContentRelationshipFieldPicker } from "@/features/customTypes/fields/ContentRelationshipFieldPicker";
1510

1611
const FormFields = {
1712
label: DefaultFields.label,
@@ -29,26 +24,8 @@ type FormProps = {
2924

3025
const WidgetForm = ({
3126
initialValues,
32-
values: formValues,
3327
fields,
34-
setFieldValue,
3528
}: FormikProps<FormProps> & { fields: Record<string, unknown> }) => {
36-
const customTypes = useSelector(selectAllCustomTypes).filter(hasLocal);
37-
38-
const options = customTypes.map((ct) => ({
39-
value: ct.local.id,
40-
label: ct.local.label,
41-
}));
42-
43-
const selectValues = formValues.config.customtypes
44-
? formValues.config.customtypes.map((id) => {
45-
const ct = customTypes.find(
46-
(frontendCustomType) => frontendCustomType.local.id === id,
47-
);
48-
return { value: ct?.local.id, label: ct?.local.label };
49-
})
50-
: null;
51-
5229
return (
5330
<>
5431
<FlexGrid>

0 commit comments

Comments
 (0)