找回密碼
 註冊
搜索
查看: 253|回復: 0

JavaScript 實作一個 search-box Web 元件

[複製鏈接]
發表於 5 天前 | 顯示全部樓層 |閱讀模式
Push to Facebook
  1. <script>
  2.     class SearchBox extends HTMLElement {
  3.         constructor() {
  4.             super();
  5.             this.attachShadow({mode:"open"});
  6.             this.shadowRoot.append(SearchBox.template.content.cloneNode(true));

  7.             this.input = this.shadowRoot.querySelector("#input");
  8.             this.leftSlot = this.shadowRoot.querySelector('slot[name="left"]');
  9.             this.rightSlot = this.shadowRoot.querySelector('slot[name="right"]');

  10.             this.input.onfocus = () => {this.setAttribute("focused", ""); };
  11.             this.input.onblur = () => {this.removeAttribute("focused", ""); };

  12.             leftSolt.onclick = this.input.onchange = (event) => {
  13.                 event.stopPropagation();
  14.                 if (this.disabled) return ;
  15.                 this.dispatchEvent(new CustomEvent("search",{
  16.                     detail:this.input.value
  17.                 }));
  18.             }

  19.             rightSolt.onclick = (event) => {
  20.                 event.stopPropagation();
  21.                 if (this.disabled) return ;

  22.                 this.dispatchEvent(e);
  23.                 if (!e.defaultPrevented) {
  24.                     this.input.value = "";
  25.                 }
  26.             };
  27.         }

  28.         attributeChangedCallback(name, oldValue, newValue){
  29.                 if (name === " disabled") {
  30.                     this.input.disabled = newValue !== null ;
  31.                 } else if (name === "placeholder") {
  32.                     this.input.placeholder = newValue;
  33.                 } else if (name === "size") {
  34.                     this.input.size = newValue;
  35.                 } else if (name === "value") {
  36.                     this.input.value = newValue;
  37.                 }
  38.             }

  39.         get placeholder(){ return this.getAttribute("placeholder"); }
  40.         get size(){ return this.getAttribute("size"); }
  41.         get value(){ return this.getAttribute("value"); }
  42.         get disabled(){ return this.hasAttribute("disabled"); }
  43.         get hidden(){ return this.hasAttribute("hidden"); }


  44.         set placeholder(value){ this.setAttribute("placeholder", value); }
  45.         set size(value){ this.setAttribute("size", value); }
  46.         set value(text){ this.setAttribute("value", text); }
  47.         set disabled(value){
  48.             if (value) this.setAttribute("disabled", );
  49.             else this.removeAttribute("disabled");
  50.         }

  51.         set hidden(value){
  52.             if (value) this.setAttribute("hidden", );
  53.             else this.removeAttribute("hidden");
  54.         }
  55.     }

  56.     SearchBox.observedAttributes = ["disabled", "placeholder","size", "value"];

  57.     SearchBox.template = document.createElement("template");
  58.     SearchBox.template.innerHTML =`
  59.     <style>
  60.     :host {
  61.         display: inline-block;
  62.         border: solid black 1px;
  63.         border-radius: 5px;
  64.         padding: 4px 6px;
  65.     }

  66.     :host([hidden]) {
  67.         display: none;
  68.     }
  69.     :host([disabled]){
  70.         opacity: 0.5;
  71.     }
  72.     :host([focused]) {
  73.         box-shadow: 0 0 2px 2px #6AE;
  74.     }

  75.     input {
  76.         border-width: 0;
  77.         outline: none;
  78.         font: inherit;
  79.         background: inherit;
  80.     }

  81.     slot {
  82.         cursor: default;
  83.         user-select: none;
  84.     }
  85.     </style>
  86.     <div>
  87.         <slot name="left">\u{1f50d}</slot>
  88.         <input type="text" id="input" />
  89.         <slot name="right">\u{2573}</slot>
  90.     </div>
  91.     `;
  92.     customElements.define("search-box",SearchBox);
  93.    
  94. </script>
複製代碼

HTML
<body>加入


  1. <search-box>
  2.             </search-box>
複製代碼
完成圖

search-box, Web,元件

search-box, Web,元件



您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-5-24 19:47 , Processed in 0.026943 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表