ツール管理へ戻る

v2 → v3 の差分

--- v2
+++ v3
@@ -19,14 +19,13 @@
     body {
       margin: 0;
       min-height: 100vh;
-      display: grid;
-      place-items: center;
-      padding: 24px;
+      padding: 32px 20px;
     }
 
     main {
-      width: min(100%, 640px);
-      padding: 32px;
+      width: min(100%, 960px);
+      margin: 0 auto;
+      padding: 28px;
       background: #fff;
       border: 1px solid #e5e7eb;
       border-radius: 16px;
@@ -39,38 +38,111 @@
     }
 
     .description {
-      margin: 0 0 24px;
+      margin: 0 0 22px;
       color: #4b5563;
       line-height: 1.6;
     }
 
-    .work-list {
-      display: grid;
-      gap: 16px;
-    }
-
-    .work-item {
-      padding: 16px;
-      border: 1px solid #d1d5db;
-      border-radius: 10px;
-      background: #fff;
-    }
-
-    .work-item-header {
-      display: flex;
-      align-items: center;
-      justify-content: space-between;
-      gap: 12px;
-      margin-bottom: 14px;
-    }
-
-    .work-item-title {
-      margin: 0;
+    .table-wrap {
+      overflow-x: auto;
+      border: 1px solid #cbd5e1;
+      border-radius: 8px;
+    }
+
+    table {
+      width: 100%;
+      min-width: 680px;
+      border-collapse: collapse;
+      table-layout: fixed;
+    }
+
+    th,
+    td {
+      padding: 10px;
+      border-right: 1px solid #cbd5e1;
+      border-bottom: 1px solid #cbd5e1;
+      text-align: left;
+      vertical-align: middle;
+    }
+
+    th:last-child,
+    td:last-child {
+      border-right: 0;
+    }
+
+    tbody tr:last-child td {
+      border-bottom: 1px solid #cbd5e1;
+    }
+
+    thead th {
+      background: #eff6ff;
+      color: #1e3a8a;
+      font-weight: 700;
+    }
+
+    tbody tr:nth-child(even) {
+      background: #f8fafc;
+    }
+
+    tfoot th,
+    tfoot td {
+      border-bottom: 0;
+      background: #eaf2ff;
+      color: #1e3a8a;
+      font-weight: 750;
+    }
+
+    .column-number {
+      width: 72px;
+      text-align: center;
+    }
+
+    .column-count {
+      width: 150px;
+    }
+
+    .column-time {
+      width: 190px;
+    }
+
+    .column-subtotal {
+      width: 200px;
+    }
+
+    .column-action {
+      width: 100px;
+      text-align: center;
+    }
+
+    input {
+      width: 100%;
+      min-width: 80px;
+      padding: 9px;
+      border: 1px solid #9ca3af;
+      border-radius: 6px;
+      font: inherit;
       font-size: 1rem;
     }
 
+    input:focus {
+      outline: 3px solid #bfdbfe;
+      border-color: #2563eb;
+    }
+
+    .unit {
+      display: block;
+      margin-top: 4px;
+      color: #526071;
+      font-size: 0.85rem;
+    }
+
+    .subtotal {
+      font-weight: 650;
+      white-space: nowrap;
+    }
+
     .remove-button {
-      padding: 6px 10px;
+      padding: 7px 10px;
       border: 1px solid #b42318;
       border-radius: 6px;
       background: #fff;
@@ -83,56 +155,16 @@
       background: #fef3f2;
     }
 
-    .field {
-      margin-bottom: 14px;
-    }
-
-    .field:last-child {
-      margin-bottom: 0;
-    }
-
-    label {
-      display: block;
-      margin-bottom: 7px;
-      font-weight: 700;
-    }
-
-    .input-row {
+    .actions {
       display: flex;
-      align-items: center;
+      flex-wrap: wrap;
       gap: 10px;
-    }
-
-    input {
-      width: 100%;
-      min-width: 0;
-      padding: 11px;
-      border: 1px solid #9ca3af;
-      border-radius: 8px;
-      font: inherit;
-      font-size: 1rem;
-    }
-
-    input:focus {
-      outline: 3px solid #bfdbfe;
-      border-color: #2563eb;
-    }
-
-    .unit {
-      flex: 0 0 auto;
-      color: #4b5563;
-    }
-
-    .actions {
-      display: grid;
-      grid-template-columns: 1fr 1fr;
-      gap: 12px;
-      margin-top: 20px;
+      margin-top: 16px;
     }
 
     button.primary-button,
     button.secondary-button {
-      padding: 12px 16px;
+      padding: 11px 16px;
       border-radius: 8px;
       font: inherit;
       font-weight: 700;
@@ -162,27 +194,6 @@
     button:focus-visible {
       outline: 3px solid #93c5fd;
       outline-offset: 3px;
-    }
-
-    .result {
-      margin-top: 24px;
-      padding: 18px;
-      border-radius: 10px;
-      background: #eff6ff;
-    }
-
-    .result-label {
-      margin: 0 0 6px;
-      color: #1e3a8a;
-      font-weight: 700;
-    }
-
-    output {
-      display: block;
-      color: #1e3a8a;
-      font-size: 1.35rem;
-      font-weight: 700;
-      overflow-wrap: anywhere;
     }
 
     .message {
@@ -192,13 +203,17 @@
       font-size: 0.95rem;
     }
 
-    @media (max-width: 520px) {
+    @media (max-width: 600px) {
+      body {
+        padding: 16px 10px;
+      }
+
       main {
-        padding: 24px 20px;
-      }
-
-      .actions {
-        grid-template-columns: 1fr;
+        padding: 22px 16px;
+      }
+
+      h1 {
+        font-size: 1.4rem;
       }
     }
   </style>
@@ -207,228 +222,246 @@
   <main>
     <h1>総作業時間計算</h1>
     <p class="description">
-      作業ごとに件数と1件あたりの時間を入力し、全作業の合計時間を計算します。
+      作業ごとに件数と1件あたりの時間を入力してください。小計と合計は入力に合わせて更新されます。
     </p>
 
-    <form id="calculator" novalidate>
-      <div id="work-list" class="work-list"></div>
-
-      <div class="actions">
-        <button id="add-work" class="secondary-button" type="button">
-          新規登録
-        </button>
-        <button class="primary-button" type="submit">
-          合計を計算
-        </button>
-      </div>
-    </form>
-
-    <section class="result" aria-live="polite">
-      <p class="result-label">合計時間</p>
-      <output id="result">—</output>
-    </section>
-
-    <p id="message" class="message" role="alert"></p>
+    <div class="table-wrap">
+      <table aria-label="作業時間一覧">
+        <colgroup>
+          <col class="column-number">
+          <col class="column-count">
+          <col class="column-time">
+          <col class="column-subtotal">
+          <col class="column-action">
+        </colgroup>
+        <thead>
+          <tr>
+            <th scope="col" class="column-number">No.</th>
+            <th scope="col">作業件数</th>
+            <th scope="col">1件あたりの時間</th>
+            <th scope="col">小計</th>
+            <th scope="col" class="column-action">操作</th>
+          </tr>
+        </thead>
+        <tbody id="work-list"></tbody>
+        <tfoot>
+          <tr>
+            <th scope="row" colspan="3">合計</th>
+            <td id="grand-total">0分</td>
+            <td></td>
+          </tr>
+        </tfoot>
+      </table>
+    </div>
+
+    <div class="actions">
+      <button id="add-work" class="secondary-button" type="button">
+        新規登録
+      </button>
+      <button id="calculate" class="primary-button" type="button">
+        合計を計算
+      </button>
+    </div>
+
+    <p id="message" class="message" role="alert" aria-live="polite"></p>
   </main>
 
   <script>
     "use strict";
 
-    const form = document.getElementById("calculator");
     const workList = document.getElementById("work-list");
     const addWorkButton = document.getElementById("add-work");
-    const result = document.getElementById("result");
+    const calculateButton = document.getElementById("calculate");
+    const grandTotalCell = document.getElementById("grand-total");
     const message = document.getElementById("message");
 
-    let nextWorkNumber = 1;
-
-    function createWorkItem() {
-      const itemNumber = nextWorkNumber++;
-
-      const item = document.createElement("section");
-      item.className = "work-item";
-      item.dataset.workNumber = String(itemNumber);
-      item.setAttribute("aria-labelledby", `work-title-${itemNumber}`);
-
-      const header = document.createElement("div");
-      header.className = "work-item-header";
-
-      const title = document.createElement("h2");
-      title.className = "work-item-title";
-      title.id = `work-title-${itemNumber}`;
-      title.textContent = `作業 ${itemNumber}`;
-
-      const removeButton = document.createElement("button");
-      removeButton.className = "remove-button";
-      removeButton.type = "button";
-      removeButton.textContent = "削除";
-      removeButton.setAttribute("aria-label", `作業 ${itemNumber} を削除`);
-
-      removeButton.addEventListener("click", () => {
-        item.remove();
-        renumberWorkItems();
-        clearResult();
-      });
-
-      header.append(title, removeButton);
-
-      const countField = document.createElement("div");
-      countField.className = "field";
-
-      const countLabel = document.createElement("label");
-      countLabel.htmlFor = `count-${itemNumber}`;
-      countLabel.textContent = "作業件数";
-
-      const countRow = document.createElement("div");
-      countRow.className = "input-row";
-
+    function formatNumber(value) {
+      return new Intl.NumberFormat("ja-JP", {
+        maximumFractionDigits: 2
+      }).format(value);
+    }
+
+    function formatDuration(totalMinutes) {
+      const totalHours = Math.floor(totalMinutes / 60);
+      const remainingMinutes = totalMinutes - totalHours * 60;
+
+      if (totalHours === 0) {
+        return `${formatNumber(totalMinutes)}分`;
+      }
+
+      if (remainingMinutes === 0) {
+        return `${formatNumber(totalHours)}時間`;
+      }
+
+      return `${formatNumber(totalHours)}時間${formatNumber(remainingMinutes)}分`;
+    }
+
+    function setSubtotal(row, minutes) {
+      const subtotalCell = row.querySelector(".subtotal");
+      subtotalCell.textContent = formatDuration(minutes);
+    }
+
+    function updateTotals() {
+      let totalMinutes = 0;
+
+      for (const row of workList.querySelectorAll("tr")) {
+        const countInput = row.querySelector('input[name="count"]');
+        const timeInput = row.querySelector('input[name="minutes"]');
+
+        if (countInput.value === "" || timeInput.value === "") {
+          setSubtotal(row, 0);
+          continue;
+        }
+
+        const count = countInput.valueAsNumber;
+        const minutesPerItem = timeInput.valueAsNumber;
+
+        if (
+          !Number.isFinite(count) ||
+          !Number.isFinite(minutesPerItem) ||
+          count < 0 ||
+          !Number.isInteger(count) ||
+          minutesPerItem < 0
+        ) {
+          setSubtotal(row, 0);
+          continue;
+        }
+
+        const subtotal = count * minutesPerItem;
+        setSubtotal(row, subtotal);
+        totalMinutes += subtotal;
+      }
+
+      grandTotalCell.textContent = formatDuration(totalMinutes);
+    }
+
+    function addWorkRow() {
+      const row = document.createElement("tr");
+
+      const numberCell = document.createElement("th");
+      numberCell.scope = "row";
+      numberCell.className = "column-number";
+
+      const countCell = document.createElement("td");
       const countInput = document.createElement("input");
-      countInput.id = `count-${itemNumber}`;
+      countInput.type = "number";
       countInput.name = "count";
-      countInput.type = "number";
       countInput.min = "0";
       countInput.step = "1";
       countInput.inputMode = "numeric";
       countInput.placeholder = "例:8";
+      countInput.setAttribute("aria-label", "作業件数");
       countInput.required = true;
-
-      const countUnit = document.createElement("span");
-      countUnit.className = "unit";
-      countUnit.textContent = "件";
-
-      countRow.append(countInput, countUnit);
-      countField.append(countLabel, countRow);
-
-      const timeField = document.createElement("div");
-      timeField.className = "field";
-
-      const timeLabel = document.createElement("label");
-      timeLabel.htmlFor = `minutes-${itemNumber}`;
-      timeLabel.textContent = "1件あたりの時間";
-
-      const timeRow = document.createElement("div");
-      timeRow.className = "input-row";
-
+      countCell.append(countInput);
+
+      const timeCell = document.createElement("td");
       const timeInput = document.createElement("input");
-      timeInput.id = `minutes-${itemNumber}`;
+      timeInput.type = "number";
       timeInput.name = "minutes";
-      timeInput.type = "number";
       timeInput.min = "0";
       timeInput.step = "any";
       timeInput.inputMode = "decimal";
       timeInput.placeholder = "例:15";
+      timeInput.setAttribute("aria-label", "1件あたりの時間");
       timeInput.required = true;
 
       const timeUnit = document.createElement("span");
       timeUnit.className = "unit";
       timeUnit.textContent = "分";
-
-      timeRow.append(timeInput, timeUnit);
-      timeField.append(timeLabel, timeRow);
-
-      item.append(header, countField, timeField);
-      workList.append(item);
-
+      timeCell.append(timeInput, timeUnit);
+
+      const subtotalCell = document.createElement("td");
+      subtotalCell.className = "subtotal";
+      subtotalCell.textContent = "0分";
+
+      const actionCell = document.createElement("td");
+      actionCell.className = "column-action";
+
+      const removeButton = document.createElement("button");
+      removeButton.type = "button";
+      removeButton.className = "remove-button";
+      removeButton.textContent = "削除";
+      removeButton.setAttribute("aria-label", "この作業を削除");
+
+      removeButton.addEventListener("click", () => {
+        row.remove();
+        renumberRows();
+        message.textContent = "";
+        updateTotals();
+      });
+
+      actionCell.append(removeButton);
+      row.append(numberCell, countCell, timeCell, subtotalCell, actionCell);
+      workList.append(row);
+
+      renumberRows();
+      message.textContent = "";
       countInput.focus();
-      clearResult();
-    }
-
-    function renumberWorkItems() {
-      const items = workList.querySelectorAll(".work-item");
-
-      items.forEach((item, index) => {
-        const number = index + 1;
-        const title = item.querySelector(".work-item-title");
-        const removeButton = item.querySelector(".remove-button");
-
-        title.textContent = `作業 ${number}`;
-        removeButton.setAttribute("aria-label", `作業 ${number} を削除`);
+      updateTotals();
+    }
+
+    function renumberRows() {
+      const rows = workList.querySelectorAll("tr");
+
+      rows.forEach((row, index) => {
+        row.querySelector("th").textContent = String(index + 1);
       });
     }
 
-    function clearResult() {
-      result.textContent = "—";
+    function calculateAndValidate() {
       message.textContent = "";
-    }
-
-    function formatNumber(value) {
-      return new Intl.NumberFormat("ja-JP", {
-        maximumFractionDigits: 2
-      }).format(value);
-    }
-
-    function formatDuration(totalMinutes) {
-      const totalHours = Math.floor(totalMinutes / 60);
-      const remainingMinutes = totalMinutes - totalHours * 60;
-
-      if (totalHours === 0) {
-        return `${formatNumber(totalMinutes)}分`;
-      }
-
-      if (remainingMinutes === 0) {
-        return `${formatNumber(totalHours)}時間`;
-      }
-
-      return `${formatNumber(totalHours)}時間${formatNumber(remainingMinutes)}分`;
-    }
-
-    addWorkButton.addEventListener("click", createWorkItem);
-
-    form.addEventListener("submit", (event) => {
-      event.preventDefault();
-      clearResult();
-
-      const items = workList.querySelectorAll(".work-item");
-
-      if (items.length === 0) {
+      const rows = workList.querySelectorAll("tr");
+
+      if (rows.length === 0) {
         message.textContent = "作業を1件以上登録してください。";
         addWorkButton.focus();
         return;
       }
 
-      let totalMinutes = 0;
-
-      for (const item of items) {
-        const countInput = item.querySelector('input[name="count"]');
-        const timeInput = item.querySelector('input[name="minutes"]');
+      for (const row of rows) {
+        const countInput = row.querySelector('input[name="count"]');
+        const timeInput = row.querySelector('input[name="minutes"]');
+
+        if (countInput.value === "") {
+          message.textContent = "すべての作業の件数を入力してください。";
+          countInput.focus();
+          return;
+        }
+
+        if (timeInput.value === "") {
+          message.textContent = "すべての作業の時間を入力してください。";
+          timeInput.focus();
+          return;
+        }
+
         const count = countInput.valueAsNumber;
         const minutesPerItem = timeInput.valueAsNumber;
 
-        if (!Number.isFinite(count) || !Number.isFinite(minutesPerItem)) {
-          message.textContent = "すべての作業について、件数と時間を入力してください。";
-          if (!Number.isFinite(count)) {
-            countInput.focus();
-          } else {
-            timeInput.focus();
-          }
-          return;
-        }
-
-        if (count < 0 || !Number.isInteger(count)) {
+        if (!Number.isFinite(count) || count < 0 || !Number.isInteger(count)) {
           message.textContent = "作業件数は0以上の整数で入力してください。";
           countInput.focus();
           return;
         }
 
-        if (minutesPerItem < 0) {
+        if (!Number.isFinite(minutesPerItem) || minutesPerItem < 0) {
           message.textContent = "1件あたりの時間は0以上で入力してください。";
           timeInput.focus();
           return;
         }
-
-        totalMinutes += count * minutesPerItem;
-
-        if (!Number.isFinite(totalMinutes)) {
-          message.textContent = "入力値が大きすぎるため計算できません。";
-          return;
-        }
-      }
-
-      result.textContent =
-        `${formatNumber(totalMinutes)}分(${formatDuration(totalMinutes)})`;
+      }
+
+      updateTotals();
+    }
+
+    addWorkButton.addEventListener("click", addWorkRow);
+    calculateButton.addEventListener("click", calculateAndValidate);
+
+    workList.addEventListener("input", () => {
+      message.textContent = "";
+      updateTotals();
     });
 
-    createWorkItem();
+    addWorkRow();
   </script>
 </body>
 </html>