@dpzj/select-user-core
v0.2.1
Published
Framework-agnostic person picker core for @dpzj/select-user
Maintainers
Readme
@dpzj/select-user-core
@dpzj/select-user 家族的框架无关核心逻辑:人员字段归一化、列表解包、树裁剪、选择态计算。
不依赖 Vue / Element / Ant Design。
安装
npm install @dpzj/select-user-coreUI 包(@dpzj/select-user / @dpzj/select-user-vue3-element / @dpzj/select-user-vue3-antd)内部使用本包,业务侧一般不需要直接安装。
常用导出
import {
normalizePerson,
normalizePersonList,
getPersonKey,
unwrapList,
filterTreeByChildId,
filterPersonsByKeyword,
filterPersonsByRole,
mapGroupNodes,
mapGroupUsers,
toggleSelectedUsers,
mergeSelectAll,
isPersonSelected,
resolveOpenOptions
} from '@dpzj/select-user-core'API
人员归一化
normalizePerson(user)
将异构人员字段统一为稳定形状,额外字段原样保留。
| 输出字段 | 来源 |
|---|---|
| id / userId | 互相兜底 |
| username / userCode | 互相兜底 |
| nickname / userName | 互相兜底 |
normalizePerson({ userId: 1, userCode: 'zhangsan', userName: '张三' })
// => { userId: 1, id: 1, userCode: 'zhangsan', username: 'zhangsan', userName: '张三', nickname: '张三' }normalizePersonList(list)
批量归一化并按 getPersonKey 去重。
getPersonKey(person)
稳定主键,优先级:username → userCode → id → userId → nickname → userName。
列表与树
unwrapList(payload)
兼容多种接口返回形态,始终返回数组:
[]{ data: [] }{ data: { list: [] } }{ list: [] }
其它情况返回 []。
filterTreeByChildId(nodes, targetId)
按子节点 id 裁剪组织树,保留从根到目标节点的路径;命中目标节点时保留其完整 children。
filterPersonsByKeyword(list, keyword)
按 nickname 模糊过滤。
filterPersonsByRole(list, userRole)
按 postIds 包含关系过滤;userRole 为空时不过滤。
群组
mapGroupNodes(groups)
将群组列表包装为树根节点:
// 输出
[{
name: '个人群组',
id: '0000000',
children: [{ ...item, name: item.groupName || item.name, children: [] }]
}]mapGroupUsers(group)
读取 group.groupUserList 或 group.users,并归一化人员字段。
选择态
toggleSelectedUsers(selected, person, options?)
切换选中态。options:
| 字段 | 默认 | 说明 |
|---|---|---|
| isRadio | false | 单选时直接替换为当前人 |
| getKey | p => p.username \|\| p.id \|\| '' | 主键函数 |
mergeSelectAll(selected, currentList, checked, getKey?)
当前列表全选 / 取消全选,不影响其它已选。
isPersonSelected(selected, person, getKey?)
判断是否已选。
打开参数
resolveOpenOptions(options)
将 open(options) 参数转换为兼容旧 API 的形态:
resolveOpenOptions({ multiple: false, deptId: '1', selected: [] })
// => { type: 'once', deptId: '1', userRole: '', selected: [], sameDeptOnly: false }| 输入 | 输出 |
|---|---|
| multiple(默认 true) | type: 多选 '' / 单选 'once' |
| deptId | deptId |
| userRole | userRole |
| selected | selected |
| sameDeptOnly | sameDeptOnly |
入口
| 字段 | 值 |
|---|---|
| main | dist/index.cjs.js |
| module | dist/index.esm.js |
| exports | CJS + ESM |
开发
在 monorepo 根目录:
npm install
npm run build:core相关包
| 包 | 说明 |
|---|---|
| @dpzj/select-user | Vue2 + Element UI |
| @dpzj/select-user-vue3-element | Vue3 + Element Plus |
| @dpzj/select-user-vue3-antd | Vue3 + ant-design-vue |
License
MIT
