useModalForm
useModalForm hook also allows you to manage a form inside a modal component. It provides some useful methods to handle the form modal.
useModalForm hook is extended from useForm hook from the @pankod/refine-mantine package. This means that you can use all the features of useForm hook.
Basic Usage
We'll show three examples, "create", "edit" and "clone". Let's see how useModalForm is used in all.
- create
- edit
- clone
In this example, we will show you how to "create" a record with useModalForm.
In this example, we will show you how to "edit" a record with useModalForm.
refine doesn't automatically add a <EditButton/> to the each record in <PostList> which opens "edit" form in <Modal> when clicked.
So, we have to put the <EditButton/> on our list. In that way, "edit" form in <Modal> can fetch data by the record id.
const columns = React.useMemo<ColumnDef<IPost>[]>(
  () => [
    // --
    {
      id: 'actions',
      header: 'Actions',
      accessorKey: 'id',
      enableColumnFilter: false,
      enableSorting: false,
      cell: function render({ getValue }) {
        return (
          <Group spacing="xs" noWrap>
            <EditButton hideText onClick={() => show(getValue() as number)} />
          </Group>
        )
      },
    },
  ],
  [],
)
const table = useTable({
  columns,
})
Don't forget to pass the record "id" to show to fetch the record data. This is necessary for both "edit" and "clone" forms.
In this example, we will show you how to "clone" a record with useModalForm.
refine doesn't automatically add a <CloneButton/> to the each record in <PostList> which opens "clone" form in <Modal> when clicked.
So, we have to put the <CloneButton/> on our list. In that way, "clone" form in <Modal> can fetch data by the record id.
const columns = React.useMemo<ColumnDef<IPost>[]>(
  () => [
    // --
    {
      id: 'actions',
      header: 'Actions',
      accessorKey: 'id',
      enableColumnFilter: false,
      enableSorting: false,
      cell: function render({ getValue }) {
        return (
          <Group spacing="xs" noWrap>
            <CloneButton hideText onClick={() => show(getValue() as number)} />
          </Group>
        )
      },
    },
  ],
  [],
)
const table = useTable({
  columns,
})
Don't forget to pass the record "id" to show to fetch the record data. This is necessary for both "edit" and "clone" forms.
Properties
refineCoreProps
All useForm properties also available in useStepsForm. You can find descriptions on useForm docs.
const modalForm = useModalForm({
  refineCoreProps: {
    action: 'edit',
    resource: 'posts',
    id: '1',
  },
})
initialValues
Only available in
"create"form.
Default values for the form. Use this to pre-populate the form with data that needs to be displayed.
const modalForm = useModalForm({
  initialValues: {
    title: 'Hello World',
  },
})
defaultVisible
Default:
false
When true, modal will be visible by default.
const modalForm = useModalForm({
  modalProps: {
    defaultVisible: true,
  },
})
autoSubmitClose
Default:
true
When true, modal will be closed after successful submit.
const modalForm = useModalForm({
  modalProps: {
    autoSubmitClose: false,
  },
})
autoResetForm
Default:
true
When true, form will be reset after successful submit.
const modalForm = useModalForm({
  modalProps: {
    autoResetForm: false,
  },
})
Return Values
All useForm return values also available in useModalForm. You can find descriptions on useForm docs.
All mantine useForm return values also available in useModalForm. You can find descriptions on mantine docs.
visible
Current visibility state of the modal.
const modalForm = useModalForm({
  defaultVisible: true,
})
console.log(modalForm.modal.visible) // true
title
Title of the modal. Based on resource and action values
const {
  modal: { title },
} = useModalForm({
  refineCoreProps: {
    resource: 'posts',
    action: 'create',
  },
})
console.log(title) // "Create Post"
close
A function that can close the modal. It's useful when you want to close the modal manually.
const {
  getInputProps,
  modal: { close, visible, title },
} = useModalForm()
return (
  <Modal opened={visible} onClose={close} title={title}>
    <TextInput
      mt={8}
      label="Title"
      placeholder="Title"
      {...getInputProps('title')}
    />
    <Box mt={8} sx={{ display: 'flex', justifyContent: 'flex-end' }}>
      <SaveButton {...saveButtonProps} />
      <Button onClick={close}>Cancel</Button>
    </Box>
  </Modal>
)
submit
A function that can submit the form. It's useful when you want to submit the form manually.
const {
  modal: { submit },
} = useModalForm()
// ---
return (
  <Modal opened={visible} onClose={close} title={title}>
    <TextInput
      mt={8}
      label="Title"
      placeholder="Title"
      {...getInputProps('title')}
    />
    <Box mt={8} sx={{ display: 'flex', justifyContent: 'flex-end' }}>
      <Button onClick={submit}>Save</Button>
    </Box>
  </Modal>
)
show
A function that can show the modal.
const {
  getInputProps,
  modal: { close, visible, title, show },
} = useModalForm()
const onFinishHandler = (values) => {
  onFinish(values)
  show()
}
return (
  <>
    <Button onClick={}>Show Modal</Button>
    <Modal opened={visible} onClose={close} title={title}>
      <TextInput
        mt={8}
        label="Title"
        placeholder="Title"
        {...getInputProps('title')}
      />
      <Box mt={8} sx={{ display: 'flex', justifyContent: 'flex-end' }}>
        <SaveButton {...saveButtonProps} />
      </Box>
    </Modal>
  </>
)
saveButtonProps
It contains all the props needed by the "submit" button within the modal (disabled,loading etc.). You can manually pass these props to your custom button.
const { getInputProps, modal, saveButtonProps } = useModalForm()
return (
  <Modal {...modal}>
    <TextInput
      mt={8}
      label="Title"
      placeholder="Title"
      {...getInputProps('title')}
    />
    <Box mt={8} sx={{ display: 'flex', justifyContent: 'flex-end' }}>
      <Button
        {...saveButtonProps}
        onClick={(e) => {
          // -- your custom logic
          saveButtonProps.onClick(e)
        }}
      />
    </Box>
  </Modal>
)
API Reference
Properties
| Property | Description | Type | 
|---|---|---|
| modalProps | Configuration object for the modal or drawer | ModalPropsType | 
| refineCoreProps | Configuration object for the core of the useForm | UseFormProps | 
| @mantine/form'suseFormproperties | See useForm documentation | 
ModalPropsType
Property Description Type Default defaultVisible Initial visibility state of the modal booleanfalseautoSubmitClose Whether the form should be submitted when the modal is closed booleantrueautoResetForm Whether the form should be reset when the form is submitted booleantrue
Return values
| Property | Description | Type | 
|---|---|---|
| modal | Relevant states and methods to control the modal or drawer | ModalReturnValues | 
| refineCore | The return values of the useFormin the core | UseFormReturnValues | 
| @mantine/form'suseFormreturn values | See useForm documentation | 
ModalReturnValues
Property Description Type visible State of modal visibility booleanshow Sets the visible state to true (id?: BaseKey) => voidclose Sets the visible state to false () => voidsubmit Submits the form (values: TVariables) => voidtitle Modal title based on resource and action value stringsaveButtonProps Props for a submit button { disabled: boolean, onClick: (e: React.FormEvent<HTMLFormElement>) => void; }
Example
npm create refine-app@latest -- --example form-mantine-use-modal-form