본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

셀렉트 (Select)

원문 보기 위키 갱신

SlSelect는 미리 정의된 옵션 메뉴에서 항목을 선택할 수 있게 해주는 요소예요. 드롭다운으로 항목 하나를 고를 때 유용합니다.

출처: 문서

본문

<sl-select>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
  <sl-option value="option-4">Option 4</sl-option>
  <sl-option value="option-5">Option 5</sl-option>
  <sl-option value="option-6">Option 6</sl-option>
</sl-select>

React에서는 이렇게 사용할 수 있어요.

import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
    <SlOption value="option-4">Option 4</SlOption>
    <SlOption value="option-5">Option 5</SlOption>
    <SlOption value="option-6">Option 6</SlOption>
  </SlSelect>
);

이 컴포넌트는 표준 `` 요소와 함께 동작해요. 폼 제출(form submission)과 클라이언트 측 유효성 검사(client-side validation)에 대해 더 자세히 알고 싶다면 폼 컨트롤 섹션을 참고하세요.

예시 (Examples)

라벨 (Labels)

label 속성을 사용하면 select에 접근 가능한(accessible) 라벨을 붙일 수 있어요. HTML이 포함된 라벨이 필요하다면 label 슬롯을 대신 사용하세요.

<sl-select label="Select one">
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect label="Select one">
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlSelect>
);

도움말 텍스트 (Help Text)

help-text 속성을 사용하면 select에 설명용 도움말 텍스트를 추가할 수 있어요. HTML이 포함된 도움말이 필요하다면 help-text 슬롯을 대신 사용하세요.

<sl-select label="Experience" help-text="Please tell us your skill level.">
  <sl-option value="1">Novice</sl-option>
  <sl-option value="2">Intermediate</sl-option>
  <sl-option value="3">Advanced</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect label="Experience" help-text="Please tell us your skill level.">
    <SlOption value="1">Novice</SlOption>
    <SlOption value="2">Intermediate</SlOption>
    <SlOption value="3">Advanced</SlOption>
  </SlSelect>
);

플레이스홀더 (Placeholders)

placeholder 속성을 사용하면 플레이스홀더를 추가할 수 있어요.

<sl-select placeholder="Select one">
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect placeholder="Select one">
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlSelect>
);

지우기 가능 (Clearable)

clearable 속성을 사용하면 컨트롤의 값을 지울 수 있게 만들 수 있어요. 지우기 버튼은 옵션이 선택된 상태에서만 나타납니다.

<sl-select clearable value="option-1">
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect placeholder="Clearable" clearable>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlSelect>
);

채워진 셀렉트 (Filled Selects)

filled 속성을 추가하면 채워진(filled) 스타일의 select를 만들 수 있어요.

<sl-select filled>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect filled>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlSelect>
);

알약 (Pill)

pill 속성을 사용하면 select에 둥근 모서리를 줄 수 있어요.

<sl-select pill>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect pill>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlSelect>
);

비활성화 (Disabled)

disabled 속성을 사용하면 select를 비활성화할 수 있어요.

<sl-select placeholder="Disabled" disabled>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect placeholder="Disabled" disabled>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlSelect>
);

다중 선택 (Multiple)

여러 옵션을 선택할 수 있게 하려면 multiple 속성을 사용하면 돼요. 이 옵션을 켤 때는 clearable을 함께 사용하는 것이 좋은 습관입니다. 여러 값을 한 번에 설정하려면 value를 공백으로 구분된 값 목록으로 지정하면 됩니다.

<sl-select label="Select a Few" value="option-1 option-2 option-3" multiple clearable>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
  <sl-option value="option-4">Option 4</sl-option>
  <sl-option value="option-5">Option 5</sl-option>
  <sl-option value="option-6">Option 6</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect label="Select a Few" value={["option-1", "option-2", "option-3"]} multiple clearable>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
    <SlOption value="option-4">Option 4</SlOption>
    <SlOption value="option-5">Option 5</SlOption>
    <SlOption value="option-6">Option 6</SlOption>
  </SlSelect>
);

다중 선택 옵션은 줄 바꿈이 일어나 컨트롤이 세로로 늘어날 수 있다는 점을 참고하세요. max-options-visible 속성으로 한 번에 보여줄 최대 선택 옵션 수를 제어할 수 있습니다.

초기 값 설정 (Setting Initial Values)

value 속성을 사용해 초기 선택 값을 설정할 수 있어요.

multiple을 사용할 때 value 속성은 공백으로 구분된 값들을 사용해서 두 개 이상의 옵션을 선택합니다. 그래서 `` 값에는 공백이 들어갈 수 없어요. JavaScript에서 value 프로퍼티에 접근하면 배열(array)로 반환됩니다.

<sl-select value="option-1 option-2" multiple clearable>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
  <sl-option value="option-4">Option 4</sl-option>
</sl-select>
import SlDivider from '@shoelace-style/shoelace/dist/react/divider';
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect value={["option-1", "option-2"]} multiple clearable>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlSelect>
);

옵션 그룹화 (Grouping Options)

를 사용하면 listbox 항목을 시각적으로 그룹화할 수 있어요. 를 사용해 라벨을 제공할 수도 있지만, 대부분의 접근성 도구가 이를 읽어주지는 않습니다.

<sl-select>
  <small>Section 1</small>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
  <sl-divider></sl-divider>
  <small>Section 2</small>
  <sl-option value="option-4">Option 4</sl-option>
  <sl-option value="option-5">Option 5</sl-option>
  <sl-option value="option-6">Option 6</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect>
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
    <SlOption value="option-4">Option 4</SlOption>
    <SlOption value="option-5">Option 5</SlOption>
    <SlOption value="option-6">Option 6</SlOption>
  </SlSelect>
);

크기 (Sizes)

size 속성을 사용하면 select의 크기를 바꿀 수 있어요. 단, 크기는 listbox 옵션에는 적용되지 않는다는 점을 참고하세요.

<sl-select placeholder="Small" size="small">
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
<br />
<sl-select placeholder="Medium" size="medium">
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
<br />
<sl-select placeholder="Large" size="large">
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <>
    <SlSelect placeholder="Small" size="small">
      <SlOption value="option-1">Option 1</SlOption>
      <SlOption value="option-2">Option 2</SlOption>
      <SlOption value="option-3">Option 3</SlOption>
    </SlSelect>
    <br />
    <SlSelect placeholder="Medium" size="medium">
      <SlOption value="option-1">Option 1</SlOption>
      <SlOption value="option-2">Option 2</SlOption>
      <SlOption value="option-3">Option 3</SlOption>
    </SlSelect>
    <br />
    <SlSelect placeholder="Large" size="large">
      <SlOption value="option-1">Option 1</SlOption>
      <SlOption value="option-2">Option 2</SlOption>
      <SlOption value="option-3">Option 3</SlOption>
    </SlSelect>
  </>
);

배치 (Placement)

select의 listbox가 표시될 선호 위치는 placement 속성으로 설정할 수 있어요. 실제 위치는 패널을 뷰포트 안에 유지하기 위해 달라질 수 있다는 점을 참고하세요. 유효한 값은 top과 bottom입니다.

<sl-select placement="top">
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <SlSelect placement="top">
    <SlOption value="option-1">Option 1</SlOption>
    <SlOption value="option-2">Option 2</SlOption>
    <SlOption value="option-3">Option 3</SlOption>
  </SlDropdown>
);

접두·접미 (Prefix & Suffix)

prefix와 suffix 슬롯을 사용하면 표현용(presentational) 아이콘이나 텍스트를 추가할 수 있어요. 버튼이나 링크 같은 상호작용 요소는 슬롯에 넣지 않는 게 좋습니다.

<sl-select placeholder="Small" size="small" clearable>
  <sl-icon name="house" slot="prefix"></sl-icon>
  <sl-badge slot="suffix">New</sl-badge>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
<br />
<sl-select placeholder="Medium" size="medium" clearable>
  <sl-icon name="house" slot="prefix"></sl-icon>
  <sl-badge slot="suffix">New</sl-badge>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
<br />
<sl-select placeholder="Large" size="large" clearable>
  <sl-icon name="house" slot="prefix"></sl-icon>
  <sl-badge slot="suffix">New</sl-badge>
  <sl-option value="option-1">Option 1</sl-option>
  <sl-option value="option-2">Option 2</sl-option>
  <sl-option value="option-3">Option 3</sl-option>
</sl-select>
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
import SlOption from '@shoelace-style/shoelace/dist/react/option';
import SlSelect from '@shoelace-style/shoelace/dist/react/select';
const App = () => (
  <>
    <SlSelect placeholder="Small" size="small">
      <SlIcon name="house" slot="prefix"></SlIcon>
      <SlOption value="option-1">Option 1</SlOption>
      <SlOption value="option-2">Option 2</SlOption>
      <SlOption value="option-3">Option 3</SlOption>
    </SlSelect>
    <br />
    <SlSelect placeholder="Medium" size="medium">
      <SlIcon name="house" slot="prefix"></SlIcon>
      <SlOption value="option-1">Option 1</SlOption>
      <SlOption value="option-2">Option 2</SlOption>
      <SlOption value="option-3">Option 3</SlOption>
    </SlSelect>
    <br />
    <SlSelect placeholder="Large" size="large">
      <SlIcon name="house" slot="prefix"></SlIcon>
      <SlOption value="option-1">Option 1</SlOption>
      <SlOption value="option-2">Option 2</SlOption>
      <SlOption value="option-3">Option 3</SlOption>
    </SlSelect>
  </>
);

커스텀 태그 (Custom Tags)

여러 옵션을 선택할 수 있을 때 getTag 프로퍼티에 함수를 넘겨주면 커스텀 태그를 제공할 수 있어요. 함수는 HTML 문자열, Lit Template, 또는 HTMLElement를 반환할 수 있습니다. getTag() 함수는 각 옵션에 대해 호출되며, 첫 번째 인자는 `` 요소, 두 번째 인자는 태그의 인덱스(태그 목록에서의 위치)입니다.

커스텀 태그는 shadow root 안에서 렌더링된다는 점을 기억하세요. 스타일을 지정하려면 템플릿에서 style 속성을 사용하거나, 직접 파트(parts)를 추가하고 ::part() 선택자로 타겟팅하면 됩니다.

<sl-select
  placeholder="Select one"
  value="email phone"
  multiple
  clearable
  class="custom-tag"
>
  <sl-option value="email">
    <sl-icon slot="prefix" name="envelope"></sl-icon>
    Email
  </sl-option>
  <sl-option value="phone">
    <sl-icon slot="prefix" name="telephone"></sl-icon>
    Phone
  </sl-option>
  <sl-option value="chat">
    <sl-icon slot="prefix" name="chat-dots"></sl-icon>
    Chat
  </sl-option>
</sl-select>
<script type="module">
  const select = document.querySelector('.custom-tag');
  select.getTag = (option, index) => {
    // Use the same icon used in the <sl-option>
    const name = option.querySelector('sl-icon[slot="prefix"]').name;
    // You can return a string, a Lit Template, or an HTMLElement here
    return `
      <sl-tag removable>
        <sl-icon name="${name}" style="padding-inline-end: .5rem;"></sl-icon>
        ${option.getTextLabel()}
      </sl-tag>
    `;
  };
</script>

옵션 지연 로딩 (Lazy loading options)

옵션의 지연 로딩은 제대로 구현하기가 매우 까다로워요. 은 대체로 네이티브 의 동작 방식을 따릅니다.

조건은 다음과 같아요:

  • 옵션 없이 생성됐지만 value 속성이 부여된 가 있다면, 그 `value`는 `""`가 되고, 이후 옵션이 추가됐을 때 어떤 옵션의 값이 의 값과 같다면 의 값은 그 옵션의 값과 같아집니다. 예: 은 Foo가 연결되기 전까지 값이 ""이고, 연결되면 제출 시 값이 "foo"가 됩니다.
  • 초기 값이 있는데 여러 값 중 일부 옵션만 존재하는 ``라면, 존재하는 옵션만 존중합니다. 그리고 나중에 선택된 옵션이 로드됐는데, 값이 사용자 상호작용이나 직접적인 프로퍼티 할당으로 바뀌지 않았다면, 선택된 옵션을 폼 값과 select의 .value에 추가합니다.

이 개념은 이해하기 어려울 수 있어서, 초기 값 속성이 주어졌을 때 지연 로딩 옵션이 및와 어떻게 동작하는지 보여주는 비교적 큰 예시를 준비했어요. codepen에서 직접 만져보면서 확인해 보세요.

<form id="lazy-options-example">
  <div>
    <sl-select name="select-1" value="foo" label="Single select (with existing options)">
      <sl-option value="bar">Bar</sl-option>
      <sl-option value="baz">Baz</sl-option>
    </sl-select>
    <br>
    <sl-button type="button">Add "foo" option</sl-button>
  </div>
  <br>
  <div>
    <sl-select name="select-2" value="foo" label="Single select (with no existing options)">
    </sl-select>
    <br>
    <sl-button type="button">Add "foo" option</sl-button>
  </div>
  <br>
  <div>
    <sl-select name="select-3" value="foo bar baz" multiple label="Multiple Select (with existing options)">
      <sl-option value="bar">Bar</sl-option>
      <sl-option value="baz">Baz</sl-option>
    </sl-select>
    <br>
    <sl-button type="button">Add "foo" option</sl-button>
  </div>
  <br>
  <div>
    <sl-select name="select-4" value="foo" multiple label="Multiple Select (with no existing options)">
    </sl-select>
    <br>
    <sl-button type="button">Add "foo" option</sl-button>
  </div>
  <br><br>
  <div style="display: flex; gap: 16px;">
    <sl-button type="reset">Reset</sl-button>
    <sl-button type="submit" variant="brand">Show FormData</sl-button>
  </div>
  <br>
  <pre hidden><code id="lazy-options-example-form-data"></code></pre>
  <br>
</form>
<script type="module">
  function addFooOption(e) {
    const addFooButton = e.target.closest("sl-button[type='button']")
    if (!addFooButton) {
      return
    }
    const select = addFooButton.parentElement.querySelector("sl-select")
    if (select.querySelector("sl-option[value='foo']")) {
      // Foo already exists. no-op.
      return
    }
    const option = document.createElement("sl-option")
    option.setAttribute("value", "foo")
    option.innerText = "Foo"
    select.append(option)
  }
  function handleLazySubmit (event) {
    event.preventDefault()
    const formData = new FormData(event.target)
    const codeElement = document.querySelector("#lazy-options-example-form-data")
    const obj = {}
    for (const key of formData.keys()) {
      const val = formData.getAll(key).length > 1 ? formData.getAll(key) : formData.get(key)
      obj[key] = val
    }
    codeElement.textContent = JSON.stringify(obj, null, 2)
    const preElement = codeElement.parentElement
    preElement.removeAttribute("hidden")
  }
  const container = document.querySelector("#lazy-options-example")
  container.addEventListener("click", addFooOption)
  container.addEventListener("submit", handleLazySubmit)
</script>

출력하는 콘텐츠를 반드시 신뢰할 수 있는지 확인하세요! getTag()에 검증되지 않은 사용자 입력을 넘기면 XSS 취약점이 발생할 수 있습니다.

불러오기 (Importing)

autoloader나 전통적인 loader를 사용 중이라면 이 부분은 무시해도 돼요. 그게 아니라면 아래 스니펫 중 원하는 방식으로 이 컴포넌트만 골라서 불러올 수 있어요.

Script 태그로 CDN에서 이 컴포넌트를 불러오려면:

<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/select/select.js"></script>

JavaScript import 구문으로 CDN에서 불러오려면:

import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.20.1/cdn/components/select/select.js';

번들러(bundler)를 사용해 불러오려면:

import '@shoelace-style/shoelace/dist/components/select/select.js';

React 컴포넌트로 불러오려면:

import SlSelect from '@shoelace-style/shoelace/dist/react/select';

슬롯 (Slots)

Name Description
(default) The listbox options. Must be elements. You can use to group items visually.
label The input’s label. Alternatively, you can use the label attribute.
prefix Used to prepend a presentational icon or similar element to the combobox.
suffix Used to append a presentational icon or similar element to the combobox.
clear-icon An icon to use in lieu of the default clear icon.
expand-icon The icon to show when the control is expanded and collapsed. Rotates on open and close.
help-text Text that describes how to use the input. Alternatively, you can use the help-text attribute.

슬롯 사용법에 대해 더 알아보기.

속성 (Properties)

Name Description Reflects Type Default
name The name of the select, submitted as a name/value pair with form data. string ''
value The current value of the select, submitted as a name/value pair with form data. When multiple is enabled, the value attribute will be a space-delimited list of values based on the options selected, and the value property will be an array. For this reason, values must not contain spaces. - -
defaultValue value The default value of the form control. Primarily used for resetting the form control. string | string[] ''
size The select’s size. 'small' | 'medium' | 'large' 'medium'
placeholder Placeholder text to show as a hint when the select is empty. string ''
multiple Allows more than one option to be selected. boolean false
maxOptionsVisible max-options-visible The maximum number of selected options to show when multiple is true. After the maximum, ”+n” will be shown to indicate the number of additional items that are selected. Set to 0 to remove the limit. number 3
disabled Disables the select control. boolean false
clearable Adds a clear button when the select is not empty. boolean false
open Indicates whether or not the select is open. You can toggle this attribute to show and hide the menu, or you can use the show() and hide() methods and this attribute will reflect the select’s open state. boolean false
hoist Enable this option to prevent the listbox from being clipped when the component is placed inside a container with overflow: auto|scroll. Hoisting uses a fixed positioning strategy that works in many, but not all, scenarios. boolean false
filled Draws a filled select. boolean false
pill Draws a pill-style select with rounded edges. boolean false
label The select’s label. If you need to display HTML, use the label slot instead. string ''
placement The preferred placement of the select’s menu. Note that the actual placement may vary as needed to keep the listbox inside of the viewport. 'top' | 'bottom' 'bottom'
helpText help-text The select’s help text. If you need to display HTML, use the help-text slot instead. string ''
form By default, form controls are associated with the nearest containing `` element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work. string ''
required The select’s required attribute. boolean false
getTag A function that customizes the tags to be rendered when multiple=true. The first argument is the option, the second is the current tag’s index. The function should return either a Lit TemplateResult or a string containing trusted HTML of the symbol to render at the specified value. (option: SlOption, index: number) => TemplateResult | string | HTMLElement -
validity Gets the validity state object - -
validationMessage Gets the validation message - -
updateComplete A read-only promise that resolves when the component has finished updating.

속성(attribute)과 프로퍼티(property)에 대해 더 알아보기.

이벤트 (Events)

Name React Event Description Event Detail
sl-change onSlChange Emitted when the control’s value changes. -
sl-clear onSlClear Emitted when the control’s value is cleared. -
sl-input onSlInput Emitted when the control receives input. -
sl-focus onSlFocus Emitted when the control gains focus. -
sl-blur onSlBlur Emitted when the control loses focus. -
sl-show onSlShow Emitted when the select’s menu opens. -
sl-after-show onSlAfterShow Emitted after the select’s menu opens and all animations are complete. -
sl-hide onSlHide Emitted when the select’s menu closes. -
sl-after-hide onSlAfterHide Emitted after the select’s menu closes and all animations are complete. -
sl-invalid onSlInvalid Emitted when the form control has been checked for validity and its constraints aren’t satisfied. -

이벤트에 대해 더 알아보기.

메서드 (Methods)

Name Description Arguments
show() Shows the listbox. -
hide() Hides the listbox. -
checkValidity() Checks for validity but does not show a validation message. Returns true when valid and false when invalid. -
getForm() Gets the associated form, if one exists. -
reportValidity() Checks for validity and shows the browser’s validation message if the control is invalid. -
setCustomValidity() Sets a custom validation message. Pass an empty string to restore validity. message: string
focus() Sets focus on the control. options: FocusOptions
blur() Removes focus from the control. -

메서드에 대해 더 알아보기.

파트 (Parts)

Name Description
form-control The form control that wraps the label, input, and help text.
form-control-label The label’s wrapper.
form-control-input The select’s wrapper.
form-control-help-text The help text’s wrapper.
combobox The container the wraps the prefix, suffix, combobox, clear icon, and expand button.
prefix The container that wraps the prefix slot.
suffix The container that wraps the suffix slot.
display-input The element that displays the selected option’s label, an `` element.
listbox The listbox container where options are slotted.
tags The container that houses option tags when multiselect is used.
tag The individual tags that represent each multiselect option.
tag__base The tag’s base part.
tag__content The tag’s content part.
tag__remove-button The tag’s remove button.
tag__remove-button__base The tag’s remove button base part.
clear-button The clear button.
expand-icon The container that wraps the expand icon.

CSS 파트 커스터마이징에 대해 더 알아보기.

의존성 (Dependencies)

이 컴포넌트는 다음 의존성을 자동으로 불러옵니다.

  • ``
  • ``
  • ``
  • ``

더 알아보기 (Learn more)