Skip to content

Commit 82db475

Browse files
committed
add example useWatch and re-export react-hook-form API
1 parent 72c6724 commit 82db475

4 files changed

Lines changed: 90 additions & 13 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {FunctionComponent, PropsWithChildren} from "react";
2+
import {AppBar, Box, Button, Toolbar} from "@mui/material";
3+
import Link from "next/link";
4+
5+
export const Layout: FunctionComponent<PropsWithChildren> = ({children}) => {
6+
return (
7+
<Box>
8+
<AppBar>
9+
<Toolbar>
10+
<Link href={'/'} passHref>
11+
<Button color={'inherit'}>
12+
Base
13+
</Button>
14+
</Link>
15+
<Link href={'/withSub'} passHref>
16+
<Button color={'inherit'}>
17+
With useWatch
18+
</Button>
19+
</Link>
20+
</Toolbar>
21+
</AppBar>
22+
<Toolbar/>
23+
<Box padding={3}>
24+
25+
{children}
26+
</Box>
27+
</Box>
28+
)
29+
}

apps/nextjs/src/pages/_app.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import CssBaseline from '@mui/material/CssBaseline'
66
import {CacheProvider, EmotionCache} from '@emotion/react'
77
import theme from '../theme'
88
import createEmotionCache from '../createEmotionCache'
9+
import {Layout} from "../components/Layout";
910

1011
// Client-side cache, shared for the whole session of the user in the browser.
1112
const clientSideEmotionCache = createEmotionCache()
@@ -15,17 +16,19 @@ interface MyAppProps extends AppProps {
1516
}
1617

1718
export default function MyApp(props: MyAppProps) {
18-
const {Component, emotionCache = clientSideEmotionCache, pageProps} = props
19-
return (
20-
<CacheProvider value={emotionCache}>
21-
<Head>
22-
<title>Demo for MUI react-hook-forms</title>
23-
<meta name="viewport" content="initial-scale=1, width=device-width"/>
24-
</Head>
25-
<ThemeProvider theme={theme}>
26-
<CssBaseline/>
27-
<Component {...pageProps} />
28-
</ThemeProvider>
29-
</CacheProvider>
30-
)
19+
const {Component, emotionCache = clientSideEmotionCache, pageProps} = props
20+
return (
21+
<CacheProvider value={emotionCache}>
22+
<Head>
23+
<title>Demo for MUI react-hook-forms</title>
24+
<meta name="viewport" content="initial-scale=1, width=device-width"/>
25+
</Head>
26+
<ThemeProvider theme={theme}>
27+
<CssBaseline/>
28+
<Layout>
29+
<Component {...pageProps} />
30+
</Layout>
31+
</ThemeProvider>
32+
</CacheProvider>
33+
)
3134
}

apps/nextjs/src/pages/withSub.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Button} from "@mui/material";
2+
import {FormContainer, TextFieldElement, useWatch} from "react-hook-form-mui";
3+
4+
const SubComponent = () => {
5+
const [name, email] = useWatch({
6+
name: ["name", "email"]
7+
});
8+
console.log(name, email);
9+
return (
10+
<>
11+
<Button type={"submit"} color={"primary"} disabled={!(name && email)}>
12+
Submit
13+
</Button>
14+
</>
15+
);
16+
};
17+
18+
export default function IndexPage() {
19+
20+
return (
21+
<div>
22+
<FormContainer
23+
defaultValues={{
24+
name: ""
25+
}}
26+
onSuccess={(data) => {
27+
console.log(data)
28+
}}
29+
>
30+
<TextFieldElement name={"name"} label={"Name"} required/> <br/>
31+
<TextFieldElement
32+
name={"email"}
33+
label={"Email"}
34+
required
35+
type={"email"}
36+
/>{" "}
37+
<br/>
38+
<TextFieldElement name={"interest"} label={"Interest"}/> <br/>
39+
<SubComponent/>
40+
</FormContainer>
41+
</div>
42+
);
43+
}

packages/rhf-mui/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ export type {AutocompleteElementProps} from './AutocompleteElement'
3939

4040
export {default as SliderElement} from './SliderElement'
4141
export type {SliderElementProps} from './SliderElement'
42+
43+
export * from 'react-hook-form'

0 commit comments

Comments
 (0)