feat: improve kanban

This commit is contained in:
chenos 2022-04-25 08:35:07 +08:00
parent cc4da4ca15
commit 21448f3232
5 changed files with 68 additions and 34 deletions

View File

@ -2,6 +2,7 @@ import { ArrayField } from '@formily/core';
import { useField } from '@formily/react'; import { useField } from '@formily/react';
import { Spin } from 'antd'; import { Spin } from 'antd';
import React, { createContext, useContext, useEffect } from 'react'; import React, { createContext, useContext, useEffect } from 'react';
import { useACLRoleContext } from '../acl';
import { useCollection } from '../collection-manager'; import { useCollection } from '../collection-manager';
import { toColumns } from '../schema-component/antd/kanban-v2/Kanban'; import { toColumns } from '../schema-component/antd/kanban-v2/Kanban';
import { BlockProvider, useBlockRequestContext } from './BlockProvider'; import { BlockProvider, useBlockRequestContext } from './BlockProvider';
@ -33,6 +34,9 @@ const InternalKanbanBlockProvider = (props) => {
return ( return (
<KanbanBlockContext.Provider <KanbanBlockContext.Provider
value={{ value={{
props: {
resource: props.resource,
},
field, field,
service, service,
resource, resource,
@ -56,6 +60,16 @@ export const useKanbanBlockContext = () => {
return useContext(KanbanBlockContext); return useContext(KanbanBlockContext);
}; };
const useDisableCardDrag = () => {
const ctx = useKanbanBlockContext();
const { allowAll, allowConfigure, getActionParams } = useACLRoleContext();
if (allowAll || allowConfigure) {
return false;
}
const result = getActionParams(`${ctx?.props?.resource}:update`, { skipOwnCheck: true });
return !result;
}
export const useKanbanBlockProps = () => { export const useKanbanBlockProps = () => {
const field = useField<ArrayField>(); const field = useField<ArrayField>();
const ctx = useKanbanBlockContext(); const ctx = useKanbanBlockContext();
@ -67,6 +81,7 @@ export const useKanbanBlockProps = () => {
}, [ctx?.service?.loading]); }, [ctx?.service?.loading]);
return { return {
groupField: ctx.groupField, groupField: ctx.groupField,
disableCardDrag: useDisableCardDrag(),
async onCardDragEnd({ columns, groupField }, { fromColumnId, fromPosition }, { toColumnId, toPosition }) { async onCardDragEnd({ columns, groupField }, { fromColumnId, fromPosition }, { toColumnId, toPosition }) {
const sourceColumn = columns.find((column) => column.id === fromColumnId); const sourceColumn = columns.find((column) => column.id === fromColumnId);
const destinationColumn = columns.find((column) => column.id === toColumnId); const destinationColumn = columns.find((column) => column.id === toColumnId);

View File

@ -58,7 +58,7 @@ export const Kanban: ComposedKanban = observer((props: any) => {
const { groupField, onCardDragEnd, ...restProps } = useProps(props); const { groupField, onCardDragEnd, ...restProps } = useProps(props);
const field = useField<ArrayField>(); const field = useField<ArrayField>();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const [disableCardDrag, setDisableCardDrag] = useState(false); const [disableCardDrag, setDisableCardDrag] = useState(!!restProps.disableCardDrag);
const schemas = useMemo( const schemas = useMemo(
() => () =>
fieldSchema.reduceProperties( fieldSchema.reduceProperties(
@ -88,7 +88,7 @@ export const Kanban: ComposedKanban = observer((props: any) => {
return ( return (
<Spin spinning={field.loading || false}> <Spin spinning={field.loading || false}>
<Board <Board
allowAddCard allowAddCard={!!schemas.cardAdder}
disableColumnDrag disableColumnDrag
cardAdderPosition={'bottom'} cardAdderPosition={'bottom'}
disableCardDrag={disableCardDrag} disableCardDrag={disableCardDrag}
@ -124,6 +124,9 @@ export const Kanban: ComposedKanban = observer((props: any) => {
); );
}} }}
renderCardAdder={({ column }) => { renderCardAdder={({ column }) => {
if (!schemas.cardAdder) {
return null;
}
return ( return (
<KanbanColumnContext.Provider value={{ column, groupField }}> <KanbanColumnContext.Provider value={{ column, groupField }}>
<SchemaComponentOptions scope={{ useCreateActionProps }}> <SchemaComponentOptions scope={{ useCreateActionProps }}>

View File

@ -0,0 +1,35 @@
export const KanbanActionInitializers = {
title: "{{t('Configure actions')}}",
icon: 'SettingOutlined',
style: {
marginLeft: 8,
},
items: [
{
type: 'itemGroup',
title: "{{t('Enable actions')}}",
children: [
{
type: 'item',
title: "{{t('Filter')}}",
component: 'FilterActionInitializer',
schema: {
'x-align': 'left',
},
},
{
type: 'item',
title: "{{t('Add new')}}",
component: 'CreateActionInitializer',
schema: {
'x-align': 'right',
'x-decorator': 'ACLActionProvider',
'x-acl-action-props': {
skipScopeCheck: true,
},
},
},
],
},
],
};

View File

@ -4,6 +4,7 @@ export * from './CreateFormBlockInitializers';
export * from './DetailsActionInitializers'; export * from './DetailsActionInitializers';
export * from './FormActionInitializers'; export * from './FormActionInitializers';
export * from './FormItemInitializers'; export * from './FormItemInitializers';
export * from './KanbanActionInitializers';
export * from './ReadPrettyFormActionInitializers'; export * from './ReadPrettyFormActionInitializers';
export * from './ReadPrettyFormItemInitializers'; export * from './ReadPrettyFormItemInitializers';
export * from './RecordBlockInitializers'; export * from './RecordBlockInitializers';

View File

@ -701,8 +701,19 @@ export const createKanbanBlockSchema = (options) => {
...others, ...others,
}, },
'x-designer': 'KanbanV2.Designer', 'x-designer': 'KanbanV2.Designer',
'x-component': 'BlockItem', 'x-component': 'CardItem',
properties: { properties: {
actions: {
type: 'void',
'x-initializer': 'KanbanActionInitializers',
'x-component': 'ActionBar',
'x-component-props': {
style: {
marginBottom: 16,
},
},
properties: {},
},
[uid()]: { [uid()]: {
type: 'array', type: 'array',
'x-component': 'KanbanV2', 'x-component': 'KanbanV2',
@ -724,37 +735,6 @@ export const createKanbanBlockSchema = (options) => {
}, },
}, },
}, },
cardAdder: {
title: '{{t("Add new")}}',
type: 'void',
'x-designer': 'Action.Designer',
'x-designer-props': {
draggable: false,
},
'x-component': 'KanbanV2.CardAdder',
'x-component-props': {
type: 'text',
openMode: 'drawer',
},
properties: {
drawer: {
type: 'void',
title: '{{ t("Add record") }}',
'x-component': 'Action.Container',
'x-component-props': {
className: 'nb-action-popup',
},
properties: {
grid: {
type: 'void',
'x-component': 'Grid',
'x-initializer': 'CreateFormBlockInitializers',
properties: {},
},
},
},
},
},
cardViewer: { cardViewer: {
type: 'void', type: 'void',
title: '{{ t("View") }}', title: '{{ t("View") }}',