tachybase_todo/packages/plugins/china-region/src/client/index.tsx

81 lines
2.0 KiB
TypeScript
Raw Normal View History

import { ArrayField } from '@formily/core';
import { useField } from '@formily/react';
import { SchemaComponentOptions, useAPIClient, useRequest } from '@nocobase/client';
import React from 'react';
const useChinaRegionDataSource = (options) => {
const field = useField<ArrayField>();
const maxLevel = field.componentProps.maxLevel;
return useRequest(
{
resource: 'chinaRegions',
action: 'list',
params: {
sort: 'code',
paginate: false,
filter: {
level: 1,
},
},
},
{
...options,
onSuccess(data) {
options?.onSuccess({
data:
data?.data?.map((item) => {
if (maxLevel !== 1) {
item.isLeaf = false;
}
return item;
}) || [],
});
},
refactor(association-field): support sub table (#1862) * fix: association select support data scope and sort setting * fix: appends on demand loading * fix: asociationSelect support multiple * fix: recordPicker -> AssociationField * fix: add migration * fix: audit logs not show assication data * fix: flattenNestedList * refactor: file manager field mode * refactor: field mode refactor * fix: subtable action * fix: subtable appends * refactor: code improve * fix: nester add new * feat: sub table * fix: data scope not effect immediately * fix: association add new * fix: association field failed to add new and mutual influence * style: style improve * style: style improve * refactor: updateAssociationValues * refactor: form init values * refactor: select options * fix: form initialValues * fix: record picker values * fix: field value change when field mode change * fix: select data scope * feat: add migration * fix: table column enable link * fix: table column enable link * refactor: locale improve * fix: migration * fix: mutiple config * fix: readPretty enable link * fix: appends on demand * fix: enable link style * refactor: locale improve * refactor: locale improve * feat: sub-form migration * fix: skip migration * fix: translation * fix: skip migration * fix: getLabelFormatValue * fix: error TS2339: Property 'find' does not exist on type 'string | SchemaEnum<any>' * refactor: remove the logic code for converting old record picker * refactor: locale * refactor: association field support sub-table * refactor: locale * fix: sub-table should not support add new * refactor: code improve * refactor: locale * fix: compatibility history Subtable * fix: getNesterAppends filter bug * refactor: useFieldModeOptions * refactor: subTable * fix: associationSelect * fix: associationSelect * fix: associationSelect * fix: getAssociationAppends * fix: sub-form collection context * refactor: code improve * refactor: code improve * refactor: code improve * refactor: code improve * refactor: code improve * fix: sub-table * fix: appends * fix: sub-table-appends * fix: undefine id * fix: sub-table appends * refactor: sub-table * refactor: sub-table supoort delete * fix: sub-table-appends * refactor: sub table initvvalue * fix: sub-table-appends * fix: association field appends * fix: association field appends * fix: association field appends * style: style improve * style: style improve * refactor: textArear Markdown RichText * style: style improve * fix: title field * refactor: code improve * fix: system fields are not allowed to be edited * refactor: subtable does not support inline operations * refactor: jons edit in table * refactor: sequence readOnly in table * refactor: json readOnly in table * fix: sub-table appends * fix: runWhenParamsChanged * fix: runWhenParamsChanged * fix: sub-table appends * fix: sub-table appends * refactor: sub-table * style: checkbox group intable * style: delete button style improve * fix: json edit in table * fix: sub-table appends * style: upload file in table column * refactor: map edit in table * refactor: map edit in table * refactor: map field display in sub-table * fix: support mutiple row * refactor: sub-table * refactor: sub-table not support sortable * fix: sub-table appends * fix: sub-table appends * chore: useAssociationNames * refactor: code improve * refactor: support blocktemplate * refactor: support blocktemplate * fix: subtable * fix: subtable initValue * refactor: useAssociationNames in block template * fix: cross hierarchical association field layout * fix: normal field of association field should be append * style: upload field in table * refactor: sub table should not row selection * style: checkboxGroup and radioGroup display in sub-table * refactor: locale * refactor: code improve * refactor: quickEdit in sub-table * refactor: quickEdit * refactor: quickEdit * refactor: quickEdit * refactor: quickEdit * style: quickEdit style improve * style: quickEdit style improve * style: quickEdit style improve * refactor: sub-table support required config * refactor: cell validate * refactor: cell validate * refactor: cell required validate * refactor: cell required validate * refactor: sub-table display when readPretty * refactor: sub-table required * refactor: sub-table required * refactor: sub-table required * refactor: sub-table initialValue * refactor: table * refactor: sub-tble field required * refactor: sub-table style improve * refactor: sub-table attachemnt * refactor: code improve * test: tableV2 snap * fix: subtable drag * test: tableV2 snap * refactor: checkbox group and radio group in sub-table * refactor: sub-table required style * style: markdown style in sub-table * refactor: markdown display in sub-table * refactor: json field display in sub-table * refactor: code improve * style: style improve * style: style improve * refactor: inserView when designable * refactor: china-region load data when dropdown click * refactor: code improve * refactor: code improve * refactor: display loading when the form is first loaded * refactor: display loading when the form is first loaded * fix: verification exception when deleting sub table rows * refactor: sub-table code improve * refactor: sub-table code improve * fix: json error feedback * refactor: code improve * fix: json onchange * fix: sub table data did not change when switching data templates * test: variable test * test: variable test * refactor: data template * refactor: code improve * refactor: code improve * fix: upload readpretty * fix: attach upload in sub-tabl * fix: remove small * fix: attach upload in sub-table * refactor: association fields should not display in sub-table * fix: dropdownMatchSelectWidth * refactor: association fields should not display in sub-table * refactor: selectFile is false in sub-table * refactor: attacattachment in sub-table * refactor: attacattachment in sub-table * fix: selector style * refactor: quick edit component * fix: zindex * style: style improve * style: style improve * feat: styling * fix: style * fix: style * refactor: quickEdit getContainer * refactor: quickEdit * refactor: quickEdit * fix: improve code * refactor: code improve * refactor: code improve --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-06-16 12:07:43 +08:00
manual: true,
},
);
};
const useChinaRegionLoadData = () => {
const api = useAPIClient();
const field = useField<ArrayField>();
const maxLevel = field.componentProps.maxLevel;
return (selectedOptions) => {
const targetOption = selectedOptions[selectedOptions.length - 1];
if (targetOption?.children?.length > 0) {
return;
}
targetOption.loading = true;
api
.resource('chinaRegions')
.list({
sort: 'code',
paginate: false,
filter: {
parentCode: targetOption.code,
},
})
.then(({ data }) => {
targetOption.loading = false;
targetOption.children =
data?.data?.map((item) => {
if (maxLevel > item.level) {
item.isLeaf = false;
}
return item;
}) || [];
field.dataSource = [...field.dataSource];
});
};
};
export const ChinaRegionProvider = (props) => {
return (
<SchemaComponentOptions scope={{ useChinaRegionDataSource, useChinaRegionLoadData }}>
{props.children}
</SchemaComponentOptions>
);
};
export default ChinaRegionProvider;