@xfe-repo/web-components 1.8.0 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,222 +0,0 @@
1
- # EffectLabelSelect
2
-
3
- > 标签维度双选择器,左侧选择搜索维度(如商户号/商户名),右侧根据选中维度远程搜索并选择具体值。
4
-
5
- ## 导入
6
-
7
- ```tsx
8
- import { EffectLabelSelect } from '@xfe-repo/web-components'
9
- import type { SelectOptionItem, LabelDic } from '@xfe-repo/web-components'
10
- // 工具函数
11
- import { getStrValue, getSelectValue, getInitialValue, getFirstValue, getSecondValue } from '@xfe-repo/web-components'
12
- ```
13
-
14
- ## 基本用法
15
-
16
- ### 标准三件套模式
17
-
18
- 业务中最常见的使用方式 — 同时使用 `customLabelInValue` + `optionsDisplayType` + `labelDic`:
19
-
20
- ```tsx
21
- <Form.Item name="merchantStr" noStyle>
22
- <EffectLabelSelect customLabelInValue optionsDisplayType="labelAndValue" labelDic={{ phone: '手机号', name: '商户名称' }} />
23
- </Form.Item>
24
- ```
25
-
26
- > 💡 三件套的含义:
27
- >
28
- > - `customLabelInValue`:value 格式为分号分隔字符串(`displayType;value;label;labelDicKey`)
29
- > - `optionsDisplayType`:下拉选项展示格式(`labelAndValue` 显示为 `value - label`)
30
- > - `labelDic`:自定义 label 字典,根据 `labelDicKey` 动态切换显示文案
31
-
32
- ### 独立受控模式
33
-
34
- 使用默认的商户号/商户名维度搜索商户列表:
35
-
36
- ```tsx
37
- import { EffectLabelSelect } from '@xfe-repo/web-components'
38
-
39
- function MyForm() {
40
- const [value, setValue] = useState<string>()
41
-
42
- return (
43
- <EffectLabelSelect
44
- value={value}
45
- onChange={(val, option) => {
46
- setValue(val)
47
- console.log('选中项:', option) // { label, value, customFields }
48
- }}
49
- />
50
- )
51
- }
52
- ```
53
-
54
- ## 进阶用法
55
-
56
- ### 自定义搜索维度
57
-
58
- 通过 `labelDic` 自定义左侧维度选项:
59
-
60
- ```tsx
61
- const customLabelDic: LabelDic = {
62
- phone: '手机号',
63
- email: '邮箱',
64
- name: '姓名',
65
- }
66
-
67
- <EffectLabelSelect
68
- labelDic={customLabelDic}
69
- defaultLabel="phone"
70
- onFetchData={mySearchApi}
71
- onTransformData={({ phone, email, name, ...rest }) => ({
72
- label: name,
73
- value: phone,
74
- customFields: rest,
75
- })}
76
- onChange={(value) => console.log(value)}
77
- />
78
- ```
79
-
80
- ### customLabelInValue 模式
81
-
82
- 当 `customLabelInValue` 为 `true` 时,value 会编码为 `展示类型;ID;名称;labelDicKey` 格式的字符串:
83
-
84
- ```tsx
85
- <EffectLabelSelect
86
- customLabelInValue
87
- onChange={(value) => {
88
- // value 格式如 "label;123;某某商户;businessNo"
89
- console.log(value)
90
- }}
91
- />
92
- ```
93
-
94
- ### 显示 ID + 名称
95
-
96
- ```tsx
97
- <EffectLabelSelect optionsDisplayType="labelAndValue" />
98
- // 选项显示为 "123456 - 某某商户"
99
- ```
100
-
101
- ### 自定义 labelDic 文案
102
-
103
- 通过前缀区分不同场景的同类搜索:
104
-
105
- ```tsx
106
- // 买家商户搜索
107
- <EffectLabelSelect
108
- customLabelInValue
109
- optionsDisplayType="labelAndValue"
110
- labelDic={{ phone: '买家手机号', name: '买家商户名' }}
111
- />
112
-
113
- // 卖家商户搜索
114
- <EffectLabelSelect
115
- customLabelInValue
116
- optionsDisplayType="labelAndValue"
117
- labelDic={{ phone: '卖家手机号', name: '卖家商户名' }}
118
- />
119
- ```
120
-
121
- ## Props
122
-
123
- ```typescript
124
- interface EffectLabelSelectProps extends Omit<SelectProps, 'onChange'> {
125
- onChange?: (value?: string, selectedOptionItem?: SelectOptionItem) => void
126
- defaultLabel?: string
127
- labelDic?: LabelDic
128
- value?: string
129
- customLabelInValue?: boolean
130
- onFetchData?: (params: any, config: any) => Promise<any>
131
- onTransformData?: (params: Record<string, string>) => SelectOptionItem
132
- fetchParams?: Object
133
- optionsDisplayType?: 'labelAndValue'
134
- }
135
- ```
136
-
137
- | 属性 | 类型 | 必填 | 默认值 | 说明 |
138
- | ------------------ | --------------------------------------- | ---- | ------------------------------------------ | --------------------------------------------------------- |
139
- | value | `string` | 否 | - | 受控值 |
140
- | onChange | `(value?, selectedOptionItem?) => void` | 否 | - | 值变化回调 |
141
- | defaultLabel | `string` | 否 | `labelDic` 第一个 key | 默认选中的搜索维度 key |
142
- | labelDic | `LabelDic` | 否 | `{ businessNo: '商户号', name: '商户名' }` | 搜索维度配置,key 为请求参数名,value 为显示文案 |
143
- | customLabelInValue | `boolean` | 否 | `false` | 是否将 value 编码为 `展示类型;ID;名称;labelDicKey` 格式 |
144
- | onFetchData | `(params, config) => Promise` | 否 | `apiService.merchantList` | 自定义数据请求函数 |
145
- | onTransformData | `(params) => SelectOptionItem` | 否 | 默认转换函数 | 将接口返回的列表项转换为 `{ label, value, customFields }` |
146
- | fetchParams | `Object` | 否 | `{}` | 额外请求参数,会合并到请求中 |
147
- | optionsDisplayType | `'labelAndValue'` | 否 | - | 选项显示格式,设置后显示 `value - label` |
148
- | disabled | `boolean` | 否 | - | 禁用状态 |
149
- | className | `string` | 否 | - | 右侧 Select 的自定义样式类名 |
150
- | style | `React.CSSProperties` | 否 | - | 自定义行内样式 |
151
-
152
- ## 导出类型
153
-
154
- ```typescript
155
- // 选项数据结构
156
- export type SelectOptionItem = {
157
- value: string
158
- label: string
159
- customFields?: Object | null
160
- }
161
-
162
- // 搜索维度配置
163
- export type LabelDic = Record<string, string>
164
- ```
165
-
166
- ## 导出工具函数
167
-
168
- ```typescript
169
- // 编码 customLabelInValue 的值
170
- export const getStrValue = (displayType: 'label' | 'value', value?: string, label?: string, labelDicKey?: string) => string | undefined
171
-
172
- // 解码 customLabelInValue 的值
173
- export const getSelectValue = (str?: string | null | number, returnType?: 'label' | 'value' | 'labelDicKey' | 'labelAndValue') =>
174
- string | undefined
175
-
176
- // 拼接 "value - label" 显示文本
177
- export const getInitialValue = (label?: string, value?: string) => string
178
-
179
- // 从 "value - label" 格式中取 value
180
- export const getFirstValue = (str?: string) => string
181
-
182
- // 从 "value - label" 格式中取 label
183
- export const getSecondValue = (str?: string) => string
184
- ```
185
-
186
- ## API 数据源
187
-
188
- - **内部 Hook**:`useReqData`(基于 `useAsyncFn` + `useDebounce`,600ms 防抖)
189
- - **默认接口**:`apiService.merchantList`
190
- - **请求参数**:`{ page: 1, pageSize: 20, [curSelectLabel]: curSearchValue, ...fetchParams }`
191
- - **可覆盖**:通过 `onFetchData` prop 传入自定义请求函数,或通过 `<ConfigProvider apiService={{ merchantList: customFn }}>` 替换默认实现
192
-
193
- ## 常见陷阱
194
-
195
- - ❌ 切换左侧维度后期望保留右侧已选值:
196
- ```tsx
197
- // 切换维度时组件会自动清空右侧选择值和搜索结果
198
- ```
199
- - ✅ 在 `onChange` 中处理值可能为 `undefined` 的情况
200
-
201
- - ❌ 使用 `customLabelInValue` 模式时直接展示 value:
202
- ```tsx
203
- // value 格式为 "label;123;某某商户;businessNo",不可直接展示
204
- ```
205
- - ✅ 使用 `getSelectValue` 解析所需字段:
206
-
207
- ```tsx
208
- const displayText = getSelectValue(value, 'label') // "某某商户"
209
- const id = getSelectValue(value, 'value') // "123"
210
- ```
211
-
212
- - ❌ 忘记提供 `onTransformData` 而使用非商户接口
213
- - ✅ 当自定义 `onFetchData` 时,务必同时提供 `onTransformData` 确保数据格式正确
214
-
215
- - ⚠️ 使用 `customLabelInValue` 时,初始值必须是分号分隔格式字符串(`displayType;value;label;labelDicKey`),不能直接传普通字符串或数字。
216
-
217
- ## 相关组件
218
-
219
- | 场景 | 组件 | 说明 |
220
- | ------------ | ------------------- | --------------------------------------- |
221
- | 简单下拉搜索 | `EffectStaffSelect` | 单维度员工选择器 |
222
- | API 配置 | `ConfigProvider` | 提供 `apiService.merchantList` 接口实现 |