-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add CodeSandbox demo to Modular Forms guide (#4095)
* Add CodeSandbox demo to Modular Forms guide * Sync CodeSandbox demo code of other guides * Add ts-nocheck to allow unused variables in code * Fix CodeSandbox demo of deep store and sync docs
- Loading branch information
1 parent
3daee3d
commit e00340d
Showing
9 changed files
with
238 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/docs/src/routes/demo/integration/modular-forms/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @ts-nocheck | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { $, component$ } from '@builder.io/qwik'; | ||
import { routeLoader$, z } from '@builder.io/qwik-city'; | ||
import type { InitialValues, SubmitHandler } from '@modular-forms/qwik'; | ||
import { formAction$, useForm, zodForm$ } from '@modular-forms/qwik'; | ||
|
||
const loginSchema = z.object({ | ||
email: z | ||
.string() | ||
.min(1, 'Please enter your email.') | ||
.email('The email address is badly formatted.'), | ||
password: z | ||
.string() | ||
.min(1, 'Please enter your password.') | ||
.min(8, 'You password must have 8 characters or more.'), | ||
}); | ||
|
||
type LoginForm = z.infer<typeof loginSchema>; | ||
|
||
export const useFormLoader = routeLoader$<InitialValues<LoginForm>>(() => ({ | ||
email: '', | ||
password: '', | ||
})); | ||
|
||
export const useFormAction = formAction$<LoginForm>((values) => { | ||
// Runs on server | ||
}, zodForm$(loginSchema)); | ||
|
||
export default component$(() => { | ||
const [loginForm, { Form, Field }] = useForm<LoginForm>({ | ||
loader: useFormLoader(), | ||
action: useFormAction(), | ||
validate: zodForm$(loginSchema), | ||
}); | ||
|
||
const handleSubmit: SubmitHandler<LoginForm> = $((values, event) => { | ||
// Runs on client | ||
console.log(values); | ||
}); | ||
|
||
return ( | ||
<Form onSubmit$={handleSubmit}> | ||
<Field name="email"> | ||
{(field, props) => ( | ||
<div> | ||
<input {...props} type="email" value={field.value} /> | ||
{field.error && <div>{field.error}</div>} | ||
</div> | ||
)} | ||
</Field> | ||
<Field name="password"> | ||
{(field, props) => ( | ||
<div> | ||
<input {...props} type="password" value={field.value} /> | ||
{field.error && <div>{field.error}</div>} | ||
</div> | ||
)} | ||
</Field> | ||
<button type="submit">Login</button> | ||
</Form> | ||
); | ||
}); |
31 changes: 23 additions & 8 deletions
31
packages/docs/src/routes/demo/state/counter-store-deep/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,38 @@ | ||
import { component$, useStore } from '@builder.io/qwik'; | ||
|
||
export default component$(() => { | ||
const store = useStore( | ||
{ | ||
nested: { fields: { are: 'not tracked' } }, | ||
const store = useStore({ | ||
nested: { | ||
fields: { are: 'also tracked' }, | ||
}, | ||
{ deep: true } | ||
); | ||
list: ['Item 1'], | ||
}); | ||
|
||
return ( | ||
<> | ||
<p>{store.nested.fields.are}</p> | ||
<button onClick$={() => (store.nested.fields.are = 'tracked')}> | ||
<button | ||
onClick$={() => { | ||
// Even though we are mutating a nested object, this will trigger a re-render | ||
store.nested.fields.are = 'tracked'; | ||
}} | ||
> | ||
Clicking me works because store is deep watched | ||
</button> | ||
<br /> | ||
<button onClick$={() => (store.nested = { fields: { are: 'tracked' } })}> | ||
Click me still works | ||
<button | ||
onClick$={() => { | ||
// Because store is deep watched, this will trigger a re-render | ||
store.list.push(`Item ${store.list.length}`); | ||
}} | ||
> | ||
Add to list | ||
</button> | ||
<ul> | ||
{store.list.map((item) => ( | ||
<li>{item}</li> | ||
))} | ||
</ul> | ||
</> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.