Skip to content

Commit f24f3b5

Browse files
authored
Merge pull request #5 from peakchen90/dev
Dev
2 parents d35c3ec + a85e9f3 commit f24f3b5

File tree

9 files changed

+49
-32
lines changed

9 files changed

+49
-32
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
## v1.1.0
44
2019-12-01
55

6-
- **Feature**: Added `x-class` directive for conditionally joining classNames together.
7-
- **Optimization**: Split the runtime into multiple independent files to reduce runtime bundle size.
6+
- **Feature**: Added `x-class` directive for conditionally joining classNames together
7+
- **Optimization**: Split the runtime into multiple independent files to reduce runtime bundle size
88

99
## v1.0.2
1010
2019-11-23
1111

12-
- **Bug**: fix bug that when `x-if` is nested under `x-else` and `x-else-if`, an error is reported.
12+
- **Bug**: fix bug that when `x-if` is nested under `x-else` and `x-else-if`, an error is reported
1313

1414
## v1.0.0
1515
2019-10-20
1616

17-
- **Feature**: Added `x-model-hook` directive for **useState hook function**, and the `x-model` can only be used in **class component** now
17+
- **Feature**: Added `x-model-hook` directive for function component with *useState hook*, and the `x-model` can only be used in *class component* now
1818
- **Optimization**: In some cases use the runtime instead of the AST code
1919
- **Optimization**: Fix some problem of `x-model`
2020
- **Optimization**: Improved stability

README.ZH-CN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- [开始使用](#toc-usage)
1414
- [安装](#toc-installation)
1515
- [添加配置 `.babelrc`](#toc-configuring)
16-
- [或使用插件options](#toc-or-use-options)
16+
- [插件options](#toc-plugin-options)
1717
- [指令](#toc-directives)
1818
- [x-if](#toc-directives-x-if)
1919
- [x-else-if 和 x-else](#toc-directives-x-else-if-and-x-else)
@@ -51,7 +51,7 @@ yarn add --dev babel-plugin-react-directives
5151
}
5252
```
5353

54-
### <span id="toc-or-use-options">或使用插件options (<a target="_blank" href="https://babeljs.io/docs/en/plugins#plugin-options">配置说明</a>)</span>
54+
### <span id="toc-plugin-options">插件options</span>
5555

5656
```json
5757
{
@@ -226,7 +226,7 @@ const foo = (
226226

227227
### <span id="toc-directives-x-model">x-model</span>
228228
`x-model` 是类似于 vue `v-model` 的语法糖,使用时绑定一个值到表单元素的 `value` prop 上,在表单元素更新时自动更新状态。
229-
通过调用 [resolveValue 方法](./runtime/resolve-value.js) 获取更新的值(如果第一个参数 `arg` 不为空,且 `arg.target` 是一个对象,返回 `arg.target.value`,否则返回 `arg`
229+
通过调用 [resolveValue 方法](./runtime/resolve-value.js) 获取更新的值(如果第一个参数 `arg` 不为空,且 `arg.target` 是一个 HTMLElement, 返回 `arg.target.value`,否则返回 `arg`
230230

231231
**例子:**
232232
```jsx harmony
@@ -374,7 +374,7 @@ class Foo extends React.Component {
374374
```
375375

376376
### <span id="toc-directives-x-model-hook">x-model-hook</span>
377-
`x-model-hook``x-model` 非常相似,区别在于 `x-model-hook` 用于 **useState hook function**,而 `x-model` 用于 **class component**
377+
`x-model-hook``x-model` 非常相似,区别在于 `x-model-hook` 用于使用了 *useState hook* 的函数组件,而 `x-model` 用于 *class component*
378378

379379
**例子:**
380380
```jsx harmony

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ A babel plugin that provides some directives for react(any JSX), similar to dire
1414
- [Usage](#toc-usage)
1515
- [Installation](#toc-installation)
1616
- [Configuring via `.babelrc`](#toc-configuring)
17-
- [Or use options](#toc-or-use-options)
17+
- [Plugin options](#toc-plugin-options)
1818
- [Directives](#toc-directives)
1919
- [x-if](#toc-directives-x-if)
2020
- [x-else-if and x-else](#toc-directives-x-else-if-and-x-else)
@@ -52,7 +52,7 @@ yarn add --dev babel-plugin-react-directives
5252
}
5353
```
5454

55-
### <span id="toc-or-use-options">Or use options (<a target="_blank" href="https://babeljs.io/docs/en/plugins#plugin-options">Babel plugin options</a>)</span>
55+
### <span id="toc-plugin-options">Plugin options</span>
5656

5757
```json
5858
{
@@ -68,7 +68,7 @@ yarn add --dev babel-plugin-react-directives
6868
}
6969
```
7070

71-
- `prefix`: JSX props prefix for directives. Default: "x", usage example: `x-if`
71+
- `prefix`: JSX props prefix for directives. Default: "x", example usage: `x-if`
7272
- `pragmaType`: Help internal to correctly identify some syntax, such as hooks. Default: "React"
7373

7474
## <span id="toc-directives">Directives</span>
@@ -120,7 +120,7 @@ const foo = (
120120
```
121121

122122
### <span id="toc-directives-x-show">x-show</span>
123-
The `x-show` controls the display or hiding of elements by the `display` of the `style` prop. If the `x-show` value is **falsy**, will set `style.display = "none"`, otherwise do nothing.
123+
The `x-show` controls the display or hiding of elements through the `display` of the `style` prop. If the `x-show` value is **falsy**, will set `style.display = "none"`, otherwise do nothing.
124124

125125

126126
**Example:**
@@ -229,7 +229,7 @@ const foo = (
229229

230230
### <span id="toc-directives-x-model">x-model</span>
231231
The `x-model` is a syntax sugar similar to vue `v-model`, which binds a state to the `value` prop of the **form element** and automatically updates the state when the element is updated.
232-
It resolves the updated value by calling the [resolveValue method](./runtime/resolve-value.js) (If the first argument `arg` is non-empty, and `arg.target` is an object, return `arg.target.value`, otherwise return `arg`).
232+
It resolves the updated value by calling the [resolveValue method](./runtime/resolve-value.js) (If the first argument `arg` is non-null, and `arg.target` is a HTMLElement, return `arg.target.value`, otherwise return `arg`).
233233

234234
**Example:**
235235
```jsx harmony
@@ -377,7 +377,7 @@ class Foo extends React.Component {
377377
```
378378

379379
### <span id="toc-directives-x-model-hook">x-model-hook</span>
380-
The `x-model-hook` is similar to the `x-model`, the difference is that the `x-model-hook` is used in the **useState hook function**, and the `x-model` is used in the **class component**.
380+
The `x-model-hook` is similar to the `x-model`, the difference is that the `x-model-hook` is used in the function component with *useState hook*, and the `x-model` is used in the *class component*.
381381

382382
**Example:**
383383
```jsx harmony
@@ -427,7 +427,6 @@ const foo = <div className={classNames({ abc: true, def: false })}>
427427
**Note**: `classNames` method references [runtime/classnames.js](./runtime/classnames.js).
428428

429429
Of course, it will also merge other `className` props, for example:
430-
**Example:**
431430
```jsx harmony
432431
const foo = <div x-class={{ abc: true, def: false }} className="xyz">
433432
```
@@ -438,7 +437,7 @@ const foo = <div className={classNames(["xyz", { abc: true, def: false }])}>
438437
// className="xyz abc"
439438
```
440439

441-
The `x-class` can also be used with [css-modules](https://github.com/css-modules/css-modules), for example:
440+
The `x-class` can also be used with [css-modules](https://github.com/css-modules/css-modules), the usage is as follows:
442441
```jsx harmony
443442
import styles from './style.css';
444443

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-react-directives",
3-
"version": "1.1.0-rc.1",
3+
"version": "1.1.0-rc.2",
44
"description": "A babel plugin that provides some directives for react(any JSX), similar to directive of vue.",
55
"main": "src/index.js",
66
"files": [

runtime/resolve-value.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
function resolveValue(args) {
2-
return args[0] && args[0].target && typeof args[0].target === 'object'
3-
? args[0].target.value
4-
: args[0];
2+
var target = args[0] && args[0].target;
3+
if(target && target.nodeType === 1) {
4+
return target.value;
5+
}
6+
return args[0];
57
}
68

79
module.exports = resolveValue;

test/e2e/specs/model.test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3+
import { createInputNode } from '../util';
34

45
class Demo1 extends React.Component {
56
constructor(props) {
@@ -62,30 +63,30 @@ function Demo4({ fn, extraProps }) {
6263
}
6364

6465
describe('directive: model', () => {
65-
test('in class', () => {
66+
test('use in class: x-model', () => {
6667
const wrapper = mount(<Demo1/>);
67-
wrapper.find('input').simulate('change', { target: { value: 'new value' } });
68+
wrapper.find('input').simulate('change', createInputNode('new value'));
6869
expect(wrapper.find('.text').text()).toBe('new value');
6970
expect(wrapper.find('.extra').text()).toBe('B');
7071
});
7172

72-
test('use hook', () => {
73+
test('use in hook: x-model-hook', () => {
7374
const wrapper = mount(<Demo2/>);
74-
wrapper.find('input').simulate('change', { target: { value: 'new value' } });
75+
wrapper.find('input').simulate('change', createInputNode('new value'));
7576
expect(wrapper.find('.text').text()).toBe('new value');
7677
expect(wrapper.find('.extra').text()).toBe('foo');
7778
});
7879

7980
test('custom onChange event', () => {
8081
const wrapper = mount(<Demo3/>);
81-
wrapper.find('input').simulate('change', { target: { value: 'custom value' } });
82+
wrapper.find('input').simulate('change', createInputNode('custom value'));
8283
expect(wrapper.find('.text').text()).toBe('custom value');
8384
});
8485

8586
test('merge onChange', () => {
8687
const fn = jest.fn();
8788
const wrapper = mount(<Demo4 fn={fn}/>);
88-
wrapper.find('input').simulate('change', { target: { value: 'custom value' } });
89+
wrapper.find('input').simulate('change', createInputNode('custom value'));
8990
expect(wrapper.find('.text').text()).toBe('custom value');
9091
expect(fn.mock.calls.length).toBe(1);
9192
expect(fn.mock.calls[0][0].target.value).toBe('custom value');
@@ -98,7 +99,7 @@ describe('directive: model', () => {
9899
onChange: fn2
99100
};
100101
const wrapper = mount(<Demo4 fn={fn} extraProps={extraProps}/>);
101-
wrapper.find('input').simulate('change', { target: { value: 'custom value' } });
102+
wrapper.find('input').simulate('change', createInputNode('custom value'));
102103
expect(wrapper.find('.text').text()).toBe('custom value');
103104
expect(fn.mock.calls.length).toBe(0);
104105
expect(fn2.mock.calls.length).toBe(1);

test/e2e/specs/todo.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3+
import { createInputNode } from '../util';
34

45
class Todo extends React.Component {
56
constructor(props) {
@@ -55,23 +56,23 @@ describe('Todo Component', () => {
5556
const wrapper = mount(<Todo/>);
5657

5758
test('add todo', () => {
58-
wrapper.find('.input').simulate('change', { target: { value: 'First Todo' } });
59+
wrapper.find('.input').simulate('change', createInputNode('First Todo'));
5960
wrapper.find('.add').simulate('click');
6061
expect(wrapper.find('.todo-item .todo-text').at(0).text()).toBe('First Todo');
6162
expect(wrapper.find('.clear').prop('style').display).toBe(undefined);
6263
});
6364

6465
test('search todo', () => {
65-
wrapper.find('.input').simulate('change', { target: { value: 'Second Todo' } });
66+
wrapper.find('.input').simulate('change', createInputNode('Second Todo'));
6667
wrapper.find('.add').simulate('click');
6768

68-
wrapper.find('.input').simulate('change', { target: { value: 'First' } });
69+
wrapper.find('.input').simulate('change', createInputNode('First'));
6970
expect(wrapper.find('.todo-item .todo-text').length).toBe(1);
7071
expect(wrapper.find('.todo-item .todo-text').at(0).text()).toBe('First Todo');
7172
});
7273

7374
test('clear todo', () => {
74-
wrapper.find('.input').simulate('change', { target: { value: '' } });
75+
wrapper.find('.input').simulate('change', createInputNode(''));
7576
expect(wrapper.find('.todo-item .todo-text').length).toBe(2);
7677

7778
wrapper.find('.todo-item .remove').at(0).simulate('click');

test/e2e/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ export function sleep(ms) {
88
setTimeout(resolve, ms);
99
});
1010
}
11+
12+
/**
13+
* 创建类Input Node节点对象
14+
* @param value
15+
*/
16+
export function createInputNode(value) {
17+
const node = {};
18+
node.target = {
19+
nodeName: 'INPUT',
20+
nodeType: 1,
21+
value
22+
};
23+
return node;
24+
}

0 commit comments

Comments
 (0)