Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Form example improvement and removed unused type references #1184

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions examples/36_form/src/components/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/// <reference types="react/canary" />
/// <reference types="react-dom/canary" />

'use client';

import { useActionState } from 'react';
Expand Down
3 changes: 0 additions & 3 deletions examples/36_form/src/components/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/// <reference types="react/canary" />
/// <reference types="react-dom/canary" />

'use client';

import { useFormStatus } from 'react-dom';
Expand Down
75 changes: 55 additions & 20 deletions examples/36_form/src/components/ServerForm.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
async function requestUsername(formData: FormData) {
async function submitUserProfile(formData: FormData) {
'use server';
const username = formData.get('username');
console.log(`username: ${username}`);
const name = formData.get('name');
const age = formData.get('age');
const favoriteColor = formData.get('favoriteColor');
const hobby = formData.get('hobby');
const isSubscribed = formData.get('newsletter') === 'on';

console.log({
name,
age,
favoriteColor,
hobby,
isSubscribed,
});
}

// FIXME make this example more realistic
export const ServerForm = () => {
return (
<>
<form action={requestUsername}>
<input type="text" name="username" />
<button type="submit">Request</button>
</form>
<form
action={async (formData: FormData) => {
'use server';
const hobby = formData.get('hobby');
console.log(`hobby: ${hobby}`);
}}
>
<input type="text" name="hobby" />
<button type="submit">Request</button>
</form>
</>
<form action={submitUserProfile} className="space-y-4">
<div style={{ display: 'flex', gap: 4, marginBottom: 4 }}>
<label htmlFor="name">Full Name</label>
<input type="text" name="name" id="name" required />
</div>

<div style={{ display: 'flex', gap: 4, marginBottom: 4 }}>
<label htmlFor="age">Age</label>
<input type="number" name="age" id="age" min="13" max="120" />
</div>

<div style={{ display: 'flex', gap: 4, marginBottom: 4 }}>
<label htmlFor="favoriteColor">Favorite Color</label>
<select name="favoriteColor" id="favoriteColor">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="purple">Purple</option>
<option value="yellow">Yellow</option>
</select>
</div>

<div style={{ display: 'flex', gap: 4, marginBottom: 4 }}>
<label htmlFor="hobby">Favorite Hobby</label>
<input
type="text"
name="hobby"
id="hobby"
placeholder="e.g. Reading, Gaming, Cooking"
/>
</div>

<div style={{ display: 'flex', gap: 4, marginBottom: 4 }}>
<label>
<input type="checkbox" name="newsletter" />
Subscribe to newsletter
</label>
</div>

<button type="submit">Save Profile</button>
</form>
);
};
Loading