노브 컴포넌트
노브 컴포넌트 (Knob Component)
Knob은 다이얼 형태로 숫자 값을 입력할 수 있는 폼 컴포넌트예요. ngModel이나 리액티브 폼과 함께 사용할 수 있고, 색상, 크기, 최솟값·최댓값, 스텝, 스트로크 두께 같은 속성으로 원하는 모습으로 조절할 수 있어요. 이 문서에서는 기본 사용법부터 템플릿 활용, 접근성, Props와 Emits, 패스 스루 옵션, 테마까지 Knob을 사용하는 데 필요한 내용을 전부 다뤄요
출처: 문서
본문
접근성
스크린 리더에서는 Knob 엘리먼트 컴포넌트가 aria-valuemin, aria-valuemax, aria-valuenow 속성 외에 slider 역할을 사용해요. 컴포넌트를 설명하는 값은 ariaLabelledBy와 ariaLabel 프로퍼티로 정의할 수 있어요.
기본
Knob은 입력 컴포넌트로, 표준 ngModel 디렉티브와 함께 사용해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobBasicDemo {
value!: number;
}
색상
색상은 textColor, rangeColor, valueColor 프로퍼티로 커스터마이징할 수 있어요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" valueColor="SlateGray" rangeColor="MediumTurquoise" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobColorDemo {
value: number = 50;
}
비활성화
disabled가 설정되어 있으면 Knob과 상호작용할 수 없다는 시각적 힌트가 적용돼요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" [disabled]="true" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobDisabledDemo {
value: number = 75;
}
최소/최대
경계값은 min과 max 프로퍼티로 설정하며, 기본값은 각각 0과 100이에요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" [min]="-50" [max]="50" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobMinmaxDemo {
value: number = 10;
}
리액티브
Knob은 커스텀 컨트롤로도 제어할 수 있어요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ButtonModule } from 'primeng/button';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex flex-col items-center gap-2">
<p-knob [(ngModel)]="value" size="150" readonly="true" />
<div class="flex gap-2">
<p-button icon="pi pi-plus" (click)="value = value + 1" [disabled]="value >= 100" />
<p-button icon="pi pi-minus" (click)="value = value - 1" [disabled]="value <= 0" />
</div>
</div>
`,
standalone: true,
imports: [ButtonModule, KnobModule, FormsModule]
})
export class KnobReactiveDemo {
value: number = 0;
}
리액티브 폼
Knob은 리액티브 폼과도 함께 사용할 수 있어요. 이 경우 formControlName 프로퍼티를 사용해 컴포넌트를 폼 컨트롤에 바인딩해요.
import { Component, inject } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
import { MessageModule } from 'primeng/message';
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 items-center gap-4">
<div class="flex flex-col items-center gap-1">
<p-knob formControlName="value" [invalid]="isInvalid('value')" />
@if (isInvalid('value')) {
<p-message severity="error" size="small" variant="simple">{{ getErrorMessage('value') }}</p-message>
}
</div>
<button pButton severity="secondary" type="submit"><span pButtonLabel>Submit</span></button>
</form>
</div>
`,
standalone: true,
imports: [KnobModule, MessageModule, ToastModule, ButtonModule, ReactiveFormsModule]
})
export class KnobReactiveformsDemo {
messageService = inject(MessageService);
items: any[] | undefined;
exampleForm: FormGroup | undefined;
formSubmitted: boolean = false;
constructor() {
this.exampleForm = this.fb.group({
value: [15, [Validators.min(25), Validators.max(75)]]
});
}
onSubmit() {
this.formSubmitted = true;
if (this.exampleForm.valid) {
this.messageService.add({ severity: 'success', summary: 'Success', detail: 'Form is submitted', life: 3000 });
this.exampleForm.reset({
value: 15
});
this.formSubmitted = false;
}
}
getControl(controlName: string): AbstractControl | null {
return this.exampleForm?.get(controlName) ?? null;
}
getErrorMessage(controlName: string): string | null {
const control = this.getControl(controlName);
if (!control || !control.errors) return null;
if (control.errors['min']) {
return 'Value must be greater than 15.';
}
if (control.errors['max']) {
return 'Must be less than 75.';
}
}
isInvalid(controlName: string) {
const control = this.getControl(controlName);
return control?.invalid && (control.dirty || this.formSubmitted);
}
}
읽기 전용
readonly가 설정되어 있으면 값을 편집할 수 없어요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" [readonly]="true" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobReadonlyDemo {
value: number = 50;
}
크기
Knob의 지름은 size 프로퍼티를 사용해 픽셀 단위로 정의해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" [size]="200" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobSizeDemo {
value: number = 60;
}
스텝
한 번에 움직이는 크기는 step 프로퍼티로 정의해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" [step]="10" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobStepDemo {
value!: number;
}
스트로크
테두리 크기는 strokeWidth 프로퍼티로 픽셀 단위 숫자로 지정해요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" [strokeWidth]="5" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobStrokeDemo {
value: number = 40;
}
템플릿
레이블은 문자열 템플릿으로, valueTemplate 프로퍼티로 커스터마이징할 수 있으며 60이 placeholder로 사용돼요.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
@Component({
template: `
<div class="card flex justify-center">
<p-knob [(ngModel)]="value" valueTemplate="{value}%" />
</div>
`,
standalone: true,
imports: [KnobModule, FormsModule]
})
export class KnobTemplateDemo {
value: number = 60;
}
templatedrivenforms-doc
import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { KnobModule } from 'primeng/knob';
import { MessageModule } from 'primeng/message';
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 flex-col items-center gap-4">
<div class="flex flex-col items-center gap-1">
<p-knob #model="ngModel" [(ngModel)]="value" [invalid]="isInvalid(model)" name="knob" />
@if (isInvalid(model)) {
<p-message severity="error" size="small" variant="simple">{{ getErrorMessage(model) }}</p-message>
}
</div>
<button pButton severity="secondary" type="submit"><span pButtonLabel>Submit</span></button>
</form>
</div>
`,
standalone: true,
imports: [KnobModule, MessageModule, ToastModule, ButtonModule, FormsModule]
})
export class KnobTemplatedrivenformsDemo {
messageService = inject(MessageService);
value: number = 15;
formSubmitted: boolean = false;
onSubmit(form: NgForm) {
this.formSubmitted = true;
if (!this.isInvalid(form.controls['knob'])) {
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: 'Form is submitted',
life: 3000
});
form.resetForm({ knob: 15 });
this.formSubmitted = false;
}
}
getErrorMessage(control: any): string | null {
const value = control?.value;
return value < 25 ? 'Value must be greater than 25.' : value > 75 ? 'Must be less than 75.' : null;
}
isInvalid(control: any): boolean {
if (!control) return false;
const value = control.value;
const hasError = value < 25 || value > 75;
return hasError && (this.formSubmitted || control.dirty);
}
}
Knob
Knob은 다이얼을 사용해 숫자 입력을 정의하는 폼 컴포넌트예요.
프로퍼티
| 이름 | 타입 | 기본값 | 설명 |
|---|---|---|---|
| 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 | 설정되어 있으면 입력의 이름을 지정해요. |
| styleClass | string | - | 컴포넌트의 스타일 클래스예요. (지원 중단됨) |
| ariaLabel | string | - | 접근성을 위해 입력에 레이블을 붙이는 문자열을 정의해요. |
| ariaLabelledBy | string | - | 입력 필드에 레이블을 지정하는 DOM 내 하나 이상의 ID를 지정해요. |
| tabindex | number | 0 | 탭 순서에서 엘리먼트의 인덱스예요. |
| valueColor | string | ... | 값의 배경이에요. |
| rangeColor | string | ... | 범위의 배경색이에요. |
| textColor | string | ... | 값 텍스트의 색상이에요. |
| valueTemplate | string | {value} | 값의 템플릿 문자열이에요. |
| size | number | 100 | 픽셀 단위의 컴포넌트 크기예요. |
| min | number | 0 | 최소 경계값이에요. |
| max | number | 100 | 최대 경계값이에요. |
| step | number | 1 | 값을 증가/감소시키는 스텝 단위예요. |
| strokeWidth | number | 14 | Knob 스트로크의 너비예요. |
| showValue | boolean | true | Knob 내부에 값을 표시할지 여부예요. |
| readonly | boolean | false | 설정되어 있으면 컴포넌트 값을 편집할 수 없어요. |
Emits
| Name | Parameters | Description |
|---|---|---|
| onChange | value: number | 값이 변경될 때 호출되는 콜백이에요. |
Pass Through Options
| Name | Type | Description |
|---|---|---|
| host | PassThroughOption<HTMLElement, I> | 호스트의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| svg | PassThroughOption<SVGElement, I> | SVG의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| range | PassThroughOption<SVGPathElement, I> | range의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| value | PassThroughOption<SVGPathElement, I> | value의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| text | PassThroughOption<SVGTextElement, I> | text의 DOM 요소에 속성을 전달하는 데 사용돼요. |
Theming
CSS Classes
| Class | Description |
|---|---|
| p-knob | 루트 요소의 클래스 이름 |
| p-knob-range | range 요소의 클래스 이름 |
| p-knob-value | value 요소의 클래스 이름 |
| p-knob-text | text 요소의 클래스 이름 |
Design Tokens
| Token | CSS Variable | Description |
|---|---|---|
| knob.transition.duration | --p-knob-transition-duration | 루트의 전환 지속 시간 |
| knob.focus.ring.width | --p-knob-focus-ring-width | 루트의 포커스 링 너비 |
| knob.focus.ring.style | --p-knob-focus-ring-style | 루트의 포커스 링 스타일 |
| knob.focus.ring.color | --p-knob-focus-ring-color | 루트의 포커스 링 색상 |
| knob.focus.ring.offset | --p-knob-focus-ring-offset | 루트의 포커스 링 오프셋 |
| knob.focus.ring.shadow | --p-knob-focus-ring-shadow | 루트의 포커스 링 그림자 |
| knob.value.background | --p-knob-value-background | value의 배경 |
| knob.range.background | --p-knob-range-background | range의 배경 |
| knob.text.color | --p-knob-text-color | text의 색상 |
더 알아보기 (Learn more)
이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.