<select> 에는 placeholder 속성이 없다 — 값이 "" 인 첫 option 을 disabled 로 다시 못 고르게, hidden 으로 목록에서 숨긴다. 필수 입력(required)이면 스펙상 그 option 이 placeholder label option 이 되어, 고르지 않고 제출하면 막힌다.1 React 에서는 <option selected> 대신 <select defaultValue=""> 로 기본 선택한다.2
function Select({ placeholder, children }) {
return (
<select defaultValue="">
<option value="" disabled hidden>
{placeholder}
</option>
{children}
</select>
)
}
Footnotes
-
HTML Standard — the select element: “If a select element has a required attribute specified, and has a display size of 1; and if the value of the first option element in the select element’s list of options (if any) is the empty string, … then that option is the select element’s placeholder label option.” ↩
-
: “Unlike in HTML, passing a
selectedattribute to<option>is not supported. Instead, use<select defaultValue>for uncontrolled select boxes and<select value>for controlled select boxes.” ↩
Building a multi-select component
다중 선택 UI를 구현하기위해서 checkbox, select 두가지 방법으로 작업하는 방식을 소개하고 있다. 그외 선택된 상태값을 얻기위한 counter() 함수, 모바일 체크를 위한 미디어쿼리도 알려주고 있다. 개수 세기는 JS 없이 CSS 카운터로 된다.1
aside {
counter-reset: filters;
& :checked {
counter-increment: filters;
}
&::after {
content: counter(filters);
}
}
@media (pointer: coarse) {
/* … */
}
Footnotes
-
Using CSS counters - MDN: “Once initialized, a counter’s value can be increased or decreased using counter-increment.” / “The value of a counter can be displayed using either the counter() or counters() function in a content property.” ↩
Under-Engineered Select Menus | Adrian Roselli
font,letter-spacing,word-spacing상속appearance화살표 수정- 상태(focus, required, invalid)에 따른 스타일 추가
field-sizing를 사용하면 콘텐츠를 기반으로 크기 조절을 사용 설정하는 데 CSS 한 줄이 필요합니다. 이 콘텐츠 기반 크기 조절 스타일은textarea외에도 다른 요소에도 적용됩니다.