Back to Refine

Create

documentation/versioned_docs/version-3.xx.xx/api-reference/antd/components/buttons/create.md

3.25.04.0 KB
Original Source

<CreateButton> uses Ant Design's <Button> component. It uses the create method from useNavigation under the hood. It can be useful to redirect the app to the create page route of resource.

:::info-tip Swizzle You can swizzle this component to customize it with the refine CLI :::

Usage

tsx
const { useRouterContext } = RefineCore;
// visible-block-start
import {
  Table,
  List,
  useTable,
  // highlight-next-line
  CreateButton,
} from "@pankod/refine-antd";

const PostList: React.FC = () => {
  const { tableProps } = useTable<IPost>();

  return (
    <List
      // highlight-next-line
      headerButtons={<CreateButton />}
    >
      <Table {...tableProps} rowKey="id">
        <Table.Column dataIndex="id" title="ID" />
        <Table.Column dataIndex="title" title="Title" width="100%" />
      </Table>
    </List>
  );
};

interface IPost {
  id: number;
  title: string;
}
// visible-block-end

const CreatePage = () => {
  const params = useRouterContext().useParams();
  return <div>{JSON.stringify(params)}</div>;
};

render(
  <RefineAntdDemo
    resources={[
      {
        name: "posts",
        list: PostList,
        create: CreatePage,
      },
    ]}
  />,
);

Properties

resourceNameOrRouteName

It is used to redirect the app to the /create endpoint of the given resource name. By default, the app redirects to a URL with /create defined by the name property of resource object.

tsx
const { useRouterContext } = RefineCore;

// visible-block-start
import { CreateButton } from "@pankod/refine-antd";

const MyCreateComponent = () => {
  return (
    <CreateButton
      // highlight-next-line
      resourceNameOrRouteName="categories"
    />
  );
};

// visible-block-end

const CreatePage = () => {
  const params = useRouterContext().useParams();
  return <div>{JSON.stringify(params)}</div>;
};

render(
  <RefineAntdDemo
    initialRoutes={["/"]}
    resources={[
      {
        name: "posts",
      },
      {
        name: "categories",
        create: CreatePage,
      },
    ]}
    DashboardPage={MyCreateComponent}
  />,
);

Clicking the button will trigger the create method of useNavigation and then redirect to /posts/create.

hideText

It is used to show and not show the text of the button. When true, only the button icon is visible.

tsx
const { useRouterContext } = RefineCore;

// visible-block-start
import { CreateButton } from "@pankod/refine-antd";

const MyCreateComponent = () => {
  return (
    <CreateButton
      // highlight-next-line
      hideText={true}
    />
  );
};

// visible-block-end

const CreatePage = () => {
  const params = useRouterContext().useParams();
  return <div>{JSON.stringify(params)}</div>;
};

render(
  <RefineAntdDemo
    initialRoutes={["/"]}
    resources={[
      {
        name: "posts",
        list: MyCreateComponent,
        create: CreatePage,
      },
    ]}
  />,
);

accessControl

This prop can be used to skip access control check with its enabled property or to hide the button when the user does not have the permission to access the resource with hideIfUnauthorized property. This is relevant only when an accessControlProvider is provided to <Refine/>

tsx
import { CreateButton } from "@pankod/refine-antd";

export const MyListComponent = () => {
  return (
    <CreateButton accessControl={{ enabled: true, hideIfUnauthorized: true }} />
  );
};

API Reference

Properties

<PropsTable module="@pankod/refine-antd/CreateButton" />

:::tip External Props It also accepts all props of Ant Design Button. :::