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

Input

원문 보기 위키 갱신

Field와 자동으로 연동되는 네이티브 input 요소를 소개할게요.

출처: Base UI Input

본문

품질이 높고 비스타일(unstyled)인 React input 컴포넌트예요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.tsx */
import { Input } from '@base-ui/react/input';

export default function ExampleInput() {
  return (
    <label className="flex flex-col items-start gap-1 text-sm font-bold text-neutral-950 dark:text-white">
      Name
      <Input
        placeholder="e.g. Colm Tuite"
        className="h-8 w-40 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 dark:text-white placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white"
      />
    </label>
  );
}

CSS Modules

이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.

/* index.module.css */
.Label {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 10rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
/* index.tsx */
import { Input } from '@base-ui/react/input';
import styles from './index.module.css';

export default function ExampleInput() {
  return (
    <label className={styles.Label}>
      Name
      <Input placeholder="e.g. Colm Tuite" className={styles.Input} />
    </label>
  );
}

사용 가이드라인 (Usage guidelines)

  • 폼 컨트롤에는 접근 가능한 이름이 필요해요: <label> 요소나 Field 컴포넌트로 만들 수 있어요. forms 가이드를 참고하세요.

구조 (Anatomy)

컴포넌트를 임포트해서 단일 파트로 사용해요:

import { Input } from '@base-ui/react/input';

<Input />;

API 레퍼런스

Input

Field와 자동으로 연동되는 네이티브 input 요소예요. <input> 요소를 렌더링해요.

Input Props:

Prop Type Default Description
defaultValue string | number | string[] - input의 기본값이에요. 비제어(uncontrolled)로 사용할 때 써요.
value string | string[] | number - input의 값이에요. 제어(controlled)로 사용할 때 써요.
onValueChange ((value: string, eventDetails: Input.ChangeEventDetails) => void) - value가 변경될 때 호출되는 콜백이에요. 제어로 사용할 때 써요.
className string | ((state: Input.State) => string | undefined) - 요소에 적용되는 CSS 클래스이거나, 컴포넌트 상태에 따라 클래스를 반환하는 함수예요.
style React.CSSProperties | ((state: Input.State) => React.CSSProperties | undefined) - 요소에 적용되는 스타일이거나, 컴포넌트 상태에 따라 스타일 객체를 반환하는 함수예요.
render ReactElement | ((props: HTMLProps, state: Input.State) => ReactElement) - 컴포넌트의 HTML 요소를 다른 태그로 바꾸거나 다른 컴포넌트와 합성할 수 있게 해줘요. 렌더링할 요소를 반환하는 ReactElement나 함수를 받아요.

Input Data Attributes:

Attribute Type Description
data-disabled - input이 비활성화되어 있을 때 나타나요.
data-valid - input이 유효한 상태일 때 나타나요 (Field.Root로 감싸져 있을 때).
data-invalid - input이 유효하지 않은 상태일 때 나타나요 (Field.Root로 감싸져 있을 때).
data-dirty - input의 값이 변경되었을 때 나타나요 (Field.Root로 감싸져 있을 때).
data-touched - input이 터치되었을 때 나타나요 (Field.Root로 감싸져 있을 때).
data-filled - input이 채워져 있을 때 나타나요 (Field.Root로 감싸져 있을 때).
data-focused - input에 포커스되어 있을 때 나타나요 (Field.Root로 감싸져 있을 때).

Input.Props

Input props의 재수출이에요.

Input.State

type InputState = {
  /** Whether the component should ignore user interaction. */
  disabled: boolean;
  /** Whether the field has been touched. */
  touched: boolean;
  /** Whether the field value has changed from its initial value. */
  dirty: boolean;
  /** Whether the field is valid. */
  valid: boolean | null;
  /** Whether the field has a value. */
  filled: boolean;
  /** Whether the field is focused. */
  focused: boolean;
};

Input.ChangeEventReason

type InputChangeEventReason = 'none';

Input.ChangeEventDetails

type InputChangeEventDetails = {
  /** The reason for the event. */
  reason: 'none';
  /** The native event associated with the custom event. */
  event: Event;
  /** Cancels Base UI from handling the event. */
  cancel: () => void;
  /** Allows the event to propagate in cases where Base UI will stop the propagation. */
  allowPropagation: () => void;
  /** Indicates whether the event has been canceled. */
  isCanceled: boolean;
  /** Indicates whether the event is allowed to propagate. */
  isPropagationAllowed: boolean;
  /** The element that triggered the event, if applicable. */
  trigger: Element | undefined;
};

Canonical Types

Canonical: Alias 매핑 — 네임스페이스가 이미 임포트되어 있으면 Canonical을, 그렇지 않으면 Alias를 사용해요.

  • Input.Props: InputProps
  • Input.State: InputState
  • Input.ChangeEventReason: InputChangeEventReason
  • Input.ChangeEventDetails: InputChangeEventDetails

더 알아보기 (Learn more)