Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReferenceField component. #32

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add ReferenceField component.
  • Loading branch information
yildirayunlu committed Feb 7, 2021
commit 7c2d81ab07a93e5e49b7b618e512e613f05fc468
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