Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kne-components/components-admin",
"version": "1.1.31",
"version": "1.1.32",
"description": "用于实现一个后台管理系统的必要组件",
"scripts": {
"init": "husky",
Expand Down
312 changes: 168 additions & 144 deletions src/components/GroupSelect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,162 +14,186 @@ const createComponent = (callback = item => item) => {
'components-core:Global@useGlobalValue'
]
})(
withLocale(({
remoteModules,
name,
label,
type,
language: propsLanguage,
rule,
apis,
valueKey = 'code',
labelKey = 'name',
single,
placeholder,
disabled,
groupName: propsGroupName,
...props
}) => {
const { formatMessage } = useIntl();
const groupName = propsGroupName || formatMessage({ id: 'GroupSelectDefaultName' });
const [FormInfo, useFormModal, usePreset, useGlobalValue] = remoteModules;
const { fields } = FormInfo;
const { SuperSelectTableList, SuperSelectTree } = fields;
const { ajax, apis: presetApis } = usePreset();
const { message, modal } = App.useApp();
const formModal = useFormModal();
const locale = useGlobalValue('locale');
withLocale(
({
remoteModules,
name,
label,
type,
language: propsLanguage,
rule,
apis,
valueKey = 'code',
labelKey = 'name',
single,
placeholder,
disabled,
groupName: propsGroupName,
...props
}) => {
const { formatMessage } = useIntl();
const groupName = propsGroupName || formatMessage({ id: 'GroupSelectDefaultName' });
const [FormInfo, useFormModal, usePreset, useGlobalValue] = remoteModules;
const { fields } = FormInfo;
const { SuperSelectTableList, SuperSelectTree } = fields;
const { ajax, apis: presetApis } = usePreset();
const { message, modal } = App.useApp();
const formModal = useFormModal();
const locale = useGlobalValue('locale');

const language = propsLanguage || locale || 'zh-CN';
const language = propsLanguage || locale || 'zh-CN';

const handleDelete = async (item, { fetchApi, value, setValue }) => {
try {
const { data: resData } = await ajax(
Object.assign({}, apis?.remove || presetApis?.group?.remove, {
data: { id: item.id, code: item.code, type }
})
);
const handleDelete = async (item, { fetchApi, value, setValue }) => {
try {
const { data: resData } = await ajax(
Object.assign({}, apis?.remove || presetApis?.group?.remove, {
data: { id: item.id, code: item.code, type }
})
);

if (resData.code !== 0) {
return;
}
if (resData.code !== 0) {
return;
}

message.success(formatMessage({ id: 'GroupSelectDeleteSuccess' }));
fetchApi.reload();
message.success(formatMessage({ id: 'GroupSelectDeleteSuccess' }));
fetchApi.reload();

// 如果删除的项目在已选值中,从已选值中移除
if (value && value.length > 0 && value.find(target => item[valueKey] === target[valueKey])) {
const index = value.findIndex(target => item[valueKey] === target[valueKey]);
const newValue = value.slice(0);
newValue.splice(index, 1);
setValue(newValue);
// 如果删除的项目在已选值中,从已选值中移除
if (value && value.length > 0 && value.find(target => item[valueKey] === target[valueKey])) {
const index = value.findIndex(target => item[valueKey] === target[valueKey]);
const newValue = value.slice(0);
newValue.splice(index, 1);
setValue(newValue);
}
} catch (error) {
message.error(formatMessage({ id: 'GroupSelectDeleteFailed' }));
}
} catch (error) {
message.error(formatMessage({ id: 'GroupSelectDeleteFailed' }));
}
};
};

const handleAdd = ({ reload }) => {
formModal({
title: formatMessage({ id: 'GroupSelectAdd' }, { name: groupName }),
size: 'small',
formProps: {
onSubmit: async formData => {
const { data: resData } = await ajax(
Object.assign({}, apis?.create || presetApis?.group?.create, {
data: Object.assign({}, formData, { type, language })
})
);
if (resData.code !== 0) {
return false;
const handleAdd = ({ reload }) => {
formModal({
title: formatMessage({ id: 'GroupSelectAdd' }, { name: groupName }),
size: 'small',
formProps: {
onSubmit: async formData => {
const { data: resData } = await ajax(
Object.assign({}, apis?.create || presetApis?.group?.create, {
data: Object.assign({}, formData, { type, language })
})
);
if (resData.code !== 0) {
return false;
}
message.success(formatMessage({ id: 'GroupSelectAddSuccess' }));
reload();
}
message.success(formatMessage({ id: 'GroupSelectAddSuccess' }));
reload();
}
},
children: (
<FormInfo
column={1}
list={[
<FormInfo.fields.Input name="code" label={formatMessage({ id: 'GroupSelectCode' })} rule="REQ" placeholder={formatMessage({ id: 'GroupSelectCodePlaceholder' })} />,
<FormInfo.fields.Input name="name" label={formatMessage({ id: 'GroupSelectName' })} rule="REQ" placeholder={formatMessage({ id: 'GroupSelectNamePlaceholder' }, { name: groupName })} />,
<SuperSelectTree
name="parentId"
label={formatMessage({ id: 'GroupSelectParent' })}
valueKey={valueKey}
labelKey={labelKey}
single
interceptor="object-output-value"
api={merge({}, apis?.groupList || presetApis?.group?.groupList, { params: { type, output: 'list', language } })}
/>,
<FormInfo.fields.TextArea name="description" label={formatMessage({ id: 'GroupSelectDescription' })} placeholder={formatMessage({ id: 'GroupSelectDescPlaceholder' }, { name: groupName })} />
]}
/>
)
});
};
},
children: (
<FormInfo
column={1}
list={[
<FormInfo.fields.Input
name="code"
label={formatMessage({ id: 'GroupSelectCode' })}
rule="REQ"
placeholder={formatMessage({ id: 'GroupSelectCodePlaceholder' })}
/>,
<FormInfo.fields.Input
name="name"
label={formatMessage({ id: 'GroupSelectName' })}
rule="REQ"
placeholder={formatMessage({ id: 'GroupSelectNamePlaceholder' }, { name: groupName })}
/>,
<SuperSelectTree
name="parentId"
label={formatMessage({ id: 'GroupSelectParent' })}
valueKey={valueKey}
labelKey={labelKey}
single
interceptor="object-output-value"
api={merge({}, apis?.groupList || presetApis?.group?.groupList, { params: { type, output: 'list', language } })}
/>,
<FormInfo.fields.TextArea
name="description"
label={formatMessage({ id: 'GroupSelectDescription' })}
placeholder={formatMessage({ id: 'GroupSelectDescPlaceholder' }, { name: groupName })}
/>
]}
/>
)
});
};

const hasApis = apis !== undefined;
const showAdd = hasApis ? !!apis?.create : !!presetApis?.group?.create;
const showDelete = hasApis ? !!apis?.remove : !!presetApis?.group?.remove;
const hasApis = apis !== undefined;
const showAdd = hasApis ? !!apis?.create : !!presetApis?.group?.create;
const showDelete = hasApis ? !!apis?.remove : !!presetApis?.group?.remove;

const columns = [
{ title: formatMessage({ id: 'GroupSelectCode' }), name: 'code', span: 4 },
{ title: formatMessage({ id: 'GroupSelectName' }), name: 'name', span: 6 },
{ title: formatMessage({ id: 'GroupSelectDescription' }), name: 'description', span: showDelete ? 10 : 14 }
];
const columns = [
{ title: formatMessage({ id: 'GroupSelectCode' }), name: 'code', span: 4 },
{ title: formatMessage({ id: 'GroupSelectName' }), name: 'name', span: 6 },
{ title: formatMessage({ id: 'GroupSelectDescription' }), name: 'description', span: showDelete ? 10 : 14 }
];

if (showDelete) {
columns.push({
title: formatMessage({ id: 'GroupSelectOperation' }),
name: 'options',
span: 4,
getValueOf: (item, { context }) => {
const { fetchApi, value, setValue } = context;
return (
<span onClick={e => e.stopPropagation()}>
<a
onClick={() => {
modal.confirm({
title: formatMessage({ id: 'GroupSelectConfirmDelete' }),
content: formatMessage({ id: 'GroupSelectDeleteConfirm' }, { name: groupName, title: item.name }),
okText: formatMessage({ id: 'GroupSelectConfirm' }),
cancelText: formatMessage({ id: 'GroupSelectCancel' }),
onOk: () => handleDelete(item, { fetchApi, value, setValue })
});
}}>
{formatMessage({ id: 'GroupSelectDelete' })}
</a>
</span>
);
}
});
}
if (showDelete) {
columns.push({
title: formatMessage({ id: 'GroupSelectOperation' }),
name: 'options',
span: 4,
getValueOf: (item, { context }) => {
const { fetchApi, value, setValue } = context;
return (
<span onClick={e => e.stopPropagation()}>
<a
onClick={() => {
modal.confirm({
title: formatMessage({ id: 'GroupSelectConfirmDelete' }),
content: formatMessage({ id: 'GroupSelectDeleteConfirm' }, { name: groupName, title: item.name }),
okText: formatMessage({ id: 'GroupSelectConfirm' }),
cancelText: formatMessage({ id: 'GroupSelectCancel' }),
onOk: () => handleDelete(item, { fetchApi, value, setValue })
});
}}>
{formatMessage({ id: 'GroupSelectDelete' })}
</a>
</span>
);
}
});
}

const Component = callback(SuperSelectTableList);
return (
<Component
className={style['group-list']}
name={name}
label={label}
rule={rule}
placeholder={placeholder}
disabled={disabled}
single={single}
valueKey={valueKey}
labelKey={labelKey}
api={merge({}, apis?.list || presetApis?.group?.list, { params: { type, language } })}
getSearchProps={({ searchText }) => ({
filter: { keyword: searchText }
})}
pagination={{ paramsType: 'params' }}
columns={columns}
isPopup={false}
footer={showAdd ? ({ reload }) => <Button type="link" onClick={() => handleAdd({ reload })}>{formatMessage({ id: 'GroupSelectAdd' }, { name: groupName })}</Button> : null}
{...props}
/>
);
})
const Component = callback(SuperSelectTableList);
return (
<Component
className={style['group-list']}
name={name}
label={label}
rule={rule}
placeholder={placeholder}
disabled={disabled}
single={single}
valueKey={valueKey}
labelKey={labelKey}
api={merge({}, apis?.list || presetApis?.group?.list, { params: { type, filter: { language } } })}
getSearchProps={({ searchText }) => ({
filter: { keyword: searchText }
})}
pagination={{ paramsType: 'params' }}
columns={columns}
isPopup={false}
footer={
showAdd
? ({ reload }) => (
<Button type="link" onClick={() => handleAdd({ reload })}>
{formatMessage({ id: 'GroupSelectAdd' }, { name: groupName })}
</Button>
)
: null
}
{...props}
/>
);
}
)
);
};

Expand Down
Loading