셀렉트버튼
셀렉트버튼 (SelectButton)
셀렉트버튼은 버튼 형태의 목록에서 항목을 하나만 또는 여러 개 선택할 수 있게 해주는 컴포넌트예요. 기본 사용법을 시작으로 disabled, fluid, invalid 같은 상태 제어와 크기 조절, 단일·다중 선택 방법을 다뤄요. 반응형 폼과 템플릿 기반 폼 연동, 템플릿 커스터마이징, 스크린 리더를 위한 접근성 지원도 함께 설명해요. 마지막에는 props, emits, 패스 스루 옵션과 CSS 클래스·디자인 토큰을 활용한 테마 설정까지 확인할 수 있어요
출처: 문서
본문
접근성
스크린 리더: 버튼들을 감싸는 컨테이너 요소에는 group role이 적용되고, 각 버튼 요소는 button role을 사용하며, aria-pressed는 선택 상태에 따라 업데이트돼요. 옵션을 설명하는 값은 옵션의 레이블을 참조하는 ariaLabel 프로퍼티를 통해 자동으로 설정되기 때문에, 옵션 표시가 아이콘만으로 구성된 것 같은 표현적 콘텐츠라도 레이블을 정의해 두는 것이 여전히 권장돼요.
기본
SelectButton은 바인딩할 값과 옵션 컬렉션이 필요해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SelectButtonModule } from 'primeng/selectbutton';
@Component({
template: `
<div class="card flex justify-center">
<p-selectbutton [options]="stateOptions" [(ngModel)]="value" optionLabel="label" optionValue="value" aria-labelledby="basic" />
</div>
`,
standalone: true,
imports: [SelectButtonModule, FormsModule]
})
export class SelectbuttonBasicDemo {
stateOptions: any[];
value: string = 'one-way';
}
비활성화
disabled가 있으면 요소를 편집할 수 없고 포커스도 전혀 받을 수 없어요. optionDisabled 프로퍼티를 사용하면 특정 옵션만 비활성화할 수도 있어요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SelectButtonModule } from 'primeng/selectbutton';
@Component({
template: `
<div class="card flex justify-center flex-wrap gap-4">
<p-selectbutton [options]="stateOptions" [(ngModel)]="value1" optionLabel="label" optionValue="value" [disabled]="true" />
<p-selectbutton [options]="stateOptions2" [(ngModel)]="value2" optionLabel="label" optionValue="value" optionDisabled="constant" />
</div>
`,
standalone: true,
imports: [SelectButtonModule, FormsModule]
})
export class SelectbuttonDisabledDemo {
stateOptions: any[];
stateOptions2: any[];
value1: string = 'off';
value2: string = 'Option 1';
}
Fluid
fluid prop을 true로 설정하면 컴포넌트가 컨테이너의 전체 너비를 차지해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SelectButtonModule } from 'primeng/selectbutton';
@Component({
template: `
<div class="card">
<p-selectbutton [options]="stateOptions" [(ngModel)]="value" optionLabel="label" optionValue="value" fluid />
</div>
`,
standalone: true,
imports: [SelectButtonModule, FormsModule]
})
export class SelectbuttonFluidDemo {
stateOptions: any[];
value: string = 'one-way';
}
유효하지 않음
invalid 프로퍼티를 사용하면 유효성 검사 실패를 나타내는 invalid 상태가 적용되고, 이 상태는 Angular Forms와 통합할 수 있어요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SelectButtonModule } from 'primeng/selectbutton';
@Component({
template: `
<div class="card flex justify-center">
<p-selectbutton [options]="stateOptions" [(ngModel)]="value" optionLabel="label" optionValue="value" [invalid]="value === undefined" />
</div>
`,
standalone: true,
imports: [SelectButtonModule, FormsModule]
})
export class SelectbuttonInvalidDemo {
stateOptions: any[];
value: string | undefined;
}
다중 선택
SelectButton은 기본적으로 하나의 항목만 선택할 수 있고, multiple 옵션을 설정하면 두 개 이상의 항목을 선택할 수 있어요. multiple을 사용하는 경우 model 프로퍼티는 배열이어야 해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SelectButtonModule } from 'primeng/selectbutton';
@Component({
template: `
<div class="card flex justify-center">
<p-selectbutton [options]="paymentOptions" [(ngModel)]="value" [multiple]="true" optionLabel="name" optionValue="value" />
</div>
`,
standalone: true,
imports: [SelectButtonModule, FormsModule]
})
export class SelectbuttonMultipleDemo {
paymentOptions: any[];
value!: number;
}
reactiveforms-doc
SelectButton은 reactive forms와도 함께 사용할 수 있어요. 이 경우 formControlName 프로퍼티를 사용해 컴포넌트를 폼 컨트롤에 바인딩해요.
import { Component, inject } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { MessageModule } from 'primeng/message';
import { SelectButtonModule } from 'primeng/selectbutton';
import { ToastModule } from 'primeng/toast';
import { ButtonModule } from 'primeng/button';
import { MessageService } from 'primeng/api';
@Component({
template: `
<p-toast />
<div class="card flex justify-center">
<form [formGroup]="exampleForm" (ngSubmit)="onSubmit()" class="flex flex-col gap-4">
<div class="flex flex-col gap-1">
<p-selectbutton [options]="stateOptions" formControlName="value" [invalid]="isInvalid('value')" optionLabel="label" optionValue="value" />
@if (isInvalid('value')) {
<p-message severity="error" size="small" variant="simple">Selection is required</p-message>
}
</div>
<button pButton type="submit"><span pButtonLabel>Submit</span></button>
</form>
</div>
`,
standalone: true,
imports: [MessageModule, SelectButtonModule, ToastModule, ButtonModule, ReactiveFormsModule]
})
export class SelectbuttonReactiveformsDemo {
messageService = inject(MessageService);
exampleForm: FormGroup | undefined;
formSubmitted: boolean = false;
stateOptions: any[];
constructor() {
this.exampleForm = this.fb.group({
value: ['', Validators.required]
});
}
onSubmit() {
this.formSubmitted = true;
if (this.exampleForm.valid) {
this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form is submitted', life: 3000 });
this.exampleForm.reset();
this.formSubmitted = false;
}
}
isInvalid(controlName: string) {
const control = this.exampleForm.get(controlName);
return control?.invalid && (control.touched || this.formSubmitted);
}
}
크기
SelectButton은 기본 크기 외에 small과 large 크기도 제공해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SelectButtonModule } from 'primeng/selectbutton';
@Component({
template: `
<div class="card flex flex-col items-center gap-4">
<p-selectbutton [(ngModel)]="value1" [options]="options" size="small" />
<p-selectbutton [(ngModel)]="value2" [options]="options" />
<p-selectbutton [(ngModel)]="value3" [options]="options" size="large" />
</div>
`,
standalone: true,
imports: [SelectButtonModule, FormsModule]
})
export class SelectbuttonSizesDemo {
value1!: string;
value2: string = 'Beginner';
value3: string = 'Expert';
options: any[];
}
Template
커스텀 콘텐츠를 지원하려면 item이라는 이름의 템플릿을 정의하세요. 이때 기본 로컬 템플릿 변수는 옵션을 참조해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { SelectButtonModule } from 'primeng/selectbutton';
@Component({
template: `
<div class="card flex justify-center">
<p-selectbutton [options]="justifyOptions" [(ngModel)]="value" optionLabel="justify">
<ng-template #item let-item>
<i [class]="item.icon"></i>
</ng-template>
</p-selectbutton>
</div>
`,
standalone: true,
imports: [SelectButtonModule, FormsModule]
})
export class SelectbuttonTemplateDemo {
value: any;
justifyOptions: any[];
}
templatedrivenforms-doc
import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MessageModule } from 'primeng/message';
import { SelectButtonModule } from 'primeng/selectbutton';
import { ToastModule } from 'primeng/toast';
import { ButtonModule } from 'primeng/button';
import { MessageService } from 'primeng/api';
@Component({
template: `
<p-toast />
<div class="card flex justify-center">
<form #exampleForm="ngForm" (ngSubmit)="onSubmit(exampleForm)" class="flex justify-center flex-col gap-4">
<div class="flex flex-col gap-1">
<p-selectbutton #model="ngModel" [(ngModel)]="value" [options]="stateOptions" optionLabel="label" optionValue="value" [invalid]="model.invalid && (model.touched || exampleForm.submitted)" required name="value" />
@if (model.invalid && (model.touched || exampleForm.submitted)) {
<p-message severity="error" size="small" variant="simple">Selection is required.</p-message>
}
</div>
<button pButton type="submit"><span pButtonLabel>Submit</span></button>
</form>
</div>
`,
standalone: true,
imports: [MessageModule, SelectButtonModule, ToastModule, ButtonModule, FormsModule]
})
export class SelectbuttonTemplatedrivenformsDemo {
messageService = inject(MessageService);
value: any;
stateOptions: any[];
onSubmit(form: any) {
if (form.valid) {
this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form Submitted', life: 3000 });
form.resetForm();
}
}
}
Select Button
SelectButton은 버튼을 사용해 목록에서 하나 또는 여러 개의 항목을 선택할 때 사용해요.
Props
| Name | Type | Default | Description |
|---|---|---|---|
| dt | InputSignal | undefined | 컴포넌트의 스코프 디자인 토큰을 정의해요. |
| unstyled | InputSignal | undefined | 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요. |
| pt | InputSignal | undefined | 컴포넌트 내부의 DOM 요소에 속성을 전달할 때 사용해요. |
| ptOptions | InputSignal | undefined | 컴포넌트의 passthrough(pt) 옵션을 구성할 때 사용해요. |
| required | InputSignalWithTransform<boolean, unknown> | false | 값이 반드시 있어야 해요(설정한 경우). |
| invalid | InputSignalWithTransform<boolean, unknown> | false | 설정하면 컴포넌트에 invalid 상태 스타일이 적용돼요. |
| disabled | InputSignalWithTransform<boolean, unknown> | false | 설정하면 컴포넌트에 disabled 상태 스타일이 적용돼요. |
| name | InputSignal | undefined | 설정하면 입력의 name을 지정해요. |
| options | any[] | - | 선택 가능한 옵션으로 표시할 selectitem 배열이에요. |
| optionLabel | string | - | 옵션의 라벨 필드 이름이에요. |
| optionValue | string | - | 옵션의 값 필드 이름이에요. |
| optionDisabled | string | - | 옵션의 disabled 필드 이름이에요. |
| unselectable | boolean | - | 선택을 해제할 수 있는지 여부예요. |
| tabindex | number | 0 | 탭 순서에서 이 요소의 인덱스예요. |
| multiple | boolean | false | 지정하면 여러 값을 선택할 수 있어요. |
| allowEmpty | boolean | true | 선택을 해제할 수 없는지 여부예요. |
| styleClass | string | - | 컴포넌트의 스타일 클래스예요. |
| ariaLabelledBy | string | - | 컴포넌트와 라벨 간의 관계를 설정해요. 값은 하나 이상의 요소 ID여야 해요. |
| dataKey | string | - | options에서 값을 고유하게 식별하는 속성이에요. |
| autofocus | boolean | false | 설정하면 로드 시 컴포넌트가 자동으로 포커스를 받아요. |
| size | InputSignal<"small" | "large"> | undefined | 컴포넌트의 크기를 지정해요. |
| fluid | InputSignalWithTransform<boolean, unknown> | undefined | 활성화하면 컨테이너 너비의 100%를 차지해요. |
Emits
| Name | Parameters | Description |
|---|---|---|
| onOptionClick | event: SelectButtonOptionClickEvent | 입력을 클릭할 때 호출되는 콜백이에요. |
| onChange | event: SelectButtonChangeEvent | 선택이 변경될 때 호출되는 콜백이에요. |
Templates
| Name | Type | Description |
|---|---|---|
| item | TemplateRef | 커스텀 항목 템플릿이에요. |
Pass Through Options
| Name | Type | Description |
|---|---|---|
| host | PassThroughOption<HTMLElement, I> | 호스트의 DOM 요소에 속성을 전달할 때 사용해요. |
| root | PassThroughOption<HTMLDivElement, I> | 루트의 DOM 요소에 속성을 전달할 때 사용해요. |
| pcToggleButton | ToggleButtonPassThrough | ToggleButton 컴포넌트에 속성을 전달할 때 사용해요. |
Theming
CSS 클래스
| 클래스 | 설명 |
|---|---|
| p-selectbutton | 루트 엘리먼트의 클래스 이름이에요 |
디자인 토큰
| 토큰 | CSS 변수 | 설명 |
|---|---|---|
| selectbutton.border.radius | --p-selectbutton-border-radius | 루트의 테두리 반경이에요 |
| selectbutton.invalid.border.color | --p-selectbutton-invalid-border-color | 루트의 유효하지 않은 상태 테두리 색상이에요 |
더 알아보기 (Learn more)
이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.