Skip to content

Commit

Permalink
feat(QueryForm): 增加QueryForm组件,与其他查询结果类组件(例如表格)组合使用
Browse files Browse the repository at this point in the history
  • Loading branch information
xz8la8 committed Mar 31, 2021
1 parent 9814bf0 commit dbd7bd3
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ import { Form, Table, CreateForm, QueryTable, StepForm, StepQueryTable } from 's
## Discuss

<div>
<img src="https://img.alicdn.com/tfs/TB1iE3OCxD1gK0jSZFsXXbldVXa-1242-1602.jpg" width="320" />
<img src="https://img.alicdn.com/imgextra/i4/O1CN01YCq5ye21DbizEsE8e_!!6000000006951-2-tps-828-1068.png" width="300" />
</div>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ import { Form, Table, CreateForm, QueryTable, StepForm, StepQueryTable } from 's
## 讨论群

<div>
<img src="https://img.alicdn.com/tfs/TB1iE3OCxD1gK0jSZFsXXbldVXa-1242-1602.jpg" width="300" />
<img src="https://img.alicdn.com/imgextra/i4/O1CN01YCq5ye21DbizEsE8e_!!6000000006951-2-tps-828-1068.png" width="300" />
</div>
54 changes: 54 additions & 0 deletions docs/query-form/basic.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { QueryForm } from 'sula';

const queryFields = Array(10)
.fill(0)
.map((_, index) => {
return {
name: `input${index}`,
label: `Input${index}`,
field: 'input',
};
});

export default class BasicDemo extends React.Component {
state = {};

componentDidMount() {}

changevisibleFieldsCount = (v) => {
this.setState({ visibleFieldsCount: v });
};

render() {
const { visibleFieldsCount } = this.state;
return (
<div>
<button onClick={() => this.changevisibleFieldsCount(4)}>4</button>
<button onClick={() => this.changevisibleFieldsCount(3)}>3</button>
<button onClick={() => this.changevisibleFieldsCount(true)}></button>
<div style={{ background: 'rgb(241, 242, 246)', padding: 16, marginTop: 16 }}>
<QueryForm
key={visibleFieldsCount || 'all'}
visibleFieldsCount={visibleFieldsCount}
layout="vertical"
fields={queryFields}
actionsRender={[
{
type: 'button',
props: {
type: 'primary',
children: '查一下'
},
action: (ctx) => {
console.log('ctx: ', ctx);
console.log('fieldsValue', ctx.form.getFieldsValue());
},
},
]}
/>
</div>
</div>
);
}
}
55 changes: 55 additions & 0 deletions docs/query-form/hook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import { useForm, QueryForm } from 'sula';

const queryFields = Array(10)
.fill(0)
.map((_, index) => {
return {
name: `input${index}`,
label: `Input${index}`,
field: 'input',
};
});

const BasicDemo = () => {
const [form] = useForm();
return (
<div>
<button
onClick={() => {
form.setFieldsValue({
input0: '123123',
});
}}
>
设置表单值
</button>
<div style={{ background: 'rgb(241, 242, 246)', padding: 16, marginTop: 16 }}>
<QueryForm
form={form}
layout="horizontal"
labelAlign="left"
onValuesChange={(_, allValues) => {
console.log('allValues: ', allValues);
}}
fields={queryFields}
actionsRender={[
{
type: 'button',
props: {
type: 'primary',
children: '查一下',
},
action: (ctx) => {
console.log('ctx: ', ctx);
console.log('fieldsValue', ctx.form.getFieldsValue());
},
},
]}
/>
</div>
</div>
);
};

export default BasicDemo;
69 changes: 69 additions & 0 deletions docs/query-form/other.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';
import { FormInstance, QueryForm } from 'sula';

const queryFields = Array(10)
.fill(0)
.map((_, index) => {
return {
name: `input${index}`,
label: `Input${index}`,
field: 'input',
};
});

export default class BasicDemo extends React.Component {
formRef = React.createRef<FormInstance>();

render() {
return (
<div>
<button onClick={() => {
this.formRef.current?.setFieldsValue({
input0: '123123',
})
}}>设置表单值</button>
<div style={{ background: 'rgb(241, 242, 246)', padding: 16, marginTop: 16 }}>
<QueryForm
ref={this.formRef}
layout="horizontal"
labelAlign="left"
onValuesChange={(_, allValues) => {
console.log('allValues: ', allValues);
}}
fields={queryFields}
ctxGetter={() => {
return {
table: {
refreshTable: () => {
console.log('刷新表格')
}
}
}
}}
actionsRender={[
{
type: 'button',
props: {
type: 'primary',
children: '查一下'
},
action: [
{ type: 'validateQueryFields', resultPropName: '$queryFieldsValue' },
(ctx) => {
return new Promise((resolve, reject) => {
// mock
setTimeout(() => {
ctx.table?.refreshTable();
resolve('ok');
}, 2000);
})
},
],
},
]}
/>
</div>
</div>
);
}
}
8 changes: 8 additions & 0 deletions docs/sula-template/query-form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: QueryForm
order: 4
---

<code title="基本使用" src="../query-form/basic.jsx" />
<code title="其他属性" src="../query-form/other.tsx" />
<code title="hook" src="../query-form/hook.tsx" />
2 changes: 1 addition & 1 deletion packages/sula/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sula",
"version": "1.2.0-beta.38",
"version": "1.2.0-beta.41",
"module": "./es/index.js",
"main": "./es/index.js",
"types": "./es/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/sula/src/form/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ export default class Field extends React.Component<FieldProps> {
const subFormContext = {
formContext,
parentGroupName,
layout,
};

return (
Expand Down
60 changes: 15 additions & 45 deletions packages/sula/src/template-query-table/QueryFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import DownOutlined from '@ant-design/icons/DownOutlined';
import cx from 'classnames';
import { getItemSpan } from '../form/utils/layoutUtil';
import FieldGroupContext from '../form/FieldGroupContext';
import { FieldGroup, Field, FormAction, FieldProps, FormInstance } from '../form';
import { FieldGroup, Field, FormAction, FieldProps, FormInstance, FormProps } from '../form';
import './style/query-fields.less';
import LocaleReceiver from '../localereceiver';
import { ItemLayout, Layout } from '../form/FieldGroup';
import { toArray } from '../_util/common';

interface QueryFieldsProps {
export interface QueryFieldsProps {
fields: FieldProps[];
visibleFieldsCount: number | true;
itemLayout: ItemLayout;
layout: Layout;
visibleFieldsCount?: number | true;
hasBottomBorder?: boolean;
actionsRender: FormProps['actionsRender'],
/** @private */
getFormInstance: () => FormInstance;
hasActionsRender: boolean;
}

export default class QueryFields extends React.Component<QueryFieldsProps> {
Expand All @@ -29,18 +29,18 @@ export default class QueryFields extends React.Component<QueryFieldsProps> {
collapsed: true,
};

getVisibleFieldsCount = () => {
getVisibleFieldsCount = (): number => {
if (this.props.visibleFieldsCount === true) {
return this.props.fields.length;
}

return this.props.visibleFieldsCount;
return this.props.visibleFieldsCount!;
};

renderFields = () => {
const { fields, itemLayout } = this.props;
const { fields } = this.props;

const { matchedPoint } = this.context;
const { matchedPoint, itemLayout } = this.context;

const fieldsNameList = [];

Expand Down Expand Up @@ -84,40 +84,10 @@ export default class QueryFields extends React.Component<QueryFieldsProps> {
}

renderFormAction = (locale) => {
const { layout } = this.props;
const { layout } = this.context;
const { collapsed } = this.state;
const actionsRender = [
{
type: 'button',
props: {
type: 'primary',
children: locale.queryText,
},
action: [
{ type: 'validateQueryFields', resultPropName: '$queryFieldsValue' },
{
type: 'refreshTable',
args: [{ current: 1 }, '#{result}'],
},
],
},
{
type: 'button',
props: {
children: locale.resetText,
},
action: [
'resetFields',
{
type: 'resetTable',
args: [false],
},
{
type: 'refreshTable',
args: [{ current: 1 }, '#{form.getFieldsValue(true)}'],
},
],
},
...(toArray(this.props.actionsRender)),
...(this.hasMoreQueryFields()
? [
{
Expand Down Expand Up @@ -175,7 +145,7 @@ export default class QueryFields extends React.Component<QueryFieldsProps> {
};

render() {
const { hasActionsRender } = this.props;
const { hasBottomBorder } = this.props;
return (
<LocaleReceiver>
{(locale) => {
Expand All @@ -185,7 +155,7 @@ export default class QueryFields extends React.Component<QueryFieldsProps> {
type: 'div',
props: {
className: cx(`sula-template-query-table-fields-wrapper`, {
[`sula-template-query-table-fields-divider`]: hasActionsRender,
[`sula-template-query-table-fields-divider`]: hasBottomBorder,
}),
},
}}
Expand Down
45 changes: 45 additions & 0 deletions packages/sula/src/template-query-table/QueryForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import { Form, FormInstance, FormProps } from '../form';
import QueryFields, { QueryFieldsProps } from './QueryFields';

export interface QueryFormProps
extends Omit<FormProps, 'fields' | 'actionsPosition' | 'actionsRender'>,
Omit<QueryFieldsProps, 'getFormInstance'> {}

const QueryForm: React.ForwardRefRenderFunction<FormInstance, QueryFormProps> = (props, ref) => {
const {
layout,
itemLayout = {
cols: 3,
},
fields,
initialValues,
visibleFieldsCount,
actionsRender,
hasBottomBorder,
...restProps
} = props;
const [form] = Form.useForm(restProps.form);

React.useImperativeHandle(ref, () => form);

return (
<Form
{...restProps}
form={form}
initialValues={initialValues}
itemLayout={itemLayout}
layout={layout}
>
<QueryFields
fields={fields}
visibleFieldsCount={visibleFieldsCount}
getFormInstance={() => form}
hasBottomBorder={hasBottomBorder}
actionsRender={actionsRender}
/>
</Form>
);
};

export default React.forwardRef(QueryForm);
Loading

0 comments on commit dbd7bd3

Please sign in to comment.