TextInput
한 줄의 짧은 자유 텍스트 값을 입력받는 데 사용하는 컴포넌트입니다. type="number"를 넘기면 숫자 입력으로도 쓸 수 있어요. 형식이 자유로운 짧은 텍스트를 입력받는 폼에 가장 기본적으로 쓰입니다.
출처: 문서
본문
TextInput은 한 줄의 텍스트 값(text value)을 설정할 때 사용합니다.
사용자가 짧은 자유 형식 텍스트를 입력하도록 하려면 TextInput을 사용하세요. 컴포넌트에 type="number"가 전달되면 숫자 입력으로도 사용할 수 있습니다.
React 예시
기본 (Default)
<FormControl>
<FormControl.Label>Label</FormControl.Label>
<TextInput />
</FormControl>
필수 (Required)
<FormControl required>
<FormControl.Label>Label</FormControl.Label>
<TextInput />
</FormControl>
커스텀 크기 (Custom size)
상황에 맞게 입력 크기를 조정하세요. 빽빽한 공간에서는 더 작은 크기를, 넓은 공간이나 인접한 큰 요소와 비례가 맞지 않을 때는 더 큰 크기를 사용합니다.
<Stack direction="horizontal">
<FormControl>
<FormControl.Label>Small</FormControl.Label>
<TextInput size="small" />
</FormControl>
<FormControl>
<FormControl.Label>Medium (default)</FormControl.Label>
<TextInput />
</FormControl>
<FormControl>
<FormControl.Label>Large</FormControl.Label>
<TextInput size="large" />
</FormControl>
</Stack>
모노스페이스 값 (Monospace value)
구성 토큰(configuration token) 같은 코드 관련 입력에는 모노스페이스 폰트를 사용하세요.
<FormControl>
<FormControl.Label>Label</FormControl.Label>
<TextInput monospace defaultValue="Value" />
</FormControl>
로딩 상태 (In a loading state)
로딩 중일 때 스피너가 레이아웃을 이동시키지 않고 시각적 요소를 대체합니다. 필요에 따라 어느 쪽에 로딩 인디케이터를 표시할지 제어할 수 있습니다.
TextInput 컴포넌트는 어떤 시각적 요소를 대체할지 결정하는 내부 로직이 있지만, 이것을 오버라이드해서 입력의 시작 또는 끝에 표시하도록 명시적으로 설정할 수 있습니다.
<Stack gap="spacious">
<div>
<div>Default spinner position</div>
<Stack>
<FormControl>
<FormControl.Label>No leading/trailing visual</FormControl.Label>
<TextInput loading />
</FormControl>
<FormControl>
<FormControl.Label>Leading visual</FormControl.Label>
<TextInput leadingVisual={CalendarIcon} loading />
</FormControl>
<FormControl>
<FormControl.Label>Trailing visual</FormControl.Label>
<TextInput trailingVisual={CalendarIcon} loading />
</FormControl>
<FormControl>
<FormControl.Label>Both visuals</FormControl.Label>
<TextInput leadingVisual={CalendarIcon} trailingVisual={CalendarIcon} loading />
</FormControl>
</Stack>
</div>
<div>
<div>Overridden spinner position</div>
<Stack>
<FormControl>
<FormControl.Label>Leading</FormControl.Label>
<TextInput leadingVisual={CalendarIcon} trailingVisual={CalendarIcon} loading loaderPosition="leading" />
</FormControl>
<FormControl>
<FormControl.Label>Trailing</FormControl.Label>
<TextInput leadingVisual={CalendarIcon} trailingVisual={CalendarIcon} loading loaderPosition="trailing" />
</FormControl>
</Stack>
</div>
</Stack>
캡션과 함께 (With a caption)
<FormControl>
<FormControl.Label>Label</FormControl.Label>
<TextInput />
<FormControl.Caption>This is a caption</FormControl.Caption>
</FormControl>
검증 메시지와 함께 (With a validation message)
<FormControl>
<FormControl.Label>Label</FormControl.Label>
<TextInput />
<FormControl.Validation variant="error">Something went wrong</FormControl.Validation>
</FormControl>
앞쪽 시각 요소와 함께 (With a leading visual)
앞쪽(leading)과 뒤쪽(trailing) 시각 요소, 보통 아이콘은 입력의 용도를 명확히 해줍니다. 맥락을 위해 아이콘을 선호하되, 도움이 된다면 텍스트(예: "$" 또는 "%")를 사용해도 좋습니다.
앞쪽과 뒤쪽 시각 요소를 둘 다 사용해서 TextInput을 과하게 채우는 것은 피하세요. 입력에 대해 더 많은 맥락을 제공하고 싶다면 입력 캡션에 텍스트를 사용하는 것을 고려해 보세요.
<FormControl>
<FormControl.Label>Label</FormControl.Label>
<TextInput leadingVisual={CheckIcon} />
</FormControl>
뒤쪽 시각 요소와 함께 (With a trailing visual)
<FormControl>
<FormControl.Label>Label</FormControl.Label>
<TextInput trailingVisual={CheckIcon} />
</FormControl>
뒤쪽 동작과 함께 (With a trailing action)
가능하면 라벨이 없는 뒤쪽 동작 버튼(예: 지우기 버튼)을 사용하세요. 뒤쪽 시각 요소와 함께 사용하는 것은 피하세요.
<FormControl>
<FormControl.Label>Label</FormControl.Label>
<TextInput
trailingAction={
<TextInput.Action
onClick={() => {
alert('Fake clear the input')
}}
icon={XCircleFillIcon}
aria-label="Clear input"
/>
}
/>
</FormControl>
더 많은 코드 예시 (More code examples)
TextInput Storybook 스토리를 참고하세요.
Props
TextInput
| Name | Default | Description |
|---|---|---|
| aria-label | — | string — 입력에 접근 가능한 이름을 부여합니다. |
| autoComplete | — | string — 주어진 값에 따라 입력을 자동 채우기할 수 있게 합니다. |
| block | false |
boolean — 전체 너비 입력 요소를 만듭니다. |
| characterLimit | — | number — 입력에 허용되는 최대 문자 수입니다. |
| contrast | false |
boolean — 배경색을 더 높은 대비 색으로 바꿉니다. |
| size | — | 'small' | 'medium' | 'large' — 기본보다 작거나 큰 입력을 만듭니다. |
| loading | — | boolean — 입력에 로딩 인디케이터를 표시할지 여부입니다. |
| loaderPosition | 'auto' |
'auto' | 'leading' | 'trailing' — 로딩 인디케이터를 렌더링할 위치입니다. 'auto'(기본): leadingVisual이 전달되지 않으면 입력 끝에, 전달되면 시작에 렌더링됩니다. 'leading': 입력 시작. 'trailing': 입력 끝. |
| loaderText | 'Loading' |
string — 로딩 상태를 전달하기 위한 화면 낭독기용 텍스트입니다. 무엇이 로딩 중인지 설명하는 구체적인 내용이어야 합니다. 이 prop은 로딩 중임을 설명하는 보이는 컨텍스트가 있을 때만 사용해 모든 사용자에게 맥락이 제공되게 하세요. |
| leadingVisual | — | string | React.ComponentType — 입력 안 왼쪽 가장자리에 위치하는 시각 요소입니다. |
| monospace | false |
boolean — 입력을 모노스페이스 폰트로 표시합니다. |
| trailingVisual | — | string | React.ComponentType — 입력 안 오른쪽 가장자리에 위치하는 시각 요소입니다. |
| trailingAction | — | React.ReactElement<HTMLProps<HTMLButtonElement>> — 입력 안에서 타이핑 영역 뒤에 렌더링되는 시각 요소입니다. |
| validationStatus | — | 'error' | 'success' — 상태에 맞게 입력을 스타일링합니다. |
| variant | Deprecated | 'small' | 'medium' | 'large' — (size 사용) 기본보다 작거나 큰 입력을 만듭니다. |
| width | Deprecated | string | number — (css modules 사용) 입력의 너비를 설정합니다. |
| maxWidth | Deprecated | string | number — (css modules 사용) 입력의 최대 너비를 설정합니다. |
| minWidth | Deprecated | string | number — (css modules 사용) 입력의 최소 너비를 설정합니다. |
| icon | Deprecated | React.ComponentType — (leadingVisual 또는 trailingVisual 사용) input 안에서 사용하는 Octicon React 컴포넌트입니다. 왼쪽 가장자리에 위치합니다. |
| ref | — | React.RefObject<HTMLInputElement> — |
TextInput.Action
| Name | Default | Description |
|---|---|---|
| aria-label | — | string — 툴팁에 나타나는 텍스트입니다. 아이콘이 전달되면 이 값은 보조 기술이 사용하는 라벨로도 쓰입니다. |
| tooltipDirection | s |
'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw' — 툴팁이 대상에 대해 어느 위치에 렌더링될지 설정합니다. |
| icon | — | React.FunctionComponent — 버튼 안에 렌더링할 아이콘입니다. |
| variant | Deprecated | 'default' | 'primary' | 'invisible' | 'danger' — (오직 'invisible' 변형만 쓰도록 폐기됨) 버튼의 스타일을 결정합니다. |
| ref | — | React.RefObject<HTMLButtonElement> — |
| as | "button" |
React.ElementType — 더 많은 prop 옵션은 button을 참고하세요. |
관련 링크
- Forms
- Autocomplete
- TextInputWithTokens