การโต้ตอบกับเมนูการเลือก

ฟีเจอร์บนเว็บ: :user-valid และ :user-invalid

ปัญหา

สำหรับเมนูแบบเลื่อนลงที่ต้องกรอก (เช่น "เลือกประเทศ") การตรวจสอบมาตรฐานจะแจ้งว่าช่องไม่ถูกต้องทันทีหากตัวเลือกเริ่มต้นมีค่าว่างเปล่า ซึ่งอาจทำให้เกิดสัญญาณรบกวนในภาพ เราต้องการแสดงข้อผิดพลาดก็ต่อเมื่อผู้ใช้เปิดเมนูและปิดโดยไม่ได้เลือกตัวเลือก หรือพยายามส่งแบบฟอร์มเท่านั้น

วิธีแก้ปัญหา

คลาสเทียม :user-invalid ทำงานร่วมกับองค์ประกอบ <select> ได้อย่างราบรื่น ซึ่งจะเคารพขั้นตอนการโต้ตอบของผู้ใช้ กล่าวคือ การโหลดหน้าเว็บหรือการโฟกัส/เบลอโดยไม่ทำการเปลี่ยนแปลงจะไม่นับเป็นการโต้ตอบ ดังนั้นช่องจะยังคงเป็นกลางจนกว่าผู้ใช้จะพยายามเลือกอย่างจริงจัง

กลยุทธ์การติดตั้งใช้งาน

  1. ข้อจำกัด HTML: ใช้ <select> กับ required ตัวเลือกแรกควรมี value="" และควรปิดใช้/ซ่อนไว้เพื่อบังคับให้เลือกตัวเลือกที่ถูกต้อง
  2. การตอบสนองด้วยภาพ: ใช้ :user-invalid เพื่อจัดรูปแบบเส้นขอบของช่องเลือก
  3. เวลา: เบราว์เซอร์จะถือว่าช่อง "มีการโต้ตอบ" หากผู้ใช้เปลี่ยนค่า (แม้จะเปลี่ยนกลับเป็นสถานะไม่ถูกต้องเริ่มต้น) ก่อนที่จะเบลอตัวควบคุม หรือเมื่อส่งแบบฟอร์ม

คู่มือการติดตั้งใช้งาน

1. โครงสร้าง HTML

ตัวเลือก "placeholder" เป็นกุญแจสำคัญในที่นี้

<form>
  <div class="field">
    <label for="country">Country</label>
    <select
      id="country"
      name="country"
      required
      aria-errormessage="country-error"
    >
      <option value="" disabled selected>Select a country...</option>
      <option value="us">United States</option>
      <option value="ca">Canada</option>
      <option value="uk">United Kingdom</option>
    </select>
    <div id="country-error" class="error-msg">
      Please select a country.
    </div>
  </div>
</form>

2. CSS

.error-msg {
  display: none;
  color: #d93025;
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

/*
  Only show error after the user visits the select menu.
*/
select:user-invalid {
  border-color: #d93025;
  background-color: #fce8e6;
}

select:user-invalid + .error-msg {
  display: block;
}

select:user-valid {
  border-color: #188038;
}

การสำรองและการรองรับเบราว์เซอร์

การสนับสนุนเบราว์เซอร์และทางเลือกสำรองสำหรับ :user-valid และ :user-invalid

CSS สำหรับโฆษณาสำรอง

input:user-invalid,
input.user-invalid-fallback {
  border-color: #d93025;
  background-color: #fce8e6;
}

input:user-invalid + .error-msg,
input.user-invalid-fallback + .error-msg {
  display: block;
}

JavaScript สำรอง

ใช้ยูทิลิตีที่นำกลับมาใช้ใหม่ได้ซึ่งติดตามสถานะการโต้ตอบโดยใช้ WeakMap วิธีนี้จะช่วยหลีกเลี่ยงการทำให้ DOM มีคลาสหรือแอตทริบิวต์ข้อมูลที่ "ไม่ถูกต้อง"

const UserInvalidFallback = (() => {
  const dirtyState = new WeakMap();

  const updateState = (input) => {
    const isValid = input.checkValidity();

    // Update both visual and ARIA state
    input.classList.toggle('user-invalid-fallback', !isValid);
    input.classList.toggle('user-valid-fallback', isValid);

    if (!isValid) {
      input.setAttribute('aria-invalid', 'true');
    } else {
      input.removeAttribute('aria-invalid');
    }
  };

  const handleEvent = (event) => {
    const input = event.target;

    if (event.type === 'reset') {
      const controls = input.elements || [];
      for (const control of controls) {
        dirtyState.delete(control);
        control.classList.remove('user-invalid-fallback');
        control.classList.remove('user-valid-fallback');
        control.removeAttribute('aria-invalid');
      }
      return;
    }

    if (!input.checkValidity) return;

    if (event.type === 'input' || event.type === 'change') {
      const state = dirtyState.get(input) || { hasInteracted: false, hasBlurred: false };
      state.hasInteracted = true;
      dirtyState.set(input, state);
      if (state.hasBlurred) {
        updateState(input);
      }
    } else if (event.type === 'blur') {
      const state = dirtyState.get(input) || { hasInteracted: false, hasBlurred: false };
      state.hasBlurred = true;
      dirtyState.set(input, state);
      if (state.hasInteracted) {
        updateState(input);
      }
    }
  };

  const init = (root = document) => {
    if (CSS.supports('selector(:user-invalid)')) return;

    root.addEventListener('blur', handleEvent, true); // Capture phase
    root.addEventListener('input', handleEvent);
    root.addEventListener('change', handleEvent);
    root.addEventListener('reset', handleEvent, true); // Capture resets
  };

  return { init };
})();

// Initialize for a specific form
const form = document.querySelector('#demo-form');
UserInvalidFallback.init(form);

ปัจจัยอื่นๆ ที่ควรพิจารณา

  1. ลักษณะการทำงานบนอุปกรณ์เคลื่อนที่: ในอุปกรณ์เคลื่อนที่ "การเบลอ" อาจเกิดขึ้นแตกต่างกันไปตามเครื่องมือเลือกของระบบปฏิบัติการ เราขอแนะนำให้ทดสอบในอุปกรณ์จริง
  2. การช่วยเหลือพิเศษ: :user-invalid แบบเนทีฟจะไม่ซิงค์กับแอตทริบิวต์ ARIA โดยอัตโนมัติ เพิ่ม JavaScript ต่อไปนี้เพื่อให้ aria-invalid ซิงค์กับสถานะภาพ
// Sync aria-invalid with the CSS :user-invalid state
const syncAria = (el) => {
  el.setAttribute?.('aria-invalid', el.matches(':user-invalid') ? 'true' : 'false');
};

// Update on blur (to show error) and input (to clear it)
document.addEventListener('blur', (e) => syncAria(e.target), true);
document.addEventListener('input', (e) => {
  if (e.target.hasAttribute('aria-invalid')) syncAria(e.target);
});