Piplup
@piplup/rhf-adaptersmui-materialComponents

MuiSwitchElement

A pre-integrated MuiSwitchElement component for connecting MUI Switch to React Hook Form.

Import

import { MuiSwitchElement } from "@piplup/rhf-adapters/mui-material";

Usage

"use client";import FormControlLabel from "@mui/material/FormControlLabel";import Stack from "@mui/material/Stack";import {  MuiSwitchElement,  MuiButtonElement,} from "@piplup/rhf-adapters/mui-material";import { useForm } from "react-hook-form";export default function Page() {  const { control, handleSubmit } = useForm({    defaultValues: { newsletter: false },  });  return (    <form      onSubmit={handleSubmit((values) =>        alert(JSON.stringify(values, null, 2)),      )}    >      <Stack direction="column" spacing={2}>        <FormControlLabel          control={<MuiSwitchElement control={control} name="newsletter" />}          label="Newsletter"        />        <div>          <MuiButtonElement control={control} variant="contained" type="submit">            Submit          </MuiButtonElement>        </div>      </Stack>    </form>  );}
"use client";import FormControlLabel from "@mui/material/FormControlLabel";import Stack from "@mui/material/Stack";import {  MuiSwitchElement,  MuiButtonElement,} from "@piplup/rhf-adapters/mui-material";import { FormContainer } from "@piplup/rhf-core";export default function Page() {  return (    <FormContainer      defaultValues={{ newsletter: false }}      onSubmit={(values) => alert(JSON.stringify(values, null, 2))}    >      <Stack direction="column" spacing={2}>        <FormControlLabel          control={<MuiSwitchElement name="newsletter" />}          label="Newsletter"        />        <div>          <MuiButtonElement variant="contained" type="submit">            Submit          </MuiButtonElement>        </div>      </Stack>    </FormContainer>  );}

Props

Prop

Type

MuiSwitchElement supports props of the MUI Switch component, along with additional adapter-specific props. For a complete list of available Switch properties, refer to the MUI API documentation.

The ref is forwarded to the underlying <Switch /> component.

Notes

  • For custom switch UIs, build on useMuiSwitchAdapter.

See also

On this page