浏览代码

fix(web): 统一代客建单下拉样式并修复长文本换行

sy 2 天之前
父节点
当前提交
e015ad49fc
共有 2 个文件被更改,包括 106 次插入 和 73 次删除
  1. 78 49
      web-scaffold/src/pages/WorkOrderManagementPage.tsx
  2. 28 24
      web-scaffold/src/styles/work-order-ledger.css

+ 78 - 49
web-scaffold/src/pages/WorkOrderManagementPage.tsx

@@ -8,7 +8,7 @@ import { TicketState } from "../components/ticketState";
 import { WorkOrderLedgerList } from "../components/work-order/WorkOrderLedgerList";
 import { useEffect, useId, useRef, useState, type FormEvent, type KeyboardEvent } from "react";
 import {
-  ArrowRight, ChevronDown, ClipboardCheck, ClipboardList, Clock3, Headphones, Inbox, Plus, Search, Wrench, X,
+  ArrowRight, ClipboardCheck, ClipboardList, Clock3, Headphones, Inbox, Plus, Search, Wrench, X,
 } from "lucide-react";
 import { Button } from "../components/ui/button";
 import { Input } from "../components/ui/input";
@@ -59,6 +59,7 @@ type TicketLookupState = "loading" | "ready" | "error";
 
 function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCreated: (message: string) => void }) {
   const id = useId();
+  const customerTrigger = useRef<HTMLButtonElement>(null);
   const searchInput = useRef<HTMLInputElement>(null);
   const customerList = useRef<HTMLUListElement>(null);
   const submit = useSubmit();
@@ -72,26 +73,29 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
   const [customerLookup, setCustomerLookup] = useState<TicketLookupState>("loading");
   const [customerOpen, setCustomerOpen] = useState(false);
   const [activeCustomer, setActiveCustomer] = useState(0);
-  const [orders, setSubs] = useState<{ subscription_id: string; plan_name: string | null; contract_no: string | null }[]>([]);
+  const [subscriptions, setSubscriptions] = useState<{ value: string; label: string }[]>([]);
   const [subscriptionLookup, setSubscriptionLookup] = useState<TicketLookupState>("loading");
   const [invalid, setInvalid] = useState<"customer" | "title" | null>(null);
 
   const customerId = customer?.customer_id ?? "";
+  const customerPopupId = `${id}-customer-picker`;
   const customerOptionListId = `${id}-customer-options`;
   const customerHints = { loading: "正在查找客户…", error: "客户读取失败,请重新搜索", ready: "没有找到客户" };
-  const subscriptionHints = { loading: "正在读取订阅…", error: "订阅读取失败", ready: orders.length ? "不关联订阅" : "该客户暂无订阅" };
+  const subscriptionHints = { loading: "正在读取订阅…", error: "订阅读取失败", ready: subscriptions.length ? "不关联订阅" : "该客户暂无订阅" };
+  const subscriptionPlaceholder = customerId ? subscriptionHints[subscriptionLookup] : "请先选择归属客户";
+  const selectedSubscription = subscriptions.find((item) => item.value === orderId);
 
   function pickCustomer(next: TicketCustomer | null) {
+    closeCustomerPicker();
+    if (next?.customer_id === customer?.customer_id) return;
     setCustomer(next);
-    setCustomerOpen(!next);
     setOrderId("");
-    setSubs([]);
+    setSubscriptions([]);
     setSubscriptionLookup("loading");
     if (invalid === "customer") setInvalid(null);
   }
 
   function searchCustomers(value: string) {
-    setCustomerOpen(true);
     setActiveCustomer(0);
     if (value === keyword) return;
     setKeyword(value);
@@ -99,30 +103,34 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
     setCustomerLookup("loading");
   }
 
+  function openCustomerPicker() {
+    searchCustomers("");
+    setCustomerOpen(true);
+  }
+
+  function closeCustomerPicker() {
+    setCustomerOpen(false);
+    customerTrigger.current?.focus({ preventScroll: true });
+  }
+
   function handleCustomerKeys(event: KeyboardEvent<HTMLInputElement>) {
-    if (customer || isImeComposing(event)) return;
+    if (isImeComposing(event)) return;
     if (event.key === "ArrowDown" || event.key === "ArrowUp") {
       event.preventDefault();
-      setCustomerOpen(true);
       if (!customers.length) return;
       const step = event.key === "ArrowDown" ? 1 : -1;
-      const next = customerOpen
-        ? (activeCustomer + step + customers.length) % customers.length
-        : step === 1 ? 0 : customers.length - 1;
+      const next = (activeCustomer + step + customers.length) % customers.length;
       setActiveCustomer(next);
       customerList.current?.children[next]?.scrollIntoView({ block: "nearest" });
-    } else if (customerOpen && event.key === "Enter") {
+    } else if (event.key === "Enter") {
       event.preventDefault();
       const next = customers[activeCustomer];
       if (next) pickCustomer(next);
-    } else if (customerOpen && event.key === "Escape") {
-      event.preventDefault();
-      event.stopPropagation();
-      setCustomerOpen(false);
     }
   }
 
-  useEffect(() => { searchInput.current?.focus({ preventScroll: true }); }, []);
+  useEffect(() => { customerTrigger.current?.focus({ preventScroll: true }); }, []);
+  useEffect(() => { if (customerOpen) searchInput.current?.focus({ preventScroll: true }); }, [customerOpen]);
 
   useEffect(() => {
     let alive = true;
@@ -147,7 +155,10 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
     fetchAllPages((page, page_size) => orderGetList({ customer_id: customerId, page, page_size }), { pageSize: LIST_PAGE_SIZE })
       .then((r) => {
         if (!alive) return;
-        setSubs(r.items.map((s) => ({ subscription_id: s.subscription_id, plan_name: s.plan_name, contract_no: s.contract_no ?? null })));
+        setSubscriptions(r.items.map((item) => ({
+          value: item.subscription_id,
+          label: [item.plan_name ?? item.subscription_id, item.contract_no].filter(Boolean).join(" · "),
+        })));
         setSubscriptionLookup("ready");
       })
       .catch(() => { if (alive) setSubscriptionLookup("error"); });
@@ -160,7 +171,7 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
     submit.clear();
     if (!customerId) {
       setInvalid("customer");
-      searchInput.current?.focus();
+      customerTrigger.current?.focus();
       return;
     }
     if (title.trim().length < 2) { setInvalid("title"); return; }
@@ -184,40 +195,58 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
             <div className="ticket-create-field" onBlur={(event) => {
               if (!event.currentTarget.contains(event.relatedTarget)) setCustomerOpen(false);
             }}>
-              <label htmlFor={`${id}-customer`} className="field-label-title">归属客户<em aria-hidden="true">*</em></label>
-              <div className="ticket-create-search">
-                <Search size={16} aria-hidden="true" className="ticket-create-search-icon" />
-                <Input id={`${id}-customer`} ref={searchInput} role="combobox" autoComplete="off" maxLength={50}
-                  aria-autocomplete="list" aria-expanded={customerOpen} aria-controls={customerOpen ? customerOptionListId : undefined}
-                  aria-activedescendant={customerOpen && customers[activeCustomer] ? `${customerOptionListId}-${activeCustomer}` : undefined}
-                  aria-required="true" aria-invalid={invalid === "customer" || undefined} aria-describedby={invalid === "customer" ? `${id}-error` : undefined}
-                  placeholder="输入客户名称或编号" value={customer?.customer_name || customerId || keyword} readOnly={Boolean(customer)}
-                  onChange={(event) => searchCustomers(event.target.value)} onKeyDown={handleCustomerKeys}
-                  onClick={() => { if (!customer) setCustomerOpen(true); }} />
-                {customer ? <button type="button" className="ticket-create-search-clear" aria-label="清除归属客户" onClick={() => {
-                  pickCustomer(null);
-                  searchCustomers("");
-                  searchInput.current?.focus();
-                }}><X size={15} aria-hidden="true" /></button> : <ChevronDown size={15} aria-hidden="true" className="ticket-create-search-chevron" />}
-                {customerOpen && <div className="ticket-create-customer-popup">
-                  <ul ref={customerList} id={customerOptionListId} role="listbox" aria-label="客户搜索结果">
-                    {customers.map((item, index) => <li key={item.customer_id} id={`${customerOptionListId}-${index}`} role="option"
-                      aria-selected={index === activeCustomer} onMouseEnter={() => setActiveCustomer(index)}
-                      onMouseDown={(event) => event.preventDefault()} onClick={() => pickCustomer(item)}>
-                      <span>{item.customer_name || item.customer_id}</span>
-                      {item.customer_name && <small>{item.customer_id}</small>}
-                    </li>)}
-                  </ul>
-                  {!customers.length && <p role="status">{customerHints[customerLookup]}</p>}
+              <span id={`${id}-customer-label`} className="field-label-title">归属客户<em aria-hidden="true">*</em></span>
+              <div className="ticket-create-picker">
+                <button type="button" ref={customerTrigger} className="ticket-create-select" data-placeholder={!customerId || undefined}
+                  aria-labelledby={`${id}-customer-label ${id}-customer-value`} aria-haspopup="dialog" aria-expanded={customerOpen}
+                  aria-controls={customerOpen ? customerPopupId : undefined} aria-invalid={invalid === "customer" || undefined}
+                  aria-describedby={invalid === "customer" ? `${id}-error` : undefined} title={customer?.customer_name || customerId || undefined}
+                  onClick={() => { if (customerOpen) closeCustomerPicker(); else openCustomerPicker(); }}
+                  onKeyDown={(event) => {
+                    if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
+                    event.preventDefault();
+                    openCustomerPicker();
+                  }}>
+                  <span className="ticket-create-select-value" id={`${id}-customer-value`}>{customer?.customer_name || customerId || "请选择客户"}</span>
+                </button>
+                {customerOpen && <div id={customerPopupId} className="ticket-create-customer-popup" role="dialog" aria-label="选择归属客户"
+                  onKeyDown={(event) => {
+                    if (event.key !== "Escape" || isImeComposing(event)) return;
+                    event.preventDefault();
+                    event.stopPropagation();
+                    closeCustomerPicker();
+                  }}>
+                  <div className="ticket-create-search">
+                    <Search size={15} aria-hidden="true" />
+                    <Input ref={searchInput} role="combobox" aria-label="搜索客户" aria-autocomplete="list" aria-expanded="true" aria-controls={customerOptionListId}
+                      aria-activedescendant={customers[activeCustomer] ? `${customerOptionListId}-${activeCustomer}` : undefined}
+                      autoComplete="off" maxLength={50} placeholder="输入客户名称或编号" value={keyword}
+                      onChange={(event) => searchCustomers(event.target.value)} onKeyDown={handleCustomerKeys} />
+                  </div>
+                  <div className="ticket-create-customer-results">
+                    <ul ref={customerList} id={customerOptionListId} role="listbox" aria-label="客户搜索结果">
+                      {customers.map((item, index) => <li key={item.customer_id} id={`${customerOptionListId}-${index}`} role="option"
+                        aria-selected={index === activeCustomer} onMouseEnter={() => setActiveCustomer(index)}
+                        onMouseDown={(event) => event.preventDefault()} onClick={() => pickCustomer(item)}>
+                        <span>{item.customer_name || item.customer_id}</span>
+                        {item.customer_name && <small>{item.customer_id}</small>}
+                      </li>)}
+                    </ul>
+                    {!customers.length && <p role="status">{customerHints[customerLookup]}</p>}
+                  </div>
+                  {customer && <button type="button" className="ticket-create-search-clear" onClick={() => pickCustomer(null)}>清除选择</button>}
                 </div>}
               </div>
             </div>
             <FieldLabel className="ticket-create-field" label="关联订阅">
-              <select className="ticket-create-control" value={orderId} data-placeholder={!orderId || undefined}
-                disabled={!customerId || subscriptionLookup !== "ready" || orders.length === 0} onChange={(event) => setOrderId(event.target.value)}>
-                <option value="">{customerId ? subscriptionHints[subscriptionLookup] : "请先选择归属客户"}</option>
-                {orders.map((order) => <option key={order.subscription_id} value={order.subscription_id}>{order.plan_name ?? order.subscription_id}{order.contract_no ? ` · ${order.contract_no}` : ""}</option>)}
-              </select>
+              <span className="ticket-create-select" data-placeholder={!selectedSubscription || undefined} title={selectedSubscription?.label}>
+                <span className="ticket-create-select-value" aria-hidden="true">{selectedSubscription?.label ?? subscriptionPlaceholder}</span>
+                <select value={orderId} disabled={!customerId || subscriptionLookup !== "ready" || subscriptions.length === 0}
+                  onChange={(event) => setOrderId(event.target.value)}>
+                  <option value="">{subscriptionPlaceholder}</option>
+                  {subscriptions.map((item) => <option key={item.value} value={item.value}>{item.label}</option>)}
+                </select>
+              </span>
             </FieldLabel>
           </div>
         </section>

+ 28 - 24
web-scaffold/src/styles/work-order-ledger.css

@@ -84,35 +84,35 @@
 .ticket-create-field { display: grid; gap: 6px; min-width: 0; color: var(--field-label); font-size: var(--type-body); }
 .ticket-create-field em { margin-inline-start: 3px; color: var(--destructive); font-style: normal; }
 .ticket-create-field legend { margin-bottom: 6px; }
-.ticket-create-search { position: relative; display: block; }
-.ticket-create-search > :is(.ticket-create-search-icon, .ticket-create-search-chevron, .ticket-create-search-clear) { position: absolute; top: 50%; translate: 0 -50%; color: var(--icon); }
-.ticket-create-search-icon { inset-inline-start: 11px; pointer-events: none; }
-.ticket-create-search-chevron { inset-inline-end: 11px; pointer-events: none; }
-.ticket-create-search > input { padding-inline: 34px; text-overflow: ellipsis; }
-.ticket-create-search > input[readonly] { background: var(--surface-subtle); }
-.ticket-create-search-clear { inset-inline-end: 6px; display: grid; width: 25px; height: 25px; place-items: center; border-radius: 5px; cursor: pointer; }
-.ticket-create-search-clear:hover { background: var(--accent); color: var(--foreground); }
-.ticket-create-search-clear:focus-visible { outline: 2px solid var(--ring); }
+.ticket-create-picker, .ticket-create-search { position: relative; min-width: 0; }
+.ticket-create-search { flex: none; }
+.ticket-create-search > svg { position: absolute; inset-inline-start: 10px; top: 50%; translate: 0 -50%; color: var(--icon); pointer-events: none; }
+.ticket-create-search > input { padding-inline-start: 31px; }
 .ticket-create-customer-popup {
+  display: flex;
+  flex-direction: column;
+  gap: 4px;
   position: absolute;
   z-index: 3;
   inset-inline: 0;
   top: calc(100% + 5px);
-  max-height: min(220px, 40dvh);
-  overflow: auto;
-  overscroll-behavior: contain;
-  padding: 4px;
+  max-height: min(266px, 44dvh);
+  padding: 6px;
   border: 1px solid var(--border);
   border-radius: 8px;
   background: var(--card);
   color: var(--foreground);
   box-shadow: var(--shadow-card);
 }
+.ticket-create-customer-results { min-height: 0; overflow: auto; overscroll-behavior: contain; }
 .ticket-create-customer-popup li { display: grid; gap: 2px; padding: 6px 8px; border-radius: 5px; cursor: pointer; overflow-wrap: anywhere; }
 .ticket-create-customer-popup li[aria-selected="true"] { background: var(--accent); }
 .ticket-create-customer-popup :is(small, p) { color: var(--muted-foreground); font-size: var(--type-support); }
 .ticket-create-customer-popup p { padding: 7px 8px; }
-.ticket-create-control {
+.ticket-create-search-clear { flex: none; padding: 5px 8px; border-top: 1px solid var(--border-subtle); color: var(--primary); text-align: start; cursor: pointer; }
+.ticket-create-search-clear:hover { background: var(--surface-subtle); }
+.ticket-create-search-clear:focus-visible { outline: 2px solid var(--ring); outline-offset: -2px; }
+.ticket-create-control, .ticket-create-select {
   width: 100%;
   min-width: 0;
   padding: 7px 10px;
@@ -123,24 +123,28 @@
   font-size: var(--type-body);
   transition: border-color .15s, box-shadow .15s;
 }
-select.ticket-create-control {
+.ticket-create-select {
+  position: relative;
+  display: flex;
+  align-items: center;
   height: 37px;
-  appearance: none;
   padding: 0 34px 0 10px;
   background-image: var(--select-chevron);
   background-position: right 10px center;
   background-repeat: no-repeat;
   background-size: 16px;
   cursor: pointer;
-  text-overflow: ellipsis;
+  text-align: start;
 }
-textarea.ticket-create-control { min-height: 82px; line-height: 1.5; resize: vertical; }
-.ticket-create-control[data-placeholder], .ticket-create-control::placeholder { color: color-mix(in oklab, var(--muted-foreground) 70%, transparent); }
-.ticket-create-control option { color: var(--foreground); }
-.ticket-create-control:disabled { background-color: var(--surface-subtle); color: var(--muted-foreground); cursor: not-allowed; }
-.ticket-create-control:hover:enabled { border-color: var(--border-hover); }
-.ticket-create-control:focus-visible { outline: none; border-color: var(--ring); box-shadow: 0 0 0 3px var(--ring-glow); }
-.ticket-create-control[aria-invalid="true"] { border-color: var(--destructive); }
+.ticket-create-select-value { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
+.ticket-create-select > select { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0.001; cursor: pointer; }
+.ticket-create-select:has(> select:disabled) { background-color: var(--surface-subtle); color: var(--muted-foreground); }
+.ticket-create-select > select:disabled { cursor: not-allowed; }
+.ticket-create-control { min-height: 82px; line-height: 1.5; resize: vertical; }
+.ticket-create-select[data-placeholder], .ticket-create-control::placeholder { color: color-mix(in oklab, var(--muted-foreground) 70%, transparent); }
+.ticket-create-select:hover:not(:has(> select:disabled)), .ticket-create-control:hover { border-color: var(--border-hover); }
+.ticket-create-select:is(:focus-visible, [aria-expanded="true"], :has(> select:focus-visible)), .ticket-create-control:focus-visible { outline: none; border-color: var(--ring); box-shadow: 0 0 0 3px var(--ring-glow); }
+.ticket-create-select[aria-invalid="true"] { border-color: var(--destructive); }
 .ticket-create-types { display: inline-grid; grid-template-columns: repeat(2, minmax(0, 1fr)); max-width: 290px; gap: 3px; padding: 3px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-subtle); }
 .ticket-create-types label { position: relative; min-width: 0; cursor: pointer; }
 .ticket-create-types label > span { display: flex; align-items: center; justify-content: center; gap: 7px; min-height: 29px; padding: 3px 6px; border: 1px solid transparent; border-radius: 6px; transition: background-color .15s, color .15s; }