Summary
ใช้ skill นี้เมื่อสร้าง form ใน React ด้วย Formik, ต้องการ validation, ปรับแต่ง MUI component ให้ใช้กับ Formik, หรือถามเรื่องการจัดการ form submission ตามมาตรฐาน SSD
pratchaya0/ssd-agent-skills · Archived
ใช้ skill นี้เมื่อสร้าง form ใน React ด้วย Formik, ต้องการ validation, ปรับแต่ง MUI component ให้ใช้กับ Formik, หรือถามเรื่องการจัดการ form submission ตามมาตรฐาน SSD
npx skills add pratchaya0/ssd-agent-skills --skill ssd-react-form
ใช้ skill นี้เมื่อสร้าง form ใน React ด้วย Formik, ต้องการ validation, ปรับแต่ง MUI component ให้ใช้กับ Formik, หรือถามเรื่องการจัดการ form submission ตามมาตรฐาน SSD
This repository is archived — consider an actively maintained alternative.
มาตรฐานการพัฒนาซอฟต์แวร์ของทีม SSD: React/TypeScript, .NET Core, Python, Git — ใช้เมื่อเขียนโค้…
37 installsใช้ skill นี้เมื่อใช้งาน Redux ใน React, สร้าง slice, ตั้งค่า rootReducer, หรือ dispatch/subscr…
2 installsใช้ skill นี้เมื่อเขียน React component, กำหนด Props type, ใช้ Refs/Fragments, ตั้งชื่อ compone…
2 installsใช้ skill นี้เมื่อเรียก API ด้วย Axios ใน React, สร้าง React Query hook, ใช้ NSwag สร้าง API cl…
2 installsRelated neighbors and high-traction skills in the same topics — useful to compare before installing.
Browser automation CLI for AI agents. Use when the user needs to interact with websites, includ…
810.4K installsReview UI code for Web Interface Guidelines compliance. Use when asked to "review my UI", "chec…
617.3K installsOther skills from pratchaya0/ssd-agent-skills.
npx skills add pratchaya0/ssd-agent-skills
Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.
main
Parsed from SKILL.md frontmatter.
Files included with this skill beyond the listing page.
SKILL.md
6,823 B
SUMMARY.md
343 B
SSD ใช้ Formik เป็น library หลักสำหรับจัดการ form ใน React โดยมี MUI (Material UI) Components ที่ถูกปรับแต่งให้ใช้งานร่วมกับ Formik แล้วใน Template ที่ src/app/modules/_common/components/CustomFormik สามารถใช้ได้เลย และหากต้องสร้าง custom form component ใหม่ต้องปฏิบัติตามรูปแบบที่กำหนด
enableReinitialize: true เสมอเมื่อดึงข้อมูลเริ่มต้นจาก APIsrc/app/modules/components เพื่อเป็น shared component// SimpleForm.tsx
import React from "react";
import { useFormik, FormikErrors } from "formik";
// ต้อง export type ออกมาเพื่อให้ component อื่นใช้ได้
export type SimpleFormValues = {
name: string;
email: string;
};
// ใช้ react-query ในการดึงข้อมูลจาก api
const { data: user } = useGetUser('1');
const initialValues: SimpleFormValues = {
name: '',
email: '',
};
const formik = useFormik<SimpleFormValues>({
// กำหนดค่าเริ่มต้น — ถ้าดึงจาก API ให้ใช้ user || initialValues
initialValues: user || initialValues,
// ต้องใส่เสมอเมื่อ initialValues มาจาก API
enableReinitialize: true,
// validate function
validate: (values) => {
const errors: FormikErrors<SimpleFormValues> = {};
if (!values.name) {
errors.name = 'กรุณากรอกชื่อ';
}
return errors;
},
onSubmit: (values) => {
// ส่งข้อมูล
console.log(values);
},
});
return (
<div>
<Typography>ชื่อ</Typography>
<FormikTextField name="name" formik={formik} fullWidth />
<Typography>Email</Typography>
<FormikTextField name="email" formik={formik} fullWidth />
<Button onClick={() => formik.handleSubmit()}>บันทึก</Button>
<Button onClick={() => formik.handleReset()}>ยกเลิก</Button>
</div>
);
เมื่อ validate logic ยาวหรือซับซ้อน ให้แยกเป็น function ภายนอก:
const validate = (values: SimpleFormValues) => {
const errors: FormikErrors<SimpleFormValues> = {};
if (!values.name) {
errors.name = 'กรุณากรอกชื่อ';
} else if (values.name.length > 15) {
errors.name = 'ชื่อต้องไม่เกิน 15 ตัวอักษร';
}
if (!values.email) {
errors.email = 'กรุณากรอก Email';
} else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email)) {
errors.email = 'รูปแบบ Email ไม่ถูกต้อง';
}
return errors;
};
กรณีที่ MUI Component ใน Template ไม่ตอบโจทย์ ให้สร้าง custom component ดังนี้:
สร้างไฟล์ใหม่ที่ src/app/modules/components/
Component ต้องรับ props ต่อไปนี้:
name: string — ชื่อ field ใน Formikformik: FormikProps<T> — ตัวแปร formik จาก useFormikimport { FormikProps } from 'formik';
import { TextField } from '@mui/material';
type CustomTextFieldProps = {
name: string;
formik: FormikProps<any>;
label?: string;
fullWidth?: boolean;
};
const CustomTextField = ({ name, formik, label, fullWidth }: CustomTextFieldProps) => {
// รับค่าด้วย getFieldMeta และ getFieldProps
const fieldProps = formik.getFieldProps(name);
const { touched, error } = formik.getFieldMeta(name);
return (
<TextField
{...fieldProps}
// ต้องตั้ง name และ id เพื่อให้ Robot ค้นหาได้
name={name}
id={name}
label={label}
fullWidth={fullWidth}
error={touched && Boolean(error)}
helperText={touched && error}
onChange={(e) => formik.setFieldValue(name, e.target.value)}
/>
);
};
ดู Component ที่ปรับแต่งแล้วได้ที่ src/app/modules/_common/components/CustomFormik:
FormikTextField — text inputProvinceSelect — select จังหวัดEmployeeAutoComplete — autocomplete พนักงานInterestCheckboxGroup — checkbox groupGenderRadioGroup — radio groupดูตัวอย่างเพิ่มเติมได้ที่ Demo Pos ของ SSD