Skip to content

Commit

Permalink
docs: Form example improvement and removed unused type references (#1184
Browse files Browse the repository at this point in the history
)

fixing the FIXME for the server form with a more robust form example

also removed the type references since they didn't seem to do anything

Co-authored-by: Tyler <[email protected]>
  • Loading branch information
tylersayshi and tylersayshi authored Jan 23, 2025
1 parent a2ed534 commit a04d79f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
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>
);
};

0 comments on commit a04d79f

Please sign in to comment.