Skip to content

Commit

Permalink
feat(element): support createFormGrid api (#2510)
Browse files Browse the repository at this point in the history
  • Loading branch information
muuyao authored Nov 25, 2021
1 parent a51d189 commit cadd63b
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default {
computed: {
decodedCode() {
return decodeURIComponent(this.code || this.demoStr)
return this.code || this.demoStr
},
highlightCode() {
Expand Down
19 changes: 19 additions & 0 deletions packages/element/docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,23 @@ module.exports = {
},
}
},
chainWebpack: (config, isServer) => {
config.module
.rule('js') // Find the rule.
.use('babel-loader') // Find the loader
.tap((options) =>
Object.assign(options, {
// Modifying options
presets: [
[
'@vue/babel-preset-jsx',
{
vModel: false,
compositionAPI: true,
},
],
],
})
)
},
}
223 changes: 223 additions & 0 deletions packages/element/docs/demos/guide/form-grid/form.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaObjectField x-component="QueryForm">
<SchemaStringField
name="input1"
title="Input 1"
x-component="Input"
x-decorator="FormItem"
/>
<SchemaStringField
name="input2"
title="Input 2"
x-component="Input"
x-decorator="FormItem"
/>

<SchemaStringField
name="select1"
title="Select 1"
x-component="Select"
x-decorator="FormItem"
/>
<SchemaStringField
name="select2"
title="Select 2"
x-component="Select"
x-decorator="FormItem"
/>
<SchemaStringField
name="date"
title="DatePicker"
x-component="DatePicker"
x-decorator="FormItem"
/>
<SchemaStringField
name="dateRange"
title="DatePicker"
x-component="DatePicker"
x-decorator="FormItem"
:x-decorator-props="{
gridSpan: 2,
}"
:x-component-props="{
type: 'daterange',
}"
/>
<SchemaStringField
name="select3"
title="Select 3"
x-component="Select"
x-decorator="FormItem"
/>
</SchemaObjectField>
</SchemaField>
</FormProvider>
</template>

<script>
import { defineComponent, ref, onUnmounted } from '@vue/composition-api'
import { createForm } from '@formily/core'
import {
createSchemaField,
FormProvider,
FragmentComponent,
} from '@formily/vue'
import { autorun } from '@formily/reactive'
import { observer } from '@formily/reactive-vue'
import {
Form,
Input,
Select,
DatePicker,
FormItem,
FormGrid,
Submit,
Reset,
FormButtonGroup,
} from '@formily/element'
const useCollapseGrid = (maxRows) => {
const grid = FormGrid.createFormGrid({
maxColumns: 4,
maxWidth: 240,
maxRows: maxRows,
shouldVisible: (node, grid) => {
if (node.index === grid.childSize - 1) return true
if (grid.maxRows === Infinity) return true
return node.shadowRow < maxRows + 1
},
})
const expanded = ref(false)
const type = ref('')
const takeType = (realRows, computeRows) => {
if (realRows < maxRows + 1) return 'incomplete-wrap'
if (computeRows > maxRows) return 'collapsible'
return 'complete-wrap'
}
const dispose = autorun(() => {
expanded.value = grid.maxRows === Infinity
const realRows = grid.shadowRows
const computeRows = grid.fullnessLastColumn
? grid.shadowRows - 1
: grid.shadowRows
type.value = takeType(realRows, computeRows)
})
onUnmounted(dispose)
const toggle = () => {
if (grid.maxRows === Infinity) {
grid.maxRows = maxRows
} else {
grid.maxRows = Infinity
}
}
return {
grid,
expanded,
toggle,
type,
}
}
const QueryForm = observer(
defineComponent({
setup(props, { slots }) {
const { grid, expanded, toggle, type } = useCollapseGrid(1)
const renderActions = () => {
return (
<FragmentComponent>
<Submit onSubmit={console.log}>查询</Submit>
<Reset>重置</Reset>
</FragmentComponent>
)
}
const renderButtonGroup = () => {
if (type.value === 'incomplete-wrap') {
return (
<FormButtonGroup.FormItem>
<FormButtonGroup>{renderActions()}</FormButtonGroup>
</FormButtonGroup.FormItem>
)
}
if (type.value === 'collapsible') {
return (
<FragmentComponent>
<FormButtonGroup>
<a
href=""
onClick={(e) => {
e.preventDefault()
toggle()
}}
>
{expanded.value ? '收起' : '展开'}
</a>
</FormButtonGroup>
<FormButtonGroup align="right">{renderActions()}</FormButtonGroup>
</FragmentComponent>
)
}
return (
<FormButtonGroup
align="right"
style={{ display: 'flex', width: '100%' }}
>
{renderActions()}
</FormButtonGroup>
)
}
return () => {
return (
<Form {...props} layout="vertical" feedbackLayout="terse">
<FormGrid grid={grid}>
{slots.default()}
<FormGrid.GridColumn
gridSpan={-1}
style={{ display: 'flex', justifyContent: 'space-between' }}
>
{renderButtonGroup()}
</FormGrid.GridColumn>
</FormGrid>
</Form>
)
}
},
})
)
const form = createForm()
const fields = createSchemaField({
components: {
QueryForm,
Input,
Select,
DatePicker,
FormItem,
},
})
export default {
components: { FormProvider, ...fields, Submit },
data() {
return {
form,
}
},
methods: {
onSubmit(value) {
console.log(value)
},
},
}
</script>
53 changes: 42 additions & 11 deletions packages/element/docs/guide/form-grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@

<dumi-previewer demoPath="guide/form-grid/native" />

## 查询表单实现案例

<dumi-previewer demoPath="guide/form-grid/form" />

## API

### FormGrid

| 属性名 | 类型 | 描述 | 默认值 |
| ------------- | ------------------- | -------------------------------------------------------------- | ----------------- |
| minWidth | `number / number[]` | 元素最小宽度 | 100 |
| maxWidth | `number / number[]` | 元素最大宽度 | - |
| minColumns | `number / number[]` | 最小列数 | 0 |
| maxColumns | `number / number[]` | 最大列数 | - |
| breakpoints | number[] | 容器尺寸断点 | `[720,1280,1920]` |
| columnGap | number | 列间距 | 8 |
| rowGap | number | 行间距 | 4 |
| colWrap | boolean | 自动换行 | true |
| strictAutoFit | boolean | GridItem 宽度是否严格受限于 maxWidth,不受限的话会自动占满容器 | false |
| 属性名 | 类型 | 描述 | 默认值 |
| ------------- | ---------------------- | -------------------------------------------------------------- | ----------------- |
| minWidth | `number / number[]` | 元素最小宽度 | 100 |
| maxWidth | `number / number[]` | 元素最大宽度 | - |
| minColumns | `number / number[]` | 最小列数 | 0 |
| maxColumns | `number / number[]` | 最大列数 | - |
| breakpoints | number[] | 容器尺寸断点 | `[720,1280,1920]` |
| columnGap | number | 列间距 | 8 |
| rowGap | number | 行间距 | 4 |
| colWrap | boolean | 自动换行 | true |
| strictAutoFit | boolean | GridItem 宽度是否严格受限于 maxWidth,不受限的话会自动占满容器 | false |
| shouldVisible | `(node,grid)=>boolean` | 是否需要显示当前节点 | `()=>true` |
| grid | `Grid` | 外部传入 Grid 实例,用于实现更复杂的布局逻辑 | - |

注意:

Expand All @@ -41,3 +47,28 @@
| 属性名 | 类型 | 描述 | 默认值 |
| -------- | ------ | ---------------------------------------------------- | ------ |
| gridSpan | number | 元素所跨列数,如果为-1,那么会自动反向跨列填补单元格 | 1 |

### FormGrid.createFormGrid

从上下文中读取 Grid 实例

```ts
interface createFormGrid {
(props: IGridProps): Grid
}
```

- IGridProps 参考 FormGrid 属性
- Grid 实例属性方法参考 https://github.com/alibaba/formily/tree/formily_next/packages/grid

### FormGrid.useFormGrid

从上下文中读取 Grid 实例

```ts
interface useFormGrid {
(): Grid
}
```

- Grid 实例属性方法参考 https://github.com/alibaba/formily/tree/formily_next/packages/grid
9 changes: 9 additions & 0 deletions packages/element/docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
- FormStep,用户只需要关注 FormStep Reactive Model 即可,通过 createFormStep 就可以创建出 Reactive Model,传给 FormStep 组件即可快速通讯。同理,FormTab/FormCollapse 也是一样的通讯模式
- 弹窗表单,抽屉表单,想必过去,用户几乎每次都得在这两个场景上写大量的代码,这次直接提供了极其简易的 API 让用户使用,最大化提升开发效率

## 注意

因为 Element UI 是基于 Sass 构建的,如果你用 Webpack 配置请使用以下两个 Sass 工具

```
"sass": "^1.32.11",
"sass-loader": "^8.0.2"
```

## 安装

```bash
Expand Down
2 changes: 1 addition & 1 deletion packages/element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@vuepress/plugin-medium-zoom": "^1.8.2",
"codesandbox": "^2.2.3",
"core-js": "^2.4.0",
"element-ui": "latest",
"element-ui": "^2.15.7",
"sass": "^1.34.1",
"ts-import-plugin": "^1.6.7",
"ttypescript": "^1.5.12",
Expand Down
Loading

0 comments on commit cadd63b

Please sign in to comment.