babel-plugin-jsx/README.md

212 lines
3.2 KiB
Markdown
Raw Normal View History

# Babel Plugin JSX for Vue 3.0
2020-05-13 19:14:28 +08:00
2020-07-12 18:56:19 +08:00
![test](https://github.com/vueComponent/jsx/workflows/test/badge.svg)[![npm package](https://img.shields.io/npm/v/@ant-design-vue/babel-plugin-jsx.svg?style=flat-square)](https://www.npmjs.com/package/@ant-design-vue/babel-plugin-jsx)
2020-05-13 19:14:28 +08:00
To add Vue JSX support.
2020-07-02 13:30:17 +08:00
English | [简体中文](./README-zh_CN.md)
2020-05-24 10:29:41 +08:00
## Installation
Install the plugin with:
2020-05-24 10:29:41 +08:00
```
2020-05-27 00:12:34 +08:00
npm install @ant-design-vue/babel-plugin-jsx -D
2020-05-24 10:29:41 +08:00
```
Then add the plugin to .babelrc:
2020-05-24 10:29:41 +08:00
```
{
2020-07-02 13:30:17 +08:00
"plugins": ["@ant-design-vue/babel-plugin-jsx"]
2020-05-24 10:29:41 +08:00
}
```
2020-05-27 13:05:24 +08:00
## Usage
2020-05-13 19:14:28 +08:00
2020-05-27 13:05:24 +08:00
### options
2020-05-13 19:14:28 +08:00
2020-05-27 13:05:24 +08:00
* transformOn
2020-05-18 00:20:10 +08:00
2020-06-04 19:45:40 +08:00
transform `on: { click: xx }` to `onClick: xxx`
2020-05-27 13:05:24 +08:00
* compatibleProps
2020-05-23 16:58:37 +08:00
2020-06-04 19:45:40 +08:00
compatible with Vue 2.x
2020-05-23 16:58:37 +08:00
2020-07-02 13:30:17 +08:00
`{ props, on = {}, attrs, ...rest }` will be transformed to `{ ...props, ...attrs, ...transformOn(on), ...rest }`
2020-07-03 00:51:53 +08:00
## Syntax
### Content
functional component
```jsx
const App = () => <div></div>
```
with render
```jsx
const App = {
render() {
return <div>Vue 3.0</div>
}
}
```
```jsx
const App = defineComponent(() => {
const count = ref(0);
const inc = () => {
count.value++;
};
return () => (
<div onClick={inc}>
{count.value}
</div>
)
})
```
Fragment
```jsx
const App = () => (
<>
<span>I'm</span>
<span>Fragment</span>
</>
)
```
### Attributes/Props
```jsx
const App = () => <input type="email" />
```
with a dynamic binding:
```jsx
const placeholderText = 'email'
const App = () => (
<input
type="email"
placeholder={placeholderText}
/>
)
```
### Directives
> It is recommended to use camelCase version of it (`vModel`) in JSX, but you can use kebab-case too (`v-model`).
v-show
```jsx
const App = {
data() {
return { visible: true };
},
render() {
return <input vShow={this.visible} />;
},
};
```
v-model
2020-07-15 09:22:27 +08:00
> Note: You should pass the second param as string for using `arg`.
2020-07-03 00:51:53 +08:00
```jsx
2020-07-15 09:22:27 +08:00
<input vModel={val} />
2020-07-03 00:51:53 +08:00
```
2020-07-13 23:44:52 +08:00
```jsx
<input vModel={[val, ['trim']]} />
```
```jsx
<A vModel={[val, 'foo', ['bar']]} />
```
Will compile to:
```js
h(A, {
'foo': val,
"fooModifiers": {
"bar": true
},
"onUpdate:foo": $event => val = $event
})
```
2020-07-03 00:51:53 +08:00
custom directive
```jsx
const App = {
directives: { custom: customDirective },
setup() {
return () => (
<a
2020-07-13 23:44:52 +08:00
vCustom={[val, 'arg', ['a', 'b']]}
2020-07-03 00:51:53 +08:00
/>
);
},
}
```
### Slot
2020-07-11 23:14:18 +08:00
```jsx
const App = {
setup() {
const slots = {
a: () => <div>A</div>,
b: () => <span>B</span>
}
return () => <A vSlots={slots} />
}
}
```
2020-07-03 00:51:53 +08:00
2020-07-09 21:16:10 +08:00
## Who is using
<table>
<tbody>
<tr>
<td align="center">
<a target="_blank" href="https://www.antdv.com/">
<img
width="32"
src="https://qn.antdv.com/logo.png"
/>
<br>
<strong>Ant Design Vue</strong>
</a>
</td>
<td align="center">
2020-07-09 21:36:13 +08:00
<a target="_blank" href="https://youzan.github.io/vant/#/zh-CN/">
2020-07-09 21:16:10 +08:00
<img
width="32"
style="vertical-align: -0.32em; margin-right: 8px;"
src="https://img.yzcdn.cn/vant/logo.png"
/>
<br>
<strong>Vant</strong>
</a>
</td>
</tr>
</tbody>
</table>
2020-07-03 00:51:53 +08:00
2020-05-18 00:20:10 +08:00
## Compatibility
This repo is only compatible with:
- **Babel 7+**
- **Vue 3+**