Edit in place
Edit in place แสดงค่าเป็นข้อความธรรมดาที่แก้ไขได้ตรงที่มันอยู่ ไม่ต้องเปิดฟอร์ม (EditInPlace ของ Carbon for IBM Products) เมื่อ hover จะมีพื้นสีของช่องกรอกและปุ่ม "แก้ไข" เมื่อคลิกหรือ Tab เข้าไป จะกลายเป็นช่องกรอกที่มีปุ่ม "ยกเลิก" และ "บันทึก" ใช้กับค่าเดียวในหน้าที่ส่วนใหญ่ใช้อ่าน เช่น ชื่อสัญญาหรือเลขอ้างอิง
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place label="ชื่อสัญญา" value="สัญญาเช่าคลังสินค้า บางนา"></ui-edit-in-place>
</div>
<UiEditInPlace label="ชื่อสัญญา" value="สัญญาเช่าคลังสินค้า บางนา" />
<EditInPlace label="ชื่อสัญญา" defaultValue="สัญญาเช่าคลังสินค้า บางนา" />
ติดตั้ง
npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui
npx shadcn@4 add @kanok-ui/edit-in-place
npx shadcn@4 add @kanok-ui/tw-edit-in-place
# Web component
# 1. ติดตั้ง dependency
npm install lit@^3.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/edit-in-place.json
# ไปไว้ที่ target ของมัน:
# components/ui/primitives/edit-in-place/edit-in-place.ts
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-edit-in-place, icon-button, popover, shared-element, shared-events, shared-field, shared-localize, shared-position, tokens
# React + Tailwind
# 1. ติดตั้ง dependency
npm install @purpurabog/ui@^2.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-edit-in-place.json
# ไปไว้ที่ target ของมัน:
# components/ui/react-tw/edit-in-place.tsx
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-edit-in-place, core-position, tw-i18n, tw-icon-button, tw-popover, tw-theme, tw-utils
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
label คือชื่อของช่องสำหรับ assistive tech ไม่มี label ที่มองเห็น ข้อความรอบ ๆ ค่าจึงต้องบอกว่าค่านี้คืออะไร
HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/edit-in-place';</script>
<ui-edit-in-place label="ชื่อสัญญา" value="สัญญาเช่าคลังสินค้า"></ui-edit-in-place>
import { UiEditInPlace } from '@purpurabog/ui/react';
<UiEditInPlace label="ชื่อสัญญา" value="สัญญาเช่าคลังสินค้า" />
import { EditInPlace } from '@/components/ui/react-tw/edit-in-place';
<EditInPlace label="ชื่อสัญญา" defaultValue="สัญญาเช่าคลังสินค้า" />
บันทึกและยกเลิก
ui-save เกิดเมื่อบันทึกการเปลี่ยนแปลง: กดบันทึก กด Enter หรือ focus ออกไปพร้อมการเปลี่ยนแปลงที่ถูกต้อง ui-cancel เกิดเมื่อค่าถูกคืนกลับ: กดยกเลิก กด Escape หรือ focus ออกไปโดยไม่มีการเปลี่ยนแปลงที่ถูกต้อง ทั้งคู่ส่งค่ามาใน detail.value ปุ่มบันทึกและยกเลิกกดได้เมื่อค่าเปลี่ยนแล้วเท่านั้น (ช่องว่างหัวท้ายไม่นับ)
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place id="edit-in-place-po" label="เลขที่ใบสั่งซื้อ" value="PO-2026-0815"></ui-edit-in-place>
</div>
<p>เหตุการณ์ล่าสุด: <output id="edit-in-place-po-out"></output></p>
<script type="module">
const field = document.getElementById('edit-in-place-po');
const out = document.getElementById('edit-in-place-po-out');
field.addEventListener('ui-save', (event) => (out.value = `บันทึก "${event.detail.value}"`));
field.addEventListener('ui-cancel', (event) => (out.value = `คืนค่าเป็น "${event.detail.value}"`));
</script>
const [last, setLast] = useState('');
<UiEditInPlace
label="เลขที่ใบสั่งซื้อ"
value="PO-2026-0815"
onUiSave={(e) => setLast(`บันทึก "${e.detail.value}"`)}
onUiCancel={(e) => setLast(`คืนค่าเป็น "${e.detail.value}"`)}
/>
<p>เหตุการณ์ล่าสุด: <output>{last}</output></p>
const [last, setLast] = useState('');
<EditInPlace
label="เลขที่ใบสั่งซื้อ"
defaultValue="PO-2026-0815"
onSave={(value) => setLast(`บันทึก "${value}"`)}
onCancel={(value) => setLast(`คืนค่าเป็น "${value}"`)}
/>
<p>เหตุการณ์ล่าสุด: <output>{last}</output></p>
event input เกิดทุกครั้งที่พิมพ์ โดย value อัปเดตแล้ว ใน React + Tailwind onChange ได้รับค่าใหม่ทุกครั้ง และค่าที่คืนกลับเมื่อยกเลิก
บันทึกไปที่ server
การบันทึกเป็นงานของแอปคุณ: ส่งค่าไป server ใน ui-save ระหว่างนั้น element เก็บค่าที่แก้ไว้ ยกเลิกและ Escape จะคืนเป็นค่าที่บันทึกล่าสุด value ที่คุณตั้งตอนที่ไม่ได้แก้ไขอยู่จะเป็นจุดเริ่มต้นใหม่
บันทึกบน server แล้ว:
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place id="edit-in-place-ref" label="เลขอ้างอิง" value="REF-104"></ui-edit-in-place>
</div>
<p>บันทึกบน server แล้ว: <output id="edit-in-place-ref-out">REF-104</output></p>
<script type="module">
const field = document.getElementById('edit-in-place-ref');
const out = document.getElementById('edit-in-place-ref-out');
field.addEventListener('ui-save', (event) => {
const sent = event.detail.value.trim();
setTimeout(() => (out.value = sent), 400); // คำตอบจาก server
});
</script>
const [reference, setReference] = useState('REF-104');
<UiEditInPlace
label="เลขอ้างอิง"
value={reference}
onUiSave={async (e) => setReference(await saveReference(e.detail.value))}
/>
const [reference, setReference] = useState('REF-104');
<EditInPlace
label="เลขอ้างอิง"
value={reference}
onChange={setReference}
onSave={async (value) => setReference(await saveReference(value))}
/>
saveReference แทนการเรียก server ของคุณเอง value ของ React + Tailwind เป็นแบบ controlled ให้ใช้คู่กับ onChange
ขนาด
size: sm 40px (ค่าเริ่มต้น) md 48px lg 64px ปุ่มเป็นสี่เหลี่ยมจัตุรัสสูงเท่ากัน
HTMLReactReact + Tailwind<div style="max-width: 360px; display: grid; gap: 16px">
<ui-edit-in-place label="เล็ก" value="เล็ก 40px" edit-always-visible></ui-edit-in-place>
<ui-edit-in-place label="กลาง" value="กลาง 48px" size="md" edit-always-visible></ui-edit-in-place>
<ui-edit-in-place label="ใหญ่" value="ใหญ่ 64px" size="lg" edit-always-visible></ui-edit-in-place>
</div>
<UiEditInPlace label="เล็ก" value="เล็ก 40px" editAlwaysVisible />
<UiEditInPlace label="กลาง" value="กลาง 48px" size="md" editAlwaysVisible />
<UiEditInPlace label="ใหญ่" value="ใหญ่ 64px" size="lg" editAlwaysVisible />
<EditInPlace label="เล็ก" defaultValue="เล็ก 40px" editAlwaysVisible />
<EditInPlace label="กลาง" defaultValue="กลาง 48px" size="md" editAlwaysVisible />
<EditInPlace label="ใหญ่" defaultValue="ใหญ่ 64px" size="lg" editAlwaysVisible />
แสดงปุ่มแก้ไขตลอด
โดยปริยายปุ่มแก้ไขแสดงเมื่อ hover เท่านั้น edit-always-visible (editAlwaysVisible) ให้แสดงตลอด คนที่ใช้จอสัมผัสหรือไม่ได้ hover จะเห็นว่าค่านี้แก้ได้
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place label="ศูนย์ต้นทุน" value="CC-4410 ฝ่ายกฎหมาย" edit-always-visible></ui-edit-in-place>
</div>
<UiEditInPlace label="ศูนย์ต้นทุน" value="CC-4410 ฝ่ายกฎหมาย" editAlwaysVisible />
<EditInPlace label="ศูนย์ต้นทุน" defaultValue="CC-4410 ฝ่ายกฎหมาย" editAlwaysVisible />
Placeholder
placeholder แสดงในช่องว่าง สำหรับค่าที่ยังไม่ได้ตั้ง
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place label="รหัสโครงการ" placeholder="เพิ่มรหัสโครงการ" edit-always-visible></ui-edit-in-place>
</div>
<UiEditInPlace label="รหัสโครงการ" placeholder="เพิ่มรหัสโครงการ" editAlwaysVisible />
<EditInPlace label="รหัสโครงการ" placeholder="เพิ่มรหัสโครงการ" editAlwaysVisible />
ค่าไม่ถูกต้อง
invalid วาดกรอบสีแดงและไอคอนเตือน และไม่ให้บันทึก: ปุ่มบันทึกกดไม่ได้ Enter ไม่บันทึก และ focus ออกไปจะคืนค่า invalid-text (invalidText) แสดงใต้ช่องและเป็นคำอธิบายของช่อง ตรวจค่าใน input แล้วตั้ง invalid เอง
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place id="edit-in-place-tax" label="เลขประจำตัวผู้เสียภาษี" value="0105556012345" invalid-text="เลขประจำตัวผู้เสียภาษีมี 13 หลัก"></ui-edit-in-place>
</div>
<script type="module">
const field = document.getElementById('edit-in-place-tax');
const check = () => (field.invalid = !/^\d{13}$/.test(field.value));
field.addEventListener('input', check);
field.addEventListener('ui-cancel', check);
</script>
const [taxId, setTaxId] = useState('0105556012345');
<UiEditInPlace
label="เลขประจำตัวผู้เสียภาษี"
value={taxId}
invalid={!/^\d{13}$/.test(taxId)}
invalidText="เลขประจำตัวผู้เสียภาษีมี 13 หลัก"
onInput={(e) => setTaxId((e.target as HTMLInputElement).value)}
onUiCancel={(e) => setTaxId(e.detail.value)}
/>
const [taxId, setTaxId] = useState('0105556012345');
<EditInPlace label="เลขประจำตัวผู้เสียภาษี" value={taxId} onChange={setTaxId} invalid={!/^\d{13}$/.test(taxId)} invalidText="เลขประจำตัวผู้เสียภาษีมี 13 หลัก" />
ลองลบตัวเลขออกหนึ่งตัวเพื่อดู error การยกเลิกคืนค่าโดยไม่เกิด event input จึงต้องตรวจอีกครั้งใน ui-cancel ส่วน onChange ของ React + Tailwind ได้รับค่าที่คืนกลับ
อ่านอย่างเดียว
readonly (readOnly) แสดงค่าพร้อมปุ่ม "แก้ไขไม่ได้" แทนปุ่ม "แก้ไข" คลิกที่ช่องหรือที่ปุ่มจะเปิดข้อความสั้น ๆ ว่าแก้ไขไม่ได้ readonly-text (readOnlyText) บอกเหตุผลด้วยคำของคุณเอง
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place label="เลขที่สัญญา" value="CT-2026-0142" readonly readonly-text="เลขที่สัญญากำหนดตอนลงนามสัญญา"></ui-edit-in-place>
</div>
<UiEditInPlace label="เลขที่สัญญา" value="CT-2026-0142" readonly readonlyText="เลขที่สัญญากำหนดตอนลงนามสัญญา" />
<EditInPlace label="เลขที่สัญญา" defaultValue="CT-2026-0142" readOnly readOnlyText="เลขที่สัญญากำหนดตอนลงนามสัญญา" />
หัวข้อที่แก้ไขได้
inherit-typography (inheritTypography) ให้ช่องกรอกใช้ขนาดและน้ำหนักตัวอักษรของข้อความรอบ ๆ ชื่อหน้าจึงแก้ได้ในรูปเดิม
HTMLReactReact + Tailwind<div style="max-width: 480px; font: var(--ui-font-heading-03)">
<ui-edit-in-place label="ชื่อรายงาน" value="รายงานสัญญารายไตรมาส" size="md" inherit-typography></ui-edit-in-place>
</div>
<div style={{ maxWidth: 480, font: 'var(--ui-font-heading-03)' }}>
<UiEditInPlace label="ชื่อรายงาน" value="รายงานสัญญารายไตรมาส" size="md" inheritTypography />
</div>
<div className="max-w-120 type-heading-03">
<EditInPlace label="ชื่อรายงาน" defaultValue="รายงานสัญญารายไตรมาส" size="md" inheritTypography />
</div>
ในหน้าจริง ให้ใส่ไว้ใน element หัวข้อ (<h2>) เลย หัวข้อจะได้คงระดับไว้
ชื่อปุ่มมาจากภาษาของหน้า ("แก้ไข" "บันทึก" "ยกเลิก" "แก้ไขไม่ได้") เปลี่ยนได้ด้วย edit-label save-label cancel-label และ readonly-label (editLabel saveLabel cancelLabel readOnlyLabel ใน React + Tailwind) tooltip-placement ย้าย tooltip ของปุ่ม (ค่าเริ่มต้นด้านบน) และ note-placement ย้ายข้อความของโหมดอ่านอย่างเดียว (ค่าเริ่มต้นด้านล่าง)
HTMLReactReact + Tailwind<div style="max-width: 360px">
<ui-edit-in-place label="ชื่อผู้ขาย" value="สยามโลจิสติกส์" save-label="บันทึกชื่อผู้ขาย" cancel-label="ย้อนการแก้ไข" tooltip-placement="bottom" edit-always-visible></ui-edit-in-place>
</div>
<UiEditInPlace label="ชื่อผู้ขาย" value="สยามโลจิสติกส์" saveLabel="บันทึกชื่อผู้ขาย" cancelLabel="ย้อนการแก้ไข" tooltipPlacement="bottom" editAlwaysVisible />
<EditInPlace label="ชื่อผู้ขาย" defaultValue="สยามโลจิสติกส์" saveLabel="บันทึกชื่อผู้ขาย" cancelLabel="ย้อนการแก้ไข" tooltipPlacement="bottom" editAlwaysVisible />
แต่ง style
part มี base (กล่องของช่อง) input actions (กล่องของปุ่ม) และ invalid-text host มี data-editing ตอนที่เป็นช่องกรอก
HTMLReactReact + Tailwind<style>
.edit-in-place-styled::part(input) { font-weight: 600; }
.edit-in-place-styled[data-editing]::part(base) { background: var(--ui-field-2); }
</style>
<div style="max-width: 360px">
<ui-edit-in-place class="edit-in-place-styled" label="ผู้รับผิดชอบ" value="มาลี ส."></ui-edit-in-place>
</div>
// .edit-in-place-styled::part(input) { font-weight: 600; }
// .edit-in-place-styled[data-editing]::part(base) { background: var(--ui-field-2); }
<UiEditInPlace className="edit-in-place-styled" label="ผู้รับผิดชอบ" value="มาลี ส." />
// หน้าตาอยู่ใน components/ui/react-tw/edit-in-place.tsx: แก้ class ในไฟล์นั้น
// หรือสร้างหน้าตาของคุณเองบน Headless parts
<EditInPlace className="max-w-90" label="ผู้รับผิดชอบ" defaultValue="มาลี ส." />
className ใน React + Tailwind อยู่ที่กล่องด้านนอก ที่ครอบช่องกรอกและข้อความ error
คีย์บอร์ด
| ปุ่ม |
ระหว่างแก้ไข |
| Tab |
เข้าไปในช่อง ซึ่งเริ่มการแก้ไข จากนั้นไปที่ยกเลิกและบันทึกเมื่อกดได้แล้ว แล้วออกไป (ออกไปพร้อมการเปลี่ยนแปลงที่ถูกต้องจะบันทึก ไม่เช่นนั้นจะคืนค่า) |
| Enter |
บันทึกการเปลี่ยนแปลงที่ถูกต้องและแก้ไขต่อ ถ้าไม่มีอะไรให้บันทึก จะหยุดแก้ไข โดย focus ยังอยู่ในช่อง |
| Escape |
คืนค่าและแก้ไขต่อ |
Accessibility
- ช่องเป็น text box ที่มีชื่อจาก
label ให้ตั้ง label ที่บอกชื่อค่า ("ชื่อสัญญา") เพราะไม่มีอะไรที่มองเห็นบอกไว้
invalid ตั้ง aria-invalid และ invalid-text เป็นคำอธิบายของช่อง จึงถูกอ่านไปด้วย
- ปุ่มมีชื่อและ tooltip การกดบันทึกหรือยกเลิก focus ยังคงอยู่ในช่อง
- ออกจากช่องพร้อมการเปลี่ยนแปลงที่ถูกต้องจะบันทึกโดยไม่ต้องคลิก ถ้าอาจทำให้ผู้ใช้แปลกใจ ให้บอกไว้ในคำอธิบายของหน้า หรือใช้ text input ปกติกับปุ่มบันทึกสำหรับค่าที่มีผลตามมา
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Edit in place
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-edit-in-place> · React UiEditInPlace
Attribute
| Attribute |
Property |
ชนิด |
ค่าเริ่มต้น |
คำอธิบาย |
value |
value |
string |
'' |
The value (as <input>.value). |
label |
label |
string |
'' |
The field's accessible name (Carbon labelText; there is no visible label). |
placeholder |
placeholder |
string |
'' |
|
size |
size |
EditInPlaceSize |
'sm' |
|
invalid |
invalid |
boolean |
false |
|
invalid-text |
invalidText |
string |
'' |
|
readonly |
readonly |
boolean |
false |
|
edit-always-visible |
editAlwaysVisible |
boolean |
false |
Show "Edit" without hovering. |
inherit-typography |
inheritTypography |
boolean |
false |
The field takes the surrounding type (a heading made editable). |
tooltip-placement |
tooltipPlacement |
Placement |
'top' |
Where the buttons' tooltips go (Carbon tooltipAlignment). |
note-placement |
notePlacement |
Placement |
'bottom' |
Where the read-only note goes (Carbon toggleTipAlignment). |
edit-label |
editLabel |
string |
'' |
The buttons' names and the read-only note (defaults: the translation's "Edit", "Save", "Cancel", "Edit off", …). |
save-label |
saveLabel |
string |
'' |
|
cancel-label |
cancelLabel |
string |
'' |
|
readonly-label |
readonlyLabel |
string |
'' |
|
readonly-text |
readonlyText |
string |
'' |
|
Event
| Event |
React |
คำอธิบาย |
type |
ไม่มี (ใช้ ref) |
|
input |
onInput |
On every keystroke (value already updated) |
ui-save |
onUiSave |
detail: { value } a change was saved (Save, Enter, leaving it) |
ui-cancel |
onUiCancel |
detail: { value } the value was put back (Cancel, Escape, leaving it without a valid change) |
CSS part
| Part |
คำอธิบาย |
base |
The field box |
input |
The <input> |
actions |
The buttons' box |
invalid-text |
The error message |