Skip to content

Commit

Permalink
Add ReferenceField component.
Browse files Browse the repository at this point in the history
  • Loading branch information
yildirayunlu committed Feb 7, 2021
1 parent 50337e7 commit 7c2d81a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions example/src/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TextareaInput,
SelectInput,
ReferenceInput,
ReferenceField,
} from "readmin";

export const PostList = (props: any) => {
Expand All @@ -23,6 +24,15 @@ export const PostList = (props: any) => {
key="categoryId"
title="Category"
dataIndex="categoryId"
render={(value) => {
return (
<ReferenceField
value={value}
resource="categories"
optionText="title"
/>
);
}}
/>
<Column key="status" title="Status" dataIndex="status" />
</Table>
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { Layout } from "./layout";
export { List } from "./list";
export { Table } from "./table";
export { Column } from "./column";
export { ReferenceField } from "./referenceField";
export { Create } from "./create";
export { Edit } from "./edit";
export { Form, FormItem } from "./form";
Expand Down
32 changes: 32 additions & 0 deletions src/components/referenceField/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useContext } from "react";
import { useQuery } from "react-query";

import { DataContext } from "@contexts/data";
import { GetOneResponse, IDataContext } from "@interfaces";

export interface ReferenceFieldProps {
resource: string;
optionText?: string;
value: string | number;
record?: any;
}

export const ReferenceField: React.FC<ReferenceFieldProps> = ({
resource,
optionText = "title",
value,
}) => {
const { getOne } = useContext<IDataContext>(DataContext);

const { data, isFetching } = useQuery<GetOneResponse>(
[`resource/one/${resource}/`, { id: value }],
() => getOne(resource, value),
);

if (isFetching) {
// TODO: Add loding ui.
return <span>loading</span>;
}

return <span>{data?.data[optionText]}</span>;
};
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { Admin, Resource } from "./containers";
export {
List,
Column,
ReferenceField,
Table,
Form,
FormItem,
Expand Down

0 comments on commit 7c2d81a

Please sign in to comment.