Skip to content

Commit ac3783d

Browse files
authored
feat(vue): add vue3 compatibly (#1138)
* feat(vue): add vue3 compatibly * feat: add .yarnrc
1 parent a6748df commit ac3783d

34 files changed

+867
-849
lines changed

.npmrc

-1
This file was deleted.

.yarnrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry "https://registry.yarnpkg.com"

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
"@typescript-eslint/eslint-plugin": "^4.9.1",
5858
"@typescript-eslint/parser": "^4.8.2",
5959
"@umijs/plugin-sass": "^1.1.1",
60-
"@vue/compiler-sfc": "^3.0.5",
61-
"@vue/composition-api": "^1.0.0-beta.23",
60+
"@vue/composition-api": "^1.0.0-rc.1",
6261
"@vuedx/typescript-plugin-vue": "^0.4.1",
6362
"antd": "^4.0.0",
6463
"chalk": "^2.4.2",
@@ -114,7 +113,6 @@
114113
"rollup-plugin-node-resolve": "^5.2.0",
115114
"rollup-plugin-typescript2": "^0.29.0",
116115
"rollup-plugin-uglify": "^6.0.4",
117-
"rollup-plugin-vue": "^6.0.0",
118116
"semver-regex": "^2.0.0",
119117
"staged-git-files": "^1.1.2",
120118
"style-loader": "^1.1.3",
@@ -125,6 +123,7 @@
125123
"ts-node": "^9.1.1",
126124
"typescript": "^4.1.2",
127125
"vue": "^2.6.12",
126+
"vue-demi": "latest",
128127
"vue-eslint-parser": "^7.1.1",
129128
"vue-template-compiler": "^2.6.12",
130129
"webpack": "^4.41.5",

packages/vue/README.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
## Usage
22

3-
### Preparation
3+
### Requirement
44

5-
```js
6-
import VueCompositionAPI from '@vue/composition-api'
5+
vue^2.6.0 + @vue/composition-api^1.0.0-beta.1
76

8-
Vue.use(VueCompositionAPI)
9-
```
7+
Or
8+
9+
vue>=3.0.0-rc.0
1010

1111
### Quick Start
1212

@@ -41,7 +41,6 @@ Vue.use(VueCompositionAPI)
4141
>
4242
<template #default="{ field }">
4343
<div>
44-
{{ log(field) }}
4544
<div v-for="(item, index) in field.value || []" :key="item.id">
4645
<Field :name="`${index}.dd`" required :component="[Input]" />
4746
<Field :name="`${index}.ee`" :component="[Input]" />
@@ -112,7 +111,7 @@ Vue.use(VueCompositionAPI)
112111
</Field>
113112
<FormConsumer>
114113
<template #default="{ form }">
115-
{{ JSON.stringify(form.query('aa').get(.get("value")) }}
114+
{{ JSON.stringify(form.query('aa').get().value) }}
116115
</template>
117116
</FormConsumer>
118117
<FormConsumer>
@@ -144,7 +143,7 @@ Vue.use(VueCompositionAPI)
144143
</FormProvider>
145144
</template>
146145

147-
<script lang="ts">
146+
<script>
148147
import { Form, Input, Select, Card, Button } from 'ant-design-vue'
149148
import {
150149
createForm,
@@ -157,9 +156,10 @@ Vue.use(VueCompositionAPI)
157156
connect,
158157
mapProps,
159158
isVoidField,
160-
} from '../src/index'
159+
} from '@formily/vue'
161160
162161
const FormItem = connect(
162+
Form.Item,
163163
mapProps(
164164
{ extract: 'validateStatus' },
165165
{ extract: 'title', to: 'label' },
@@ -179,9 +179,10 @@ Vue.use(VueCompositionAPI)
179179
}
180180
}
181181
)
182-
)(Form.Item)
182+
)
183183
184184
export default {
185+
name: 'demo',
185186
components: {
186187
FormProvider,
187188
FormConsumer,
@@ -222,8 +223,8 @@ Vue.use(VueCompositionAPI)
222223
}
223224
},
224225
methods: {
225-
log(v) {
226-
console.log(v)
226+
log(...v) {
227+
console.log(...v)
227228
},
228229
},
229230
}

packages/vue/package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
"npm": ">=3.0.0"
1414
},
1515
"scripts": {
16-
"build": "rimraf -rf lib esm dist && rollup --config"
16+
"build": "rimraf -rf lib esm dist && npm run build:cjs && npm run build:esm && npm run build:umd",
17+
"build:cjs": "tsc --declaration",
18+
"build:esm": "tsc --declaration --module es2015 --outDir esm",
19+
"build:umd": "rollup --config"
1720
},
1821
"dependencies": {
1922
"@formily/core": "^2.0.0-beta.3",
2023
"@formily/shared": "^2.0.0-beta.3",
21-
"@formily/validator": "^2.0.0-beta.3"
24+
"@formily/validator": "^2.0.0-beta.3",
25+
"vue-demi": "latest"
2226
},
2327
"peerDependencies": {
24-
"@vue/composition-api": "^1.0.0-beta.23",
28+
"@vue/composition-api": "^1.0.0-beta.1",
2529
"mobx": "^6.0.3",
26-
"vue": ">=2.6.0"
30+
"vue": "^2.6.0 || >=3.0.0-rc.0"
2731
}
2832
}

packages/vue/rollup.config.js

+1-53
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,3 @@
1-
import vue from 'rollup-plugin-vue'
2-
import typescript from 'rollup-plugin-typescript2'
3-
import resolve from 'rollup-plugin-node-resolve'
4-
import commonjs from '@rollup/plugin-commonjs'
51
import baseConfig from '../../scripts/rollup.base'
6-
import path from 'path'
72

8-
export default [
9-
...baseConfig('formily.vue', 'Formily.Vue'),
10-
{
11-
input: 'src/index.ts',
12-
output: {
13-
format: 'esm',
14-
file: 'esm/index.js',
15-
name: 'Formily.Vue',
16-
},
17-
external: ['vue', '@vue/composition-api', 'mobx', '@formily/core'],
18-
plugins: [
19-
typescript({
20-
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
21-
tsconfigOverride: {
22-
compilerOptions: {
23-
module: 'ESNext',
24-
},
25-
},
26-
}),
27-
vue(),
28-
resolve(),
29-
commonjs(),
30-
],
31-
},
32-
// Browser build.
33-
{
34-
input: 'src/index.ts',
35-
output: {
36-
format: 'cjs',
37-
file: 'lib/index.js',
38-
name: 'Formily.Vue',
39-
},
40-
external: ['vue', '@vue/composition-api', 'mobx', '@formily/core'],
41-
plugins: [
42-
typescript({
43-
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
44-
tsconfigOverride: {
45-
compilerOptions: {
46-
module: 'ESNext',
47-
},
48-
},
49-
}),
50-
vue(),
51-
resolve(),
52-
commonjs(),
53-
],
54-
},
55-
]
3+
export default baseConfig('formily.vue', 'Formily.Vue')
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
<template>
2-
<ReactiveField :field="field">
3-
<slot :field="field" :form="form"></slot>
4-
</ReactiveField>
5-
</template>
6-
7-
<script lang="ts">
8-
import { provide } from '@vue/composition-api'
1+
import { provide } from 'vue-demi'
92
import { useField, useForm } from '../hooks'
103
import { useAttach } from '../hooks/useAttach'
11-
import { FieldSymbol } from '../shared/context'
124
import { VueComponent, IFieldProps } from '../types'
13-
import ReactiveField from './ReactiveField.vue'
5+
import ReactiveField from './ReactiveField'
146
import { defineObservableComponent } from '../utils/define-observable-component'
7+
import { FieldSymbol } from '../shared/context'
8+
import h from '../utils/compatible-create-element'
9+
import { getRowComponentFromProps } from '../utils/get-row-component-from-props'
1510

1611
export default defineObservableComponent({
1712
name: 'ArrayField',
@@ -32,27 +27,43 @@ export default defineObservableComponent({
3227
pattern: String,
3328
validateFirst: Boolean,
3429
validator: {},
35-
reactions: Array
30+
reactions: Array,
3631
},
3732
observableSetup<D extends VueComponent, C extends VueComponent>(
3833
collect,
39-
props: IFieldProps<D, C>
34+
props: IFieldProps<D, C>,
35+
{ slots }
4036
) {
4137
const form = useForm()
4238
const parent = useField()
4339
const basePath = props.basePath ? props.basePath : parent?.address
4440
const field = useAttach(
4541
form.createArrayField({
4642
...props,
47-
basePath
43+
basePath,
44+
...getRowComponentFromProps(props)
4845
})
4946
)
5047
provide(FieldSymbol, field)
5148

52-
return collect({
49+
collect({
5350
field,
5451
form: field.form
5552
})
53+
54+
return () => h(
55+
ReactiveField,
56+
{
57+
props: {
58+
field
59+
}
60+
},
61+
{
62+
default: () => slots.default && slots.default({
63+
field,
64+
form: field.form
65+
})
66+
}
67+
)
5668
}
57-
})
58-
</script>
69+
})
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
<template>
2-
<ReactiveField :field="field">
3-
<slot :field="field" :form="field.form"></slot>
4-
</ReactiveField>
5-
</template>
6-
7-
<script lang="ts">
8-
import { provide } from '@vue/composition-api'
1+
import { provide } from 'vue-demi'
92
import { useField, useForm } from '../hooks'
103
import { useAttach } from '../hooks/useAttach'
114
import { FieldSymbol } from '../shared/context'
125
import { VueComponent, IFieldProps } from '../types'
13-
import ReactiveField from './ReactiveField.vue'
6+
import ReactiveField from './ReactiveField'
147
import { defineObservableComponent } from '../utils/define-observable-component'
8+
import h from '../utils/compatible-create-element'
9+
import { getRowComponentFromProps } from '../utils/get-row-component-from-props'
1510

1611
export default defineObservableComponent({
1712
name: 'Field',
@@ -32,26 +27,43 @@ export default defineObservableComponent({
3227
pattern: String,
3328
validateFirst: Boolean,
3429
validator: {},
35-
reactions: Array
30+
reactions: Array,
3631
},
3732
observableSetup<D extends VueComponent, C extends VueComponent>(
3833
collect,
39-
props: IFieldProps<D, C>
34+
props: IFieldProps<D, C>,
35+
{ slots }
4036
) {
4137
const form = useForm()
4238
const parent = useField()
4339
const basePath = props.basePath ? props.basePath : parent?.address
4440
const field = useAttach(
4541
form.createField({
4642
...props,
47-
basePath
43+
basePath,
44+
...getRowComponentFromProps(props)
4845
})
4946
)
5047
provide(FieldSymbol, field)
5148

52-
return collect({
53-
field
49+
collect({
50+
field,
51+
form: field.form
5452
})
53+
54+
return () => h(
55+
ReactiveField,
56+
{
57+
props: {
58+
field
59+
}
60+
},
61+
{
62+
default: () => slots.default && slots.default({
63+
field,
64+
form: field.form
65+
})
66+
}
67+
)
5568
}
56-
})
57-
</script>
69+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { defineObservableComponent } from '../utils/define-observable-component'
2+
import { useForm } from '../hooks'
3+
import h from '../utils/compatible-create-element'
4+
5+
export default defineObservableComponent({
6+
name: 'FormConsumer',
7+
observableSetup(collect, props, { attrs, slots }) {
8+
const form = useForm()
9+
collect({
10+
form
11+
})
12+
return () => h(
13+
'div',
14+
{ attrs },
15+
{
16+
default: () => slots.default && slots.default({
17+
form
18+
})
19+
}
20+
)
21+
}
22+
})

packages/vue/src/components/FormConsumer.vue

-20
This file was deleted.

0 commit comments

Comments
 (0)