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

useMeterGroup

원문 보기 위키 갱신

정의된 범위 안에서 미터 값의 합산과 백분율 계산을 관리하는 훅이에요. 미터 그룹은 여러 개의 진행 표시(미터)를 묶어 하나의 전체량을 보여주는 컨트롤이에요. 각 미터 값을 전체 대비 백분율로 환산하고, 그 합계를 계속 추적해야 하죠. useMeterGroup이 그 백분율 계산과 누적 합계를 도와줘요.

출처: 문서

본문

사용법 (Usage)

import { useMeterGroup } from '@primereact/headless/metergroup';
const { rootProps, percent, percentAsString } = useMeterGroup();

return <div {...rootProps}></div>;

useMeterGroup은 누적 미터 값을 추적하고 min/max 경계 안에서 백분율로 변환해요. 컴포넌트 기반 API는 Primitive 문서를 확인해 주세요.

기능 (Features)

  • ARIA 미터 시맨틱: rootProps가 role="meter"와 함께 aria-valuemin, aria-valuemax, data-orientation을 담아요
  • 백분율 헬퍼: percent(value)와 percentAsString(value)가 어떤 원시 값이든 구성된 범위에 맞게 정규화해요
  • 집계 합계: updateTotalPercent와 resetTotalPercent가 미터들을 가로질러 누적 state.totalPercent를 유지해요
  • 커스텀 경계: 구성 가능한 min과 max가 모든 미터를 새 범위에 맞춰 다시 계산해요
  • 방향 지원: data-orientation이 horizontal과 vertical 사이 레이아웃을 전환해요

콜백 다루기 (Working with callbacks)

값을 백분율로 변환하기 (Converting values to percentages)

percent와 percentAsString이 구성된 범위에 대한 계산을 처리해요. 요소 너비를 설정하거나 라벨을 렌더링할 때 사용해요.

const { percent, percentAsString } = useMeterGroup({ max: 200 });

percent(50); // 25
percentAsString(50); // '25%'

미터들을 가로질러 합산하기 (Aggregating across meters)

각 미터에서 updateTotalPercent를 호출해 그룹 전체의 누적 합계를 만들고, 새 작업 전에 resetTotalPercent를 호출해요.

const { state, updateTotalPercent, resetTotalPercent } = useMeterGroup();

resetTotalPercent();
updateTotalPercent(15);
updateTotalPercent(25);

state.totalPercent; // 40

방향 전환하기 (Switching orientation)

루트가 data-orientation을 노출하므로 CSS에 추가 상태 없이 하나의 선택자로 레이아웃을 뒤집을 수 있어요.

const { rootProps } = useMeterGroup({ orientation: 'vertical' });

데이터 속성으로 스타일링 (Styling with data attributes)

prop 객체는 스타일링을 위한 data-scope="metergroup"과 data-part, 레이아웃 전환을 위한 루트의 data-orientation을 포함해요.

[data-scope='metergroup'][data-part='root'] {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

[data-scope='metergroup'][data-orientation='vertical'] {
    flex-direction: row;
}

API

useMeterGroup

접근성 (Accessibility)

각 세그먼트는 aria-valuenow를 노출해서 보조 기술이 개별 값을 읽을 수 있어요. 전체 WAI-ARIA 준수 세부 사항은 Primitive 문서를 참고하세요.

더 알아보기 (Learn more)