DialogAddProduct

DialogAddProduct 添加商品 dialog

代码示例

import { BfDialogAddProduct, BfInput, BfRadioGroup, BfSwitch } from '@bud-fe/react-taro-ui';
import type { TCustomListItem, TForm } from '@bud-fe/react-taro-ui/es/components/bf-dialog-add-product';
import { verifyHelper } from '@bud-fe/utils';
import { Button } from '@tarojs/components';
import React, { useState } from 'react';
export default () => {
const [isBaseOpened, setIsBaseOpened] = useState(false);
const [isDirectSearchOpened, setIsDirectSearchOpened] = useState(false);
const [isCustomOpened, setIsCustomOpened] = useState(false);
const [searchOnly, setSearchOnly] = useState(false);
const [hideDefault, setHideDefault] = useState(false);
const [aboveFields, setAboveFields] = useState<TCustomListItem[]>([
{
name: 'gift',
label: '赠品',
value: '1',
render: (data: TForm) => (
<BfRadioGroup
value={data?.gift}
groups={[
{ label: '是', value: '1' },
{ label: '否', value: '2' },
]}
onChange={(val) => {
setAboveFields((pre) => {
pre[0].value = val;
return [...pre];
});
}}
/>
),
},
{
name: 'toggle',
label: '显示or隐藏默认表单项',
value: true,
render: (data: TForm) => (
<BfSwitch
checked={data?.toggle}
onChange={(val) => {
setHideDefault(!val);
setAboveFields((pre) => {
pre[1].value = val;
return [...pre];
});
}}
/>
),
},
]);
const [bottomFields, setBottomFields] = useState<TCustomListItem[]>([
{
name: 'forecastSales',
label: '预估销量',
value: 100,
render: (data: TForm) => (
<BfInput
placeholder="请填写预估销量"
disableDebounce
value={data?.forecastSales}
type="number"
onChange={(v) => {
setBottomFields((pre) => {
pre[0].value = v;
return [...pre];
});
}}
/>
),
},
]);
const getConfirmDisable = (formData, customFormData) => {
for (const key of Object.keys(formData)) {
if (verifyHelper.isNull(formData[key]?.value)) {
return true;
}
}
const { gift, forecastSales } = customFormData;
if (verifyHelper.isNull(gift) || verifyHelper.isNull(forecastSales)) {
return true;
}
return false;
};
return (
<>
<Button
className="group-title"
onClick={() => {
setSearchOnly(false);
setIsBaseOpened(true);
}}
>
基础用法
</Button>
<Button
className="group-title"
onClick={() => {
setIsDirectSearchOpened(true);
}}
>
直接显示商品搜索
</Button>
<Button
className="group-title"
onClick={() => {
setSearchOnly(true);
setIsBaseOpened(true);
}}
>
基础用法 - searchOnly=true
</Button>
<Button className="group-title" onClick={() => setIsCustomOpened(true)}>
自定义渲染
</Button>
<BfDialogAddProduct
isOpened={isBaseOpened}
searchOnly={searchOnly}
requestOrderly={false}
searchHistoryKey="a"
initialValues={{
id: 1, // 可以传一些自定义的字段,会在onConfirm时透传回来
// brand: { label: '默认的品牌', value: '0' },
// subBrand: { label: '默认的子品牌', value: '0' },
// capacity: { label: '默认的容量', value: '0' },
// pack: { label: '默认的包装', value: '0', disabled: true },
// product: { label: '默认的商品', value: '0' },
}}
// productionDate={false}
requestBrand={async () => {
console.log('requestBrand');
return Array.from({ length: 20 }, (_, index) => ({
label: `品牌${index}`,
value: index,
}));
}}
requestSubBrand={async (brandId: string) => {
console.log('requestSubBrand', brandId);
return Array.from({ length: 20 }, (_, index) => ({
label: `子品牌${index}`,
value: index,
}));
}}
requestCapacity={async (params) => {
console.log('requestCapacity', params);
return Array.from({ length: 20 }, (_, index) => ({
label: `容量${index}`,
value: index,
}));
}}
requestPackaging={async (params) => {
console.log('requestPackaging', params);
return Array.from({ length: 20 }, (_, index) => ({
label: `包装${index}`,
value: index,
}));
}}
requestProduct={async (params) => {
// NOTE: params为请求商品选项数据时需要携带的参数(分页、搜索关键字、当前其他字段值)
console.log('requestProduct', params);
return Array.from({ length: 20 }, (_, index) => ({
label: `百威纯生 500ml 大听(${index})`,
value: index,
brandCode: `${index}`,
brandName: 'brandName',
subBrandCode: `${index}`,
subBrandName: 'subBrandName',
capacityCode: `${index}`,
capacityName: 'capacityName',
packCode: `${index}`,
packName: 'packName',
}));
}}
onCancel={() => setIsBaseOpened(false)}
onConfirm={(values, customValues) => {
console.log('onConfirm', { values, customValues });
setIsBaseOpened(false);
}}
/>
<BfDialogAddProduct.DirectSearch
isOpened={isDirectSearchOpened}
searchHistoryKey="a"
searchBarProps={{ confirmType: 'search' }}
requestProduct={async (params) => {
// NOTE: params为请求商品选项数据时需要携带的参数(分页、搜索关键字、当前其他字段值)
console.log('requestProduct', params);
return Array.from({ length: 20 }, (_, index) => ({
label: `百威纯生 500ml 大听(${index})`,
value: index,
brandCode: `${index}`,
brandName: 'brandName',
subBrandCode: `${index}`,
subBrandName: 'subBrandName',
capacityCode: `${index}`,
capacityName: 'capacityName',
packCode: `${index}`,
packName: 'packName',
}));
}}
onCancel={() => setIsDirectSearchOpened(false)}
onConfirm={(values) => {
console.log('onConfirm', { values });
setIsDirectSearchOpened(false);
}}
/>
<BfDialogAddProduct
isOpened={isCustomOpened}
hideDefault={hideDefault}
initialValues={{
brand: { label: '默认的品牌', value: '0' },
subBrand: { label: '默认的子品牌', value: '0' },
capacity: { label: '默认的容量', value: '0' },
pack: { label: '默认的包装', value: '0', disabled: true },
product: { label: '默认的商品', value: '0' },
}}
// NOTE: 若传入 confirmDisable, 说明确认按钮的disabled受控
// 否则会判断每个表单项的value来控制是否disabled
confirmDisable={getConfirmDisable}
above={aboveFields}
bottom={bottomFields}
requestBrand={async () => {
console.log('requestBrand');
return Array.from({ length: 20 }, (_, index) => ({
label: `品牌${index}`,
value: index,
}));
}}
requestSubBrand={async (brandId: string) => {
console.log('requestSubBrand', brandId);
return Array.from({ length: 20 }, (_, index) => ({
label: `子品牌${index}`,
value: index,
}));
}}
requestCapacity={async (params) => {
console.log('requestCapacity', params);
return Array.from({ length: 20 }, (_, index) => ({
label: `容量${index}`,
value: index,
}));
}}
requestPackaging={async (params) => {
console.log('requestPackaging', params);
return Array.from({ length: 20 }, (_, index) => ({
label: `包装${index}`,
value: index,
}));
}}
requestProduct={async (params) => {
// NOTE: params为请求商品选项数据时需要携带的参数(分页、搜索关键字、当前其他字段值)
console.log('requestProduct', params);
return Array.from({ length: 20 }, (_, index) => ({
label: `百威纯生 500ml 大听(${index})`,
value: index,
brandCode: `${index}`,
brandName: 'brandName',
subBrandCode: `${index}`,
subBrandName: 'subBrandName',
capacityCode: `${index}`,
capacityName: 'capacityName',
packCode: `${index}`,
packName: 'packName',
}));
}}
onCancel={() => setIsCustomOpened(false)}
onConfirm={(values, customValues) => {
console.log('onConfirm', { values, customValues });
setIsCustomOpened(false);
}}
/>
</>
);
};

API

BfDialogAddProduct

属性名描述类型默认值
requestProduct请求商品选项数据。params 为请求商品选项数据时需要携带的参数(分页、搜索关键字、当前其他字段值)(params: TRequestProductParams) => Promise<IOptions[]>(必选)
onConfirm确认后的回调。values 为默认的表单项,customValues 为有自定义渲染的表单项 回调: 确认((values: TForm, customValues?: Record<string, any>) => void) & (() => void)--
requestBrand请求品牌选项数据() => Promise<IOptions[]>--
requestSubBrand请求子品牌选项数据(brandId: string) => Promise<IOptions[]>--
requestCapacity请求容量选项数据。params 为当前其他字段值(params: TRequestParams) => Promise<IOptions[]>--
requestPackaging请求包装选项数据。params 为当前其他字段值(params: TRequestParams) => Promise<IOptions[]>--
requestProductDebounceTime搜索商品的防抖时间number500
requestOrderly选项数据(子品牌、容量、包装)的请求是否根据之前选项是否有值触发booleanfalse
initialValues默认值Partial<TForm>--
requestProductFieldNames商品选项数据中对应字段值(选完商品回填用)TRequestProductFieldNames{ brandCode: 'brandCode', brandName: 'brandName', subBrandCode: 'subBrandCode', subBrandName: 'subBrandName', capacityCode: 'capacityCode', capacityName: 'capacityName', packCode: 'packCode', packName: 'packName' }
productionDate是否展示生产日期booleanfalse
above默认表单项上方的自定义渲染TCustomListItem[]--
bottom默认表单项下方的自定义渲染TCustomListItem[]--
hideDefault是否隐藏默认表单项(商品搜索、品牌、子品牌、容量、包装、商品)booleanfalse
readonlyDefault是否默认表单项都为只读(品牌、子品牌、容量、包装、商品)booleantrue
searchOnly是否只展示顶部的商品搜索框booleanfalse
confirmDisable确认按钮是否禁用。传入函数时参数为当前表单项的值boolean | ((formData: Partial<TForm>, customFormData?: Record<string, any>) => boolean)--
searchHistoryKey存储搜索历史的localStorage的keystring--
searchHistoryMaxCount搜索历史存储的最大数量number5
searchBarProps内部 BfSearchBar 的 propsany--
isOpened是否打开booleanfalse
title标题any--
content内容any--
contentClass内容区样式string--
showActions是否展示操作栏booleantrue
actionMode操作栏模式(单/双按钮)。默认为双按钮"singleBtn" | "doubleBtn"doubleBtn
actionTip操作栏底部提示string--
cancelText自定义取消按钮文本string'取消'
confirmText自定义确认按钮文本string'确定'
cancelDisable取消按钮是否禁用booleanfalse
styleMode对话框样式。默认为bf样式"base" | "bf"'bf'
showCloseIcon是否展示关闭icon。bf样式时有效booleantrue
showActionShadow当内容区长度超过最大高度时是否显示吸底按钮上方阴影booleantrue
onCancel回调: 取消() => void--
onTipClick回调: 提示() => void--

BfDialogAddProduct.DirectSearch

属性名描述类型默认值
requestProduct请求商品选项数据。params 为请求商品选项数据时需要携带的参数(分页、搜索关键字)(params: TRequestProductParams) => Promise<IOptions[]>(必选)
isOpened是否打开booleanfalse
onConfirm确认后的回调。values 为默认的表单项(values: TForm) => void--
onCancel回调: 取消() => void--
requestProductDebounceTime搜索商品的防抖时间number500
requestProductFieldNames商品选项数据中对应字段值(选完商品回填用)TRequestProductFieldNames{ brandCode: 'brandCode', brandName: 'brandName', subBrandCode: 'subBrandCode', subBrandName: 'subBrandName', capacityCode: 'capacityCode', capacityName: 'capacityName', packCode: 'packCode', packName: 'packName' }
searchHistoryKey存储搜索历史的localStorage的keystring--
searchHistoryMaxCount搜索历史存储的最大数量number5
searchBarProps内部 BfSearchBar 的 propsany--
dumi10:24