feat(client): improve actionbar/form/markdown/space...

This commit is contained in:
chenos 2022-02-18 20:31:05 +08:00
parent 702c391bc2
commit ad38b001c5
4 changed files with 60 additions and 16 deletions

View File

@ -1,13 +1,22 @@
import { RecursionField, useFieldSchema } from '@formily/react'; import { observer, RecursionField, useFieldSchema } from '@formily/react';
import { Space } from 'antd'; import { Space } from 'antd';
import React from 'react'; import React from 'react';
import { useComponent } from '../../hooks'; import { useComponent } from '../../hooks';
export const ActionBar = () => { export const ActionBar = observer((props: any) => {
const { layout = 'tow-columns', style, ...others } = props;
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const ActionInitializer = useComponent(fieldSchema['x-action-initializer']); const ActionInitializer = useComponent(fieldSchema['x-action-initializer']);
if (layout === 'one-column') {
return (
<div style={{ display: 'flex', ...style }} {...others}>
{props.children && <div style={{ marginRight: 8 }}>{props.children}</div>}
{ActionInitializer && <ActionInitializer />}
</div>
);
}
return ( return (
<div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 16 }}> <div style={{ display: 'flex', justifyContent: 'space-between', ...style }} {...others}>
<div style={{ display: 'flex', justifyContent: 'space-between', width: '100%' }}> <div style={{ display: 'flex', justifyContent: 'space-between', width: '100%' }}>
<Space> <Space>
{fieldSchema.mapProperties((schema, key) => { {fieldSchema.mapProperties((schema, key) => {
@ -29,4 +38,4 @@ export const ActionBar = () => {
{ActionInitializer && <ActionInitializer />} {ActionInitializer && <ActionInitializer />}
</div> </div>
); );
}; });

View File

@ -74,10 +74,10 @@ const useDef = (opts: any = {}, props: FormProps = {}) => {
}; };
export const Form: React.FC<FormProps> = observer((props) => { export const Form: React.FC<FormProps> = observer((props) => {
const { request, initialValue, useValues = useDef, ...others } = props; const { request, effects, initialValue, useValues = useDef, ...others } = props;
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const form = useMemo(() => createForm(), []); const form = useMemo(() => createForm({ effects }), []);
const { loading } = useValues( const result = useValues(
{ {
uid: fieldSchema['x-uid'], uid: fieldSchema['x-uid'],
async onSuccess(data) { async onSuccess(data) {
@ -88,7 +88,7 @@ export const Form: React.FC<FormProps> = observer((props) => {
props, props,
); );
return ( return (
<Spin spinning={loading}> <Spin spinning={result?.loading || false}>
{fieldSchema['x-decorator'] === 'Form' ? ( {fieldSchema['x-decorator'] === 'Form' ? (
<FormDecorator form={form} {...others} /> <FormDecorator form={form} {...others} />
) : ( ) : (

View File

@ -4,6 +4,8 @@ import { Button, Input as AntdInput, Space } from 'antd';
import { marked } from 'marked'; import { marked } from 'marked';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { DragHandler } from '../../common';
import { useDesignable } from '../../hooks/useDesignable';
import { ReadPretty as InputReadPretty } from '../input'; import { ReadPretty as InputReadPretty } from '../input';
export function markdown(text) { export function markdown(text) {
@ -28,7 +30,7 @@ export const Markdown: any = connect(
}), }),
); );
function MarkdownTextArea(props: any) { function MarkdownEditor(props: any) {
const { t } = useTranslation(); const { t } = useTranslation();
const [value, setValue] = useState(props.defaultValue); const [value, setValue] = useState(props.defaultValue);
return ( return (
@ -63,15 +65,15 @@ function MarkdownTextArea(props: any) {
} }
Markdown.Void = observer((props: any) => { Markdown.Void = observer((props: any) => {
const { content } = props;
const field = useField();
const schema = useFieldSchema(); const schema = useFieldSchema();
const field = useField<any>(); const { dn } = useDesignable();
const text = schema['x-component-props']?.['content'] ?? schema['default'];
let value = <div className={'nb-markdown'} dangerouslySetInnerHTML={{ __html: markdown(text) }} />;
const { onSave, onCancel } = props; const { onSave, onCancel } = props;
return field.editable ? ( return field.editable ? (
<MarkdownTextArea <MarkdownEditor
{...props} {...props}
defaultValue={text} defaultValue={content}
onCancel={() => { onCancel={() => {
field.editable = false; field.editable = false;
onCancel?.(); onCancel?.();
@ -80,12 +82,45 @@ Markdown.Void = observer((props: any) => {
field.editable = false; field.editable = false;
schema['x-component-props'] ?? (schema['x-component-props'] = {}); schema['x-component-props'] ?? (schema['x-component-props'] = {});
schema['x-component-props']['content'] = value; schema['x-component-props']['content'] = value;
field.componentProps.content = value;
onSave?.(schema); onSave?.(schema);
dn.emit('patch', {
schema: {
'x-uid': schema['x-uid'],
'x-component-props': {
content: value,
},
},
});
}} }}
/> />
) : ( ) : (
<InputReadPretty.TextArea {...props} text={text} value={value} /> <div className={'nb-markdown'} dangerouslySetInnerHTML={{ __html: markdown(content) }} />
); );
}); });
Markdown.Void.Designer = () => {
const { remove } = useDesignable();
const field = useField();
return (
<div>
<a
onClick={() => {
remove();
}}
>
</a>
<a
onClick={() => {
field.editable = true;
}}
>
Edit
</a>
<DragHandler />
</div>
);
};
export default Markdown; export default Markdown;

View File

@ -5,7 +5,7 @@ import React from 'react';
export const Space: React.FC<SpaceProps> = (props) => { export const Space: React.FC<SpaceProps> = (props) => {
let { split } = props; let { split } = props;
if (split === '|') { if (split === '|') {
split = <Divider type="vertical" />; split = <Divider type="vertical" style={{ margin: '0 2px' }}/>;
} }
const layout = useFormLayout(); const layout = useFormLayout();
return React.createElement(AntdSpace, { return React.createElement(AntdSpace, {