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

인풋 (Input)

원문 보기 위키 갱신

사용자로부터 데이터를 입력받는 컴포넌트예요. 텍스트, 번호, 이메일, 비밀번호, 전화번호, URL, 시간 등 다양한 입력 타입을 지원하며 폼의 일부로도 단독으로도 쓸 수 있어요. _ | SlInput, Shoelace 2.0부터 제공되고 있으며 stable 상태예요.

이 컴포넌트는 표준 `` 요소와 함께 동작해요. 폼 제출과 클라이언트 측 검증에 대해 더 알고 싶다면 form controls 섹션을 참고하세요.

출처: 문서

본문

| SlInput — Since 2.0, stable

인풋(Input)은 사용자로부터 데이터를 수집해요.

<sl-input></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput />;

이 컴포넌트는 표준 `` 요소와 함께 동작해요. 폼 제출과 클라이언트 측 검증에 대해 더 알고 싶다면 form controls에 관한 섹션을 참고하세요.

Examples

Labels

label 속성으로 인풋에 접근 가능한(accessible) 레이블을 달 수 있어요. HTML을 포함하는 레이블이라면 label 슬롯을 쓰세요.

<sl-input label="What is your name?"></sl-input>
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput label="What is your name?" />;

Help Text

help-text 속성으로 인풋에 설명용 도움말 텍스트를 추가할 수 있어요. HTML을 포함하는 도움말이라면 help-text 슬롯을 쓰세요.

<sl-input label="Nickname" help-text="What would you like people to call you?"></sl-input>
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput label="Nickname" help-text="What would you like people to call you?" />;

Placeholders

placeholder 속성으로 플레이스홀더를 추가할 수 있어요.

<sl-input placeholder="Type something"></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput placeholder="Type something" />;

Clearable

clearable 속성을 추가하면 인풋에 내용이 있을 때 지우기 버튼이 나타나요.

<sl-input placeholder="Clearable" clearable></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput placeholder="Clearable" clearable />;

Toggle Password

password-toggle 속성을 추가하면 비밀번호를 활성화했을 때 보여주는 토글 버튼이 생겨요.

<sl-input type="password" placeholder="Password Toggle" password-toggle></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput type="password" placeholder="Password Toggle" size="medium" password-toggle />;

Filled Inputs

filled 속성을 추가하면 채워진(filled) 스타일의 인풋을 그려줘요.

<sl-input placeholder="Type something" filled></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput placeholder="Type something" filled />;

Disabled

disabled 속성을 쓰면 인풋을 비활성화할 수 있어요.

<sl-input placeholder="Disabled" disabled></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => <SlInput placeholder="Disabled" disabled />;

Sizes

size 속성으로 인풋의 크기를 바꿀 수 있어요.

<sl-input placeholder="Small" size="small"></sl-input>
<br />
<sl-input placeholder="Medium" size="medium"></sl-input>
<br />
<sl-input placeholder="Large" size="large"></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => (
  <>
    <SlInput placeholder="Small" size="small" />
    <br />
    <SlInput placeholder="Medium" size="medium" />
    <br />
    <SlInput placeholder="Large" size="large" />
  </>
);

Pill

pill 속성을 쓰면 인풋의 모서리를 둥글게 만들 수 있어요.

<sl-input placeholder="Small" size="small" pill></sl-input>
<br />
<sl-input placeholder="Medium" size="medium" pill></sl-input>
<br />
<sl-input placeholder="Large" size="large" pill></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => (
  <>
    <SlInput placeholder="Small" size="small" pill />
    <br />
    <SlInput placeholder="Medium" size="medium" pill />
    <br />
    <SlInput placeholder="Large" size="large" pill />
  </>
);

Input Types

type 속성은 브라우저가 렌더링할 인풋의 타입을 제어해요.

<sl-input type="email" placeholder="Email"></sl-input>
<br />
<sl-input type="number" placeholder="Number"></sl-input>
<br />
<sl-input type="date" placeholder="Date"></sl-input>
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => (
  <>
    <SlInput type="email" placeholder="Email" />
    <br />
    <SlInput type="number" placeholder="Number" />
    <br />
    <SlInput type="date" placeholder="Date" />
  </>
);

Prefix & Suffix Icons

prefix와 suffix 슬롯을 써서 아이콘을 추가할 수 있어요.

<sl-input placeholder="Small" size="small">
  <sl-icon name="house" slot="prefix"></sl-icon>
  <sl-icon name="chat" slot="suffix"></sl-icon>
</sl-input>
<br />
<sl-input placeholder="Medium" size="medium">
  <sl-icon name="house" slot="prefix"></sl-icon>
  <sl-icon name="chat" slot="suffix"></sl-icon>
</sl-input>
<br />
<sl-input placeholder="Large" size="large">
  <sl-icon name="house" slot="prefix"></sl-icon>
  <sl-icon name="chat" slot="suffix"></sl-icon>
</sl-input>
import SlIcon from '@shoelace-style/shoelace/dist/react/icon';
import SlInput from '@shoelace-style/shoelace/dist/react/input';
const App = () => (
  <>
    <SlInput placeholder="Small" size="small">
      <SlIcon name="house" slot="prefix"></SlIcon>
      <SlIcon name="chat" slot="suffix"></SlIcon>
    </SlInput>
    <br />
    <SlInput placeholder="Medium" size="medium">
      <SlIcon name="house" slot="prefix"></SlIcon>
      <SlIcon name="chat" slot="suffix"></SlIcon>
    </SlInput>
    <br />
    <SlInput placeholder="Large" size="large">
      <SlIcon name="house" slot="prefix"></SlIcon>
      <SlIcon name="chat" slot="suffix"></SlIcon>
    </SlInput>
  </>
);

Customizing Label Position

CSS 파트를 쓰면 폼 컨트롤이 그려지는 방식을 커스터마이징할 수 있어요. 이 예제는 CSS grid를 사용해 레이블을 컨트롤의 왼쪽에 배치하지만, 가능한 배치는 거의 무한해요. 같은 기법이 인풋, 텍스트에어리어, 라디오 그룹 등 비슷한 폼 컨트롤에도 적용돼요.

<sl-input class="label-on-left" label="Name" help-text="Enter your name"></sl-input>
<sl-input class="label-on-left" label="Email" type="email" help-text="Enter your email"></sl-input>
<sl-textarea class="label-on-left" label="Bio" help-text="Tell us something about yourself"></sl-textarea>
<style>
  .label-on-left {
    --label-width: 3.75rem;
    --gap-width: 1rem;
  }
  .label-on-left + .label-on-left {
    margin-top: var(--sl-spacing-medium);
  }
  .label-on-left::part(form-control) {
    display: grid;
    grid: auto / var(--label-width) 1fr;
    gap: var(--sl-spacing-3x-small) var(--gap-width);
    align-items: center;
  }
  .label-on-left::part(form-control-label) {
    text-align: right;
  }
  .label-on-left::part(form-control-help-text) {
    grid-column-start: 2;
  }
</style>

Importing

autoloader나 전통적인 loader를 사용하고 있다면 이 부분은 건너뛰어도 돼요. 그 외에는 아래 스니펫 중 원하는 걸 골라 이 컴포넌트만 골라 가져올 수 있어요.

Script / Import / Bundler / React 방식이 준비되어 있어요.

CDN에서 <script> 태그로 이 컴포넌트를 가져오려면:

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

CDN에서 JavaScript import로 가져오려면:

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

Bundler를 사용해 가져오려면:

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

React 컴포넌트로 가져오려면:

import SlInput from '@shoelace-style/shoelace/dist/react/input';

Slots

Name Description
label The input’s label. Alternatively, you can use the label attribute.
prefix Used to prepend a presentational icon or similar element to the input.
suffix Used to append a presentational icon or similar element to the input.
clear-icon An icon to use in lieu of the default clear icon.
show-password-icon An icon to use in lieu of the default show password icon.
hide-password-icon An icon to use in lieu of the default hide password icon.
help-text Text that describes how to use the input. Alternatively, you can use the
help-text attribute.

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

Properties

Name Description Reflects Type Default
type The type of input. Works the same as a native `` element, but only a subset
of types are supported. Defaults to text. `
'date' | 'datetime-local' | 'email' | 'number' | 'password' | 'search' | 'tel' | 'text' | 'time' |
                  'url'
                'text'` |

| name | The name of the input, submitted as a name/value pair with form data. | | string | '' | | value | The current value of the input, submitted as a name/value pair with form data. | | string | '' | | defaultValue | The default value of the form control. Primarily used for resetting the form control. | | string | '' | | size | The input’s size. | | 'small' \| 'medium' \| 'large' | 'medium' | | filled | Draws a filled input. | | boolean | false | | pill | Draws a pill-style input with rounded edges. | | boolean | false | | label | The input’s label. If you need to display HTML, use the label slot instead. | | string | '' | | helpText help-text | The input’s help text. If you need to display HTML, use the help-text slot instead. | | string | '' | | clearable | Adds a clear button when the input is not empty. | | boolean | false | | disabled | Disables the input. | | boolean | false | | placeholder | Placeholder text to show as a hint when the input is empty. | | string | '' | | readonly | Makes the input readonly. | | boolean | false | | passwordToggle password-toggle | Adds a button to toggle the password’s visibility. Only applies to password types. | | boolean | false | | passwordVisible password-visible | Determines whether or not the password is currently visible. Only applies to password input types. | | boolean | false | | noSpinButtons no-spin-buttons | Hides the browser’s built-in increment/decrement spin buttons for number inputs. | | boolean | false | | 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` | Makes the input a required field. | | `boolean` | `false` | | `pattern` | A regular expression pattern to validate input against. | | `string` | - | | `minlength` | The minimum length of input that will be considered valid. | | `number` | - | | `maxlength` | The maximum length of input that will be considered valid. | | `number` | - | | `min` | The input’s minimum value. Only applies to date and number input types. | | `number \| string` | - | | `max` | The input’s maximum value. Only applies to date and number input types. | | `number \| string` | - | | `step` | Specifies the granularity that the value must adhere to, or the special value `any` which means no stepping is implied, allowing any numeric value. Only applies to date and number input types. | | `number \| 'any'` | - | | `autocapitalize` | Controls whether and how text input is automatically capitalized as it is entered by the user. | | `'off' \| 'none' \| 'on' \| 'sentences' \| 'words' \| 'characters'` | - | | `autocorrect` | Indicates whether the browser’s autocorrect feature is on or off. | | `'off' \| 'on'` | - | | `autocomplete` | Specifies what permission the browser has to provide assistance in filling out form field values. Refer to this page on MDN for available values. | | `string` | - | | `autofocus` | Indicates that the input should receive focus on page load. | | `boolean` | - | | `enterkeyhint` | Used to customize the label or icon of the Enter key on virtual keyboards. | | `'enter' \| 'done' \| 'go' \| 'next' \| 'previous' \| 'search' \| 'send'` | - | | `spellcheck` | Enables spell checking on the input. | | `boolean` | `true` | | `inputmode` | Tells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual keyboard on supportive devices. | | `'none' \| 'text' \| 'decimal' \| 'numeric' \| 'tel' \| 'search' \| 'email' \| 'url'` | - | | `valueAsDate` | Gets or sets the current value as a `Date` object. Returns `null` if the value can’t be converted. This will use the native implementation and may result in an error. | | - | - | | valueAsNumber | Gets or sets the current value as a number. Returns NaN if the value can’t be converted. | | - | - | | validity | Gets the validity state object | | - | - | | validationMessage | Gets the validation message | | - | - | | updateComplete | A read-only promise that resolves when the component has finished updating. | | | |

속성과 프로퍼티에 대해 더 알아보세요.

Events

Name React Event Description Event Detail
sl-blur onSlBlur Emitted when the control loses focus. -
sl-change onSlChange Emitted when an alteration to the control’s value is committed by the user. -
sl-clear onSlClear Emitted when the clear button is activated. -
sl-focus onSlFocus Emitted when the control gains focus. -
sl-input onSlInput Emitted when the control receives input. -
sl-invalid onSlInvalid Emitted when the form control has been checked for validity and its constraints aren’t satisfied. -

이벤트에 대해 더 알아보세요.

Methods

Name Description Arguments
focus() Sets focus on the input. options: FocusOptions
blur() Removes focus from the input. -
select() Selects all the text in the input. -
setSelectionRange() Sets the start and end positions of the text selection (0-based). `
selectionStart: number, selectionEnd: number, selectionDirection: 'forward' | 'backward' | 'none'
`
setRangeText() Replaces a range of text with a new string. `
replacement: string, start: number, end: number, selectMode: 'select' | 'start' | 'end' |
                  'preserve'
                ` |

| showPicker() | Displays the browser picker for an input element (only works if the browser supports it for the input type). | - | | stepUp() | Increments the value of a numeric input type by the value of the step attribute. | - | | stepDown() | Decrements the value of a numeric input type by the value of the step attribute. | - | | 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 |

메서드에 대해 더 알아보세요.

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 input’s wrapper.
form-control-help-text The help text’s wrapper.
base The component’s base wrapper.
input The internal `` control.
prefix The container that wraps the prefix.
clear-button The clear button.
password-toggle-button The password toggle button.
suffix The container that wraps the suffix.

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

Dependencies

이 컴포넌트는 다음 의존성들을 자동으로 가져와요.

  • ``

더 알아보기 (Learn more)