在本指南中,我們將逐步使用 @perseid/form 庫建立動態使用者回饋表單,這是 Formik 和 React Hook Form 的強大替代品。您將看到 @perseid/form 如何輕鬆管理表單狀態、驗證和條件渲染。我們將建立的表單將要求用戶對服務進行評分並提供回饋。根據評級,它會顯示“謝謝”訊息或提示用戶提供其他回饋。
?讓我們開始吧!
第 1 步:設定表單配置
第一步是定義表單配置。此配置概述了表單的行為方式,包括欄位、步驟以及它們之間的流程。在這裡,我們將根據使用者的評分使用 條件邏輯 建立評分和評論欄位。我們還將定義正面和負面回饋的訊息。
這是設定碼:
import { type Configuration } from "@perseid/form"; const formConfiguration: Configuration = { // Root step-the form will start from there. root: "feedback", // Callback triggered on form submission. onSubmit(data) { alert(`Submitting the following JSON: ${JSON.stringify(data)}`); return Promise.resolve(); }, // `fields` define the data model the form is going to deal with. // Expect the submitted data JSON to match this schema. fields: { rating: { type: "integer", required: true, }, review: { type: "string", required: true, // Display this field only if condition is met... condition: (inputs) => inputs.rating !== null && (inputs.rating as number) (inputs.rating as number) <p>在此配置:</p>
- 表單從回饋步驟開始。
- 該表單包含兩個欄位:評級(必填)和評論(可選,除非評級低於 3)。
- 根據評級,表單導航至「好」或「壞」回饋訊息。
- 提交表單後,提交的資料會觸發一個簡單的警報。
這裡要掌握的重點是fields屬性的作用。它定義了將提交的資料的結構,本質上充當資料模型。相較之下,steps 屬性概述了表單的流程,決定如何將這些欄位呈現給使用者。
第 2 步:建立表單的 React 元件
現在我們已經有了配置,是時候建立將呈現表單的實際 UI 了。使用@perseid/form/react,我們可以建立自訂欄位元件來管理表單每個部分的使用者互動。
這是核心 React 元件:
import React from "react"; import Form, { type FormFieldProps } from "@perseid/form/react"; // The actual React component, used to build the UI! function Field(props: FormFieldProps): JSX.Element { const { path, engine, value, status } = props; const [currentRating, setCurrentRating] = React.useState(0); // Display a different element depending on the field... if (path === "thanks_good.1.message_good") { return ( <div classname="message"> <h1 id="Thanks-for-the-feedback">Thanks for the feedback ?</h1> <p>We are glad you enjoyed!</p> </div> ); } if (path === "thanks_bad.1.message_bad") { return ( <div classname="message"> <h1 id="We-re-sorry-to-hear-that">We're sorry to hear that ?</h1> <p>We'll do better next time, promise!</p> </div> ); } if (path === "feedback.0.rating") { return ( // Depending on the field status, define some extra classes for styling... <div classname="{`rating" : onmouseleave="{()"> { setCurrentRating((value as number | null) ?? 0); }} > <h1 id="How-would-you-rate-our-service">How would you rate our service?</h1> {[1, 2, 3, 4, 5].map((rating) => ( <span key="{rating}" classname="{`rating__star" currentrating>= rating ? "rating__star--active" : "" }`} onMouseEnter={() => { setCurrentRating(rating); }} onClick={() => { // On click, notify the form engine about new user input. engine.userAction({ type: "input", path, data: rating }); }} ></span> ))} </div> ); } if (path === "feedback.0.review") { return ( <div classname="{`review" :> <label>Could you tell us more?</label> <textarea onchange="{(e)"> engine.userAction({ type: "input", path, data: e.target.value }) } /> </textarea> </div> ); } // path === 'feedback.0.submit' return ( <button classname="submit" onclick="{()"> { engine.userAction({ type: "input", path, data: true }); }} > Submit </button> ); }
這裡,Field 元件使用 path 屬性決定要渲染什麼:
- 一個評級組件,使用者可以在其中選擇星級。
- 供使用者提供額外回饋的文字區域。
根據評分顯示的「謝謝」訊息。此表單將根據使用者輸入動態調整其欄位和步驟。
很酷,對吧?
第 3 步:運行應用程式
現在我們的表單配置和元件已準備就緒,讓我們將它們整合到一個基本的 React 應用程式中。這是初始化和渲染表單的程式碼:
import { createRoot, type Root } from "react-dom/client"; // Let's run the app! let app: Root; // Creating React root... const container = document.querySelector("#root") as unknown as HTMLElement; app = createRoot(container); app.render( // Router is the main component for any Perseid app.);
此程式碼使用 React 的 createRoot API 將表單安裝到 DOM。 Form 元件連接我們的配置和 Field 元件,處理其他所有事情。
第四步:新增樣式
好吧,我們有了應用程式邏輯,但是如果您現在運行程式碼,您會發現它有點......原始?
所以,讓我們透過添加一些樣式和動畫來修飾表單!下面是一個簡單的樣式表,使它更具吸引力:
// A few animations for fun... @keyframes swipe-out { 0% { opacity: 1; transform: translateX(0); } 75% { opacity: 0; transform: translateX(-100%); } 100% { opacity: 0; transform: translateX(-100%); } } @keyframes swipe-in-one { 0% { opacity: 0; transform: translateX(100%); } 75% { transform: translateX(0); } 100% { opacity: 1; transform: translateX(0); } } @keyframes swipe-in-two { 0% { opacity: 0; transform: translateX(0); } 75% { transform: translateX(-100%); } 100% { opacity: 1; transform: translateX(-100%); } } @keyframes bubble-in { 0% { transform: scale(0.5); } 75% { transform: scale(1.5); } 100% { transform: scale(1); } } @keyframes fade-in { 0% { opacity: 0; } 100% { opacity: 1; } } // Some global basic styling... * { box-sizing: border-box; } body { margin: 0; display: grid; height: 100vh; color: #aaaaaa; align-items: center; font-family: "Helvetica", sans-serif; } // And form-specific styling. .perseid-form { width: 100%; margin: auto; &__steps { display: flex; overflow: hidden; } &__step { min-width: 100%; padding: 1rem 3rem; animation: 500ms ease-in-out forwards swipe-out; &__fields { display: grid; row-gap: 2rem; } } &__step[class*="active"]:first-child { animation: 500ms ease-in-out forwards swipe-in-one; } &__step[class*="active"]:last-child:not(:first-child) { animation: 500ms ease-in-out forwards swipe-in-two; } } .submit { border: none; cursor: pointer; padding: 1rem 2rem; border-radius: 8px; color: #fefefe; font-size: 1.25rem; background: #46c0b0; justify-self: flex-end; transition: all 250ms ease-in-out; &:hover { background: #4cccbb; } } .rating { position: relative; padding: 0.25rem 0; &__star { cursor: pointer; display: inline-block; font-size: 2rem; min-width: 2rem; min-height: 2rem; &::after { content: "⚪️"; } &--active { animation: 250ms ease-in-out forwards bubble-in; &::after { content: "?"; } } } &[class*="error"] { &::after { left: 0; bottom: -1.5rem; color: #f13232; position: absolute; font-size: 0.75rem; content: "? This field is required"; animation: 250ms ease-in-out forwards fade-in; } } } .review { display: grid; row-gap: 1rem; position: relative; animation: 250ms ease-in-out forwards fade-in; label { font-size: 1.25rem; } textarea { resize: none; min-height: 5rem; border-radius: 8px; border: 1px solid #46c0b0; transition: all 250ms ease-in-out; } &[class*="error"] { &::after { left: 0; bottom: -1.5rem; color: #f13232; position: absolute; font-size: 0.75rem; content: "? This field is required"; animation: 250ms ease-in-out forwards fade-in; } } } @media screen and (min-width: 30rem) { .perseid-form { max-width: 30rem; } }
瞧?
結論
恭喜! ?您剛剛使用 Perseid 和 React 建立了一個動態使用者回饋表單。
在本教學中,我們介紹如何:
- 使用條件邏輯定義表單配置。
- 建立自訂 React 元件來處理使用者互動。
- 在您的應用程式中渲染表單並使用動畫和自訂 CSS 對其進行樣式設定。
請隨意嘗試其他欄位和步驟以適合您的用例。享受建立精彩表單的樂趣! ?
- ?更多範例
- ✅ 完整的文檔
- ?加入我們的不和諧
- ?在 GitHub 上為專案加註星標
- ❤️贊助英仙座
以上是使用 Perseid 和 React 建立使用者回饋表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!