Skip to content

Commit

Permalink
Add textField component. (refinedev#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
yildirayunlu committed Feb 8, 2021
1 parent 9180c71 commit a4c4ef3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
9 changes: 4 additions & 5 deletions example/src/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SelectInput,
ReferenceInput,
ReferenceField,
TextField,
} from "readmin";

export const PostList = (props: any) => {
Expand All @@ -26,11 +27,9 @@ export const PostList = (props: any) => {
dataIndex="categoryId"
render={(value) => {
return (
<ReferenceField
value={value}
resource="categories"
optionText="title"
/>
<ReferenceField value={value} resource="categories">
<TextField source="title" />
</ReferenceField>
);
}}
/>
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { List } from "./list";
export { Table } from "./table";
export { Column } from "./column";
export { ReferenceField } from "./referenceField";
export { TextField } from "./textField";
export { Create } from "./create";
export { Edit } from "./edit";
export { Form, FormItem } from "./form";
Expand Down
15 changes: 11 additions & 4 deletions src/components/referenceField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ 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,
children,
}) => {
const { getOne } = useContext<IDataContext>(DataContext);

Expand All @@ -28,5 +26,14 @@ export const ReferenceField: React.FC<ReferenceFieldProps> = ({
return <span>loading</span>;
}

return <span>{data?.data[optionText]}</span>;
const childrenWithProps = React.Children.map(children, (child) => {
if (React.isValidElement(child)) {
return React.cloneElement(child, {
record: data?.data,
});
}
return child;
});

return <React.Fragment>{childrenWithProps}</React.Fragment>;
};
10 changes: 10 additions & 0 deletions src/components/textField/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";

export interface TextFieldProps {
record?: any;
source: string;
}

export const TextField: React.FC<TextFieldProps> = ({ record, source }) => {
return <span>{record[source]}</span>;
};
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
RadioGroupInput,
AutoCompleteInput,
ReferenceInput,
TextField,
} from "./components";

export { IAuthContext as AuthProvider, Sort, Pagination } from "./interfaces";
Expand Down

0 comments on commit a4c4ef3

Please sign in to comment.