@gravity-ui/normalized-list

中文文档
1.0.0发布于 2026/11/05MITTypeScriptESM + CJS近 30 天下载 6197

Normalized flat/tree list model and React list/select components

原始使用文档
文档目录12 节
  1. Install
  2. Idea
  3. Two input shapes
  4. Package layout
  5. Quick start
  6. 1. Hook only
  7. 2. List UI on top of the hook
  8. 3. UIKit preset
  9. Customizable views
  10. CSS namespace
  11. Storybook
  12. License

@gravity-ui/normalized-list

Core of the package is useNormalizedList: it takes list items in either a flat or tree shape and turns them into one normalized model — id-keyed data, hierarchy meta, visible order, and interactive state (selection, expansion, focus).

UI components (NormalizedList, NormalizedSelect, and the optional UIKit layer) sit on top of that model and stay customizable via render props.

Install

npm install @gravity-ui/normalized-list

Peer dependencies for the core entry (@gravity-ui/normalized-list): react and react-dom with the range ^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0.

The @gravity-ui/normalized-list/uikit entry requires UIKit 8 (@gravity-ui/uikit@^8.0.0) and React 18 or 19 (both react and react-dom). UIKit 7 is no longer supported by this entry.

npm install @gravity-ui/normalized-list @gravity-ui/uikit@^8.0.0

The UIKit entry also needs @gravity-ui/icons and @floating-ui/react. Installing @gravity-ui/uikit is usually enough (it already depends on the other two). With strict package managers such as pnpm, install @gravity-ui/icons and @floating-ui/react explicitly if they do not resolve.

Migrating from @gravity-ui/uikit/unstable (TreeSelect, TreeList, useList)? See MIGRATION.md.

Idea

items (flat | tree)
        │
        ▼
  useNormalizedList
        │
        ├─ structure  → itemsById, groupsState, visibleFlattenIds, …
        └─ state      → selectedById, expandedById, activeItemId, …
                │
                ▼
     NormalizedList / NormalizedSelect
     (or your own view / UIKitNormalized*)

What the hook solves:

  • one API for flat and tree authoring formats;
  • O(1) access by id after normalize;
  • selection / expand / keyboard-friendly visible order (visibleFlattenIds);
  • UI stays dumb: pass list in and customize rendering.

Two input shapes

Payload T is yours. The list contract is only the node wrapper:

Flattened — item is T itself (optional id / selected / disabled):

import type {ListItemType} from '@gravity-ui/normalized-list';

const flat: ListItemType<string>[] = ['one', 'two', 'three'];

const flatObjects: ListItemType<{title: string}>[] = [
  {title: 'one'},
  {title: 'two', selected: true},
];

Tree — item has data: T and optional children:

const tree: ListItemType<{title: string}>[] = [
  {
    data: {title: 'Fruits'},
    children: [{data: {title: 'Apple'}}, {data: {title: 'Orange'}}],
  },
];

Both normalize into the same structure + state.

Package layout

EntryImportWhat you get
Core@gravity-ui/normalized-listuseNormalizedList, NormalizedList, NormalizedSelect, headless building blocks
UIKit@gravity-ui/normalized-list/uikitUIKitNormalizedList, UIKitNormalizedSelect, themed item/control views

Core ships a minimal fallback UI when you omit render props. For production, pass your own renderers or use the UIKit entry.

Quick start

1. Hook only

import {useNormalizedList} from '@gravity-ui/normalized-list';

const list = useNormalizedList({
  items: ['one', 'two', 'three'],
  getItemId: (item) => item,
});

// list.structure.itemsById
// list.structure.visibleFlattenIds
// list.state.selectedById / setSelected / …

2. List UI on top of the hook

Core NormalizedList / NormalizedSelect are supplied with fallback views for demos and exploration. For production, prefer your own UI that matches your design system — see Customizable views.

import {NormalizedList, useNormalizedList} from '@gravity-ui/normalized-list';

const items = [{data: {title: 'Parent'}, children: [{data: {title: 'Child'}}]}];

export function Example() {
  const list = useNormalizedList({items, defaultExpandedState: 'expanded'});

  return <NormalizedList list={list} multiple mapItemDataToContentProps={(item) => item} />;
}

3. UIKit preset

The uikit entry is a set of presets for @gravity-ui/uikit: themed defaults for list rows, select control, and popup. They address the “bring your own view” gap from §2 for that one case — you still can override via render props.

import {useNormalizedList} from '@gravity-ui/normalized-list';
import {UIKitNormalizedList} from '@gravity-ui/normalized-list/uikit';

export function Example() {
  const list = useNormalizedList({
    items: ['one', 'two', 'three'],
    getItemId: (item) => item,
  });

  return (
    <UIKitNormalizedList list={list} multiple mapItemDataToContentProps={(title) => ({title})} />
  );
}

NormalizedSelect / UIKitNormalizedSelect use the same model inside (items → normalize → list in a popup).

Customizable views

Override presentation without rewriting list logic:

  • NormalizedList / NormalizedSelect: renderItem, renderContainer
  • NormalizedSelect: also renderControl, renderPopup, renderError, slots slotBeforeListBody / slotAfterListBody
  • UIKit: swap defaults (UIKitListItemView in renderItem, UIKitNormalizedSelectControl, …) the same way
<NormalizedList
  list={list}
  mapItemDataToContentProps={(item) => item}
  renderItem={({id, props, context, renderContainerProps}) => (
    <MyRow {...props} {...renderContainerProps} isLast={context.isLastItem} />
  )}
  renderContainer={(containerProps) => <VirtualizedContainer {...containerProps} />}
/>

CSS namespace

BEM roots and package CSS variables use the g-nl- prefix (for example --g-nl-list-item-background-hover).

The UIKit entry maps those variables to @gravity-ui/uikit tokens (--g-color-*, --g-spacing-*, …).

Storybook

Examples live under normalized-list (core) and normalized-list/UIKit. Docs are *Docs.md next to each Storybook entry.

License

MIT

版本与发布记录

@gravity-ui/normalized-list 共发布 3 个版本,首次发布于 2026/08/21;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.0.0latest2026/11/0511 天前
0.1.12026/10/2225 天前
0.1.02026/08/212 个月前

关于 @gravity-ui/normalized-list 的常见问题

@gravity-ui/normalized-list 是什么?

Normalized flat/tree list model and React list/select components

如何安装 @gravity-ui/normalized-list?

在项目目录运行 npm install @gravity-ui/normalized-list,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0)、react-dom(^16.14.0 || ^17.0.0 || ^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gravity-ui/normalized-list 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gravity-ui/normalized-list 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gravity-ui/normalized-list 可以商用吗?

@gravity-ui/normalized-list 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gravity-ui/normalized-list 的下载量有多少?

根据 npm 官方下载统计,@gravity-ui/normalized-list 在 2026/10/17 – 2026/11/15 这 30 天内被下载 6,197 次,日均约 207 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gravity-ui/normalized-list 还在维护吗?

最新版本 1.0.0 发布于 2026/11/05(11 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/08/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。