Skip to content

Commit

Permalink
feat: add mapProp API to external field adaptors
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Oct 26, 2023
1 parent daf36ac commit 86c4979
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ External fields can be used to load content from an external content repository,
- **adaptor** (`Adaptor`): Content adaptor responsible for fetching data to show in the table
- **name** (`string`): The human-readable name of the adaptor
- **fetchList** (`(adaptorParams: object) => object`): Fetch a list of content and return an array
- **mapProp** (`(selectedItem: object) => object`): Map the selected item into another shape
- **adaptorParams** (`object`): Paramaters passed to the adaptor

### Custom Fields
Expand Down
28 changes: 14 additions & 14 deletions apps/demo/config/blocks/Hero/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { quotes } from "./quotes";
const getClassName = getClassNameFactory("Hero", styles);

export type HeroProps = {
quote?: { index: number };
quote?: { index: number; label: string };
title: string;
description: string;
align?: string;
Expand All @@ -20,23 +20,23 @@ export type HeroProps = {
buttons: { label: string; href: string; variant?: "primary" | "secondary" }[];
};

// TODO add resolveValue prop so the fetch can return different data to the adaptor
const quotesAdaptor = {
name: "Quotes API",
fetchList: async (): Promise<Partial<HeroProps>[]> =>
quotes.map((quote, idx) => ({
index: idx,
title: quote.author,
description: quote.content,
})),
};

export const Hero: ComponentConfig<HeroProps> = {
fields: {
quote: {
type: "external",
adaptor: quotesAdaptor,
getItemSummary: (item: Partial<HeroProps>) => item.description,
adaptor: {
name: "Quotes API",
fetchList: async () =>
quotes.map((quote, idx) => ({
index: idx,
title: quote.author,
description: quote.content,
})),
mapProp: (result) => {
return { index: result.index, label: result.description };
},
},
getItemSummary: (item) => item.label,
},
title: { type: "text" },
description: { type: "textarea" },
Expand Down
6 changes: 4 additions & 2 deletions packages/core/components/ExternalInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const ExternalInput = ({
onChange: (value: any) => void;
value: any;
}) => {
const { mapProp = (val) => val } = field.adaptor || {};

const [data, setData] = useState<Record<string, any>[]>([]);
const [isOpen, setOpen] = useState(false);
const [selectedData, setSelectedData] = useState(value);
Expand Down Expand Up @@ -112,11 +114,11 @@ export const ExternalInput = ({
key={i}
style={{ whiteSpace: "nowrap" }}
onClick={(e) => {
onChange(item);
onChange(mapProp(item));

setOpen(false);

setSelectedData(item);
setSelectedData(mapProp(item));
}}
>
{keys.map((key) => (
Expand Down
13 changes: 8 additions & 5 deletions packages/core/types/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { ReactElement } from "react";
import { ReactNode } from "react";
import { ItemSelector } from "../lib/get-item";

export type Adaptor<AdaptorParams = {}> = {
export type Adaptor<
AdaptorParams = {},
TableShape extends Record<string, any> = {},
PropShape = TableShape
> = {
name: string;
fetchList: (
adaptorParams?: AdaptorParams
) => Promise<Record<string, any>[] | null>;
fetchList: (adaptorParams?: AdaptorParams) => Promise<TableShape[] | null>;
mapProp?: (value: TableShape) => PropShape;
};

type WithPuckProps<Props> = Props & {
Expand Down Expand Up @@ -47,7 +50,7 @@ export type ExternalField<
Props extends { [key: string]: any } = { [key: string]: any }
> = BaseField & {
type: "external";
adaptor: Adaptor;
adaptor: Adaptor<any, any, Props>;
adaptorParams?: object;
getItemSummary: (item: Props, index?: number) => string;
};
Expand Down

0 comments on commit 86c4979

Please sign in to comment.