المكوّنات · تحرير داخل الخلية
تحرير داخل الخلية
يحفظ بالإدخال، ويتراجع بالهروب، ويعيد الحساب فورًا
12 تصاميم من هذه العائلة
<div class="tb tbd">
<div class="head"><h4 data-ar="المخزون" data-en="Inventory">المخزون</h4>
<span class="cnt"></span></div>
<div class="wrap"><table>
<thead><tr>
<th scope="col" data-ar="الصنف" data-en="Item">الصنف</th>
<th scope="col" class="e" data-ar="الكمّية" data-en="Qty">الكمّية</th>
<th scope="col" class="e" data-ar="سعر الوحدة" data-en="Unit price">سعر الوحدة</th>
<th scope="col" class="e" data-ar="القيمة" data-en="Value">القيمة</th>
</tr></thead>
<tbody></tbody>
<tfoot><tr><td colspan="3" data-ar="قيمة المخزون" data-en="Stock value">قيمة المخزون</td>
<td class="e sum"></td></tr></tfoot>
</table></div>
<p class="hint" data-ar="اضغط أي رقم لتحريره · Enter يحفظ · Esc يتراجع" data-en="Click a number to edit · Enter saves · Esc reverts">اضغط أي رقم لتحريره · Enter يحفظ · Esc يتراجع</p>
</div>
/* pt-scoped */
/* table-edit — إدارة مخزون */
.tbd{width:100%;height:100%;display:flex;flex-direction:column;gap:6px;
font-size:10px;color:var(--pt-text-secondary);overflow:hidden;padding:9px 10px}
.tbd .wrap{flex:1;min-height:0;overflow:auto;border-radius:var(--pt-radius-sm);
border:1px solid var(--pt-border-subtle)}
.tbd table{width:100%;border-collapse:collapse;font-size:8.5px}
.tbd th, .tbd td{padding:5px 7px;text-align:start;white-space:nowrap}
.tbd thead th{position:sticky;inset-block-start:0;z-index:2;background:var(--pt-bg-inset);
color:var(--pt-text-muted);font-weight:500;font-size:8px;
border-bottom:1px solid var(--pt-border-default)}
.tbd tbody tr{border-bottom:1px solid var(--pt-border-subtle)}
.tbd tbody tr:last-child{border-bottom:0}
.tbd tbody tr:hover{background:var(--pt-surface-hover)}
.tbd tbody td{color:var(--pt-text-secondary)}
.tbd .e{text-align:end;font-variant-numeric:tabular-nums}
.tbd .head{display:flex;align-items:center;gap:7px;flex-wrap:wrap}
.tbd .head h4{margin:0;font-size:10px;color:var(--pt-text-primary);font-weight:600}
.tbd .cnt{font-size:8px;color:var(--pt-text-muted);font-variant-numeric:tabular-nums;
margin-inline-start:auto}
.tbd button{font:inherit;cursor:pointer}
.tbd button:focus-visible, .tbd input:focus-visible, .tbd select:focus-visible, .tbd th:focus-visible{outline:2px solid var(--pt-focus-ring);outline-offset:-2px}
.tbd .empty{padding:12px;text-align:center;font-size:9px;color:var(--pt-text-muted)}
/* شخصية: خلايا قابلة للنقر، حقل يحلّ محلّها، انحناء صغير */
.tbd .ed{cursor:text;border-radius:var(--pt-radius-xs)}
.tbd .ed:hover{background:var(--pt-surface-hover);
box-shadow:inset 0 0 0 1px var(--pt-border-default)}
.tbd .ed input{width:100%;border:0;background:transparent;font:inherit;font-size:8.5px;
color:var(--pt-accent);text-align:end;outline:none;font-variant-numeric:tabular-nums;
padding:0}
.tbd tr.low td:first-child::before{content:'▲';color:var(--pt-color-warn-400);
font-size:7px;margin-inline-end:4px}
.tbd tfoot td{padding-top:6px;border-top:1px solid var(--pt-border-default);
color:var(--pt-text-muted);font-size:8px}
.tbd tfoot .sum{color:var(--pt-accent);font-weight:700;font-size:9.5px}
.tbd .hint{font-size:7.5px;color:var(--pt-text-muted)}
/* يعمل داخل إطار معزول — لا يعتمد على شيء خارجه */
/* pt-remount — يعاد التركيب عند تبديل اللغة */
(function () {
var host = document.querySelector('.tbd');
if (!host) return;
var snapshot = host.innerHTML;
var timers = [];
var ac = null;
/* الجسم كما كتبه المولِّد. يستقبل بدائل مظلَّلة لـ root وdocument
والمؤقّتات، فينظّف نفسه دون أن يعرف أنه قابل لإعادة التركيب. */
function run(root, document, setTimeout, setInterval) {
var ar = document.documentElement.lang !== 'en';
var n = function (v, d) { return Number(v).toLocaleString('en-US', {
minimumFractionDigits: d || 0, maximumFractionDigits: d || 0 }); };
/* العملة: ر.س (SAR) — سوق السعودية.
الأرقام مخزَّنة بـالريال وتُحوَّل، فتبقى
المبالغ معقولة في كل سوق. والتحويل لا التبديل: «18,500» راتبًا بالريال
معقول، وبالدينار الأردني ثروة.
وخانتان عشريتان — الوحدة تُقسَّم إلى مئة. */
var money = (function () {
var RATE = 3.75, DEC = 2;
return function (v, d) {
var out = (Number(v) / 3.75) * RATE;
var places = d === undefined ? DEC : d;
var s = out.toLocaleString('en-US', {
minimumFractionDigits: places, maximumFractionDigits: places });
return ar ? s + ' ر.س' : 'SAR ' + s;
};
})();
var LOW = 20;
var ROWS = [
{ nm: 'ورق تصوير A4 — علبة', q: 48, p: 32 },
{ nm: 'حبر طابعة أسود', q: 12, p: 185 },
{ nm: 'أقلام جافّة — علبة', q: 96, p: 24 },
{ nm: 'ملفّات بلاستيكية', q: 15, p: 9 },
{ nm: 'دباسة مكتبية', q: 33, p: 41 }
];
var tb = root.querySelector('tbody');
function draw() {
tb.innerHTML = ROWS.map(function (r, i) {
return '<tr class="' + (r.q <= LOW ? 'low' : '') + '"><td>' + r.nm + '</td>' +
'<td class="e ed" data-i="' + i + '" data-f="q" tabindex="0">' + n(r.q) + '</td>' +
'<td class="e ed" data-i="' + i + '" data-f="p" tabindex="0">' + n(r.p) + '</td>' +
'<td class="e">' + money(r.q * r.p, 0) + '</td></tr>';
}).join('');
var tot = ROWS.reduce(function (a, r) { return a + r.q * r.p; }, 0);
root.querySelector('.sum').textContent = money(tot, 0);
root.querySelector('.cnt').textContent = ROWS.length + (ar ? ' أصناف · ' : ' items · ') +
ROWS.filter(function (r) { return r.q <= LOW; }).length + (ar ? ' منخفض' : ' low');
}
function edit(td) {
if (td.querySelector('input')) return;
var i = +td.dataset.i, f = td.dataset.f;
var was = ROWS[i][f]; /* القيمة السابقة للتراجع */
td.innerHTML = '<input inputmode="numeric" value="' + was + '">';
var inp = td.querySelector('input');
inp.focus(); inp.select();
function commit(save) {
var v = save ? parseInt(String(inp.value).replace(/[^0-9]/g, ''), 10) : was;
ROWS[i][f] = isNaN(v) ? was : Math.max(0, v);
draw();
}
inp.addEventListener('keydown', function (e) {
if (e.key === 'Enter') { e.preventDefault(); commit(true); }
/* الهروب يستعيد السابق لا يحفظ الحالي: من فتح خلية سهوًا يحتاج
مخرجًا لا يغيّر البيانات — وإلّا صارت كل نقرة عابرة تعديلًا. */
if (e.key === 'Escape') { e.preventDefault(); commit(false); }
});
inp.addEventListener('blur', function () { commit(true); });
}
tb.addEventListener('click', function (e) {
var td = e.target.closest('.ed'); if (td) edit(td);
});
tb.addEventListener('keydown', function (e) {
var td = e.target.closest('.ed');
if (td && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); edit(td); }
});
draw();
}
function mount() {
ac = new AbortController();
var real = window.document;
/* وسيط يمرّر كل شيء إلى المستند الحقيقي، ويعلّق كل مستمع بإشارة
تُقطع عند إعادة التركيب — فلا يبقى مستمع من نسخة ماتت. */
var doc = new Proxy(real, {
get: function (t, k) {
if (k === 'addEventListener') {
return function (type, fn, opts) {
var o = (opts && typeof opts === 'object') ? Object.assign({}, opts)
: { capture: !!opts };
o.signal = ac.signal;
return t.addEventListener(type, fn, o);
};
}
var v = t[k];
return typeof v === 'function' ? v.bind(t) : v;
}
});
function track(fn) {
return function (f, ms) { var id = fn(f, ms); timers.push(id); return id; };
}
run(host, doc, track(window.setTimeout.bind(window)),
track(window.setInterval.bind(window)));
}
function unmount() {
if (ac) ac.abort();
// المعرّفان يتشاركان فضاءً واحدًا في المتصفّح، فالإلغاء المزدوج آمن
timers.forEach(function (id) { window.clearTimeout(id); window.clearInterval(id); });
timers = [];
host.innerHTML = snapshot;
}
mount();
addEventListener('pt-lang', function () { unmount(); mount(); });
})();
# Inline Cell Editing
**Role:** You are an Arabic-first UI designer and front-end engineer. Read the whole
specification before writing a single line, then follow it literally. If two requirements
conflict, favour usability over visual effect.
**Goal:** Inline Cell Editing — Saves on Enter, reverts on Escape, and recomputes immediately
**Why this component matters:** Escape must restore the previous value, not just close the editor. A user who opens a cell by accident needs a way out that does not commit — without it every stray click is a data change.
**Direction:** Genuine RTL — not a mirrored Latin layout. Use logical properties
(`margin-inline-start`, `padding-inline`, `inset-inline-start`, `text-align: start`),
never physical ones (`margin-left`, `left`). The layout must flip with `dir` on its own,
with no duplicated rules.
**Typography:** Cairo for Arabic, IBM Plex Sans for Latin. Arabic line-height must be
`0.18` higher than the Latin equivalent — dots, hamzas and maddas need more vertical room.
Arabic font-size runs `1.06×` the Latin size at the same optical weight.
**Numerals:** Western digits in both languages (`1,234.50`). Only the currency changes:
`ر.س` **after** the number in Arabic, `$` **before** it in English. Jordanian dinar
uses **three** decimal places, not two.
**Colors:** Dark mode — background `#0D142B` · surfaces `#211F54` · borders `#545C91` · text `#918FC2`.
Accent `#ADA9E8` must never exceed 8% of the screen area.
**Language:** Bilingual — every string carries both Arabic and English, and the layout
flips with `dir` automatically.
**States:** `default` · `hover` · `focus` · `active` · `disabled` · `loading` · `error`.
Do not design the resting state alone.
**Accessibility:** Contrast ratio at least `4.5:1`, complete keyboard navigation,
visible focus rings, and honour `prefers-reduced-motion`.
**Output:** Clean HTML and CSS in two separate files. No frameworks, no libraries,
no external dependencies.