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

InputBase API

원문 보기 위키 갱신

InputBase API (입력 베이스 API)

InputBase는 가능한 한 최소한의 스타일만 포함하는, 입력 필드를 만들기 위한 기초적인 빌딩 블록 컴포넌트예요. 이 페이지에서는 이 컴포넌트의 props, CSS 클래스, 그리고 기타 API를 하나씩 설명해 드릴게요.

출처: 문서

본문

데모 (Demos)

이 React 컴포넌트의 사용법과 세부 사항은 컴포넌트 데모 페이지에서 확인할 수 있어요.

  • Text Field

Import

import InputBase from '@mui/material/InputBase';
// 또는
import { InputBase } from '@mui/material';

번들 크기를 줄이는 방법이 궁금하시다면, 이 차이에 대한 가이드 문서를 읽어 보시면 좋아요.

InputBase는 가능한 한 적은 스타일만 포함해요. 입력을 만들기 위한 단순한 빌딩 블록을 지향하며, 많은 스타일 리셋과 일부 상태 로직을 포함하고 있어요.

Props

네이티브 컴포넌트의 props도 그대로 사용할 수 있어요.

Name Type Default Description
autoComplete string - This prop helps users to fill forms faster, especially on mobile devices. The name can be confusing, as it's more like an autofill. You can learn more about it following the specification.
autoFocus bool - If true, the input element is focused during the first mount.
classes object - Override or extend the styles applied to the component. See CSS classes API below for more details.
color 'primary'| 'secondary'| 'error'| 'info'| 'success'| 'warning'| string - The color of the component. It supports both default and custom theme colors, which can be added as shown in the palette customization guide. The prop defaults to the value ('primary') inherited from the parent FormControl component.
defaultValue any - The default value. Use when the component is not controlled.
disabled bool - If true, the component is disabled. The prop defaults to the value (false) inherited from the parent FormControl component.
disableInjectingGlobalStyles bool false If true, GlobalStyles for the auto-fill keyframes will not be injected/removed on mount/unmount. Make sure to inject them at the top of your application. This option is intended to help with boosting the initial rendering performance if you are loading a big amount of Input components at once.
endAdornment node - End InputAdornment for this component.
error bool - If true, the input will indicate an error. The prop defaults to the value (false) inherited from the parent FormControl component.
fullWidth bool false If true, the input will take up the full width of its container.
id string - The id of the input element.
inputComponent element type 'input' The component used for the input element. Either a string to use a HTML element or a component. This needs to be able to hold a ref.
inputProps object {} Attributes applied to the input element.
inputRef ref - Pass a ref to the input element.
margin 'dense'| 'none' - If dense, will adjust vertical spacing. This is normally obtained via context from FormControl. The prop defaults to the value ('none') inherited from the parent FormControl component.
maxRows number| string - Maximum number of rows to display when multiline option is set to true.
minRows number| string - Minimum number of rows to display when multiline option is set to true.
multiline bool false If true, a TextareaAutosize element is rendered.
name string - Name attribute of the input element.
onBlur func - Callback fired when the input is blurred. Notice that the first argument (event) might be undefined.
onChange func - Callback fired when the value is changed. Signature: function(event: React.ChangeEvent) => void. event The event source of the callback. You can pull out the new value by accessing event.target.value (string).
onInvalid func - Callback fired when the input doesn't satisfy its constraints.
placeholder string - The short hint displayed in the input before the user enters a value.
readOnly bool - It prevents the user from changing the value of the field (not from interacting with the field).
required bool - If true, the input element is required. The prop defaults to the value (false) inherited from the parent FormControl component.
rows number| string - Number of rows to display when multiline option is set to true.
size 'medium'| 'small'| string - The size of the component.
slotProps { input?: object, root?: object } {} The extra props for the slot components. You can override the existing props or add new ones.
slots { input?: elementType, root?: elementType } {} The components used for each slot inside.
startAdornment node - Start InputAdornment for this component.
sx Array<func| object| bool>| func| object - The system prop that allows defining system overrides as well as additional CSS styles. See the sx page for more details.
type string 'text' Type of the input element. It should be a valid HTML5 input type.
value any - The value of the input element, required for a controlled component.

ref는 루트 요소로 전달돼요.

테마 기본 props (Theme default props)

MuiInputBase를 사용하면 테마를 통해 이 컴포넌트의 기본 props를 바꿀 수 있어요.

CSS 클래스 (CSS classes)

이 클래스들은 CSS로 스타일을 지정할 때 유용해요. 특정 상태가 트리거되면 컴포넌트의 슬롯에 적용돼요.

Class name Rule name Description
.Mui-disabled Styles applied to the root element if disabled={true}.
.Mui-error State class applied to the root element if error={true}.
.Mui-focused Styles applied to the root element if the component is focused.
.Mui-readOnly State class applied to the root element if readOnly={true}.
.MuiInputBase-adornedEnd adornedEnd Styles applied to the root element if endAdornment is provided.
.MuiInputBase-adornedStart adornedStart Styles applied to the root element if startAdornment is provided.
.MuiInputBase-colorSecondary colorSecondary Styles applied to the root element if the color is secondary.
.MuiInputBase-formControl formControl Styles applied to the root element if the component is a descendant of FormControl.
.MuiInputBase-fullWidth fullWidth Styles applied to the root element if fullWidth={true}.
.MuiInputBase-hiddenLabel hiddenLabel Styles applied to the root element if hiddenLabel={true}.
.MuiInputBase-input input Styles applied to the input element.
.MuiInputBase-inputTypeSearch inputTypeSearch
.MuiInputBase-multiline multiline Styles applied to the root element if multiline={true}.
.MuiInputBase-root root Styles applied to the root element.
.MuiInputBase-sizeSmall sizeSmall Styles applied to the input element if size="small".

컴포넌트의 스타일을 다음 방법 중 하나로 재정의할 수 있어요.

  • 전역 클래스 이름으로 재정의하기
  • 커스텀 테마의 컴포넌트 styleOverrides 속성에 규칙 이름(rule name)으로 재정의하기

소스 코드 (Source code)

이 페이지에서 원하는 정보를 찾지 못했다면, 컴포넌트의 구현 코드를 살펴보시면 더 자세한 내용을 확인할 수 있어요.

더 알아보기