Skip to content

Commit

Permalink
fix(okam-core): fix ant custom component event emit
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhy committed Nov 9, 2018
1 parent c061f88 commit fc53d2f
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/okam-core/src/Component.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @file Create component instance
* @file Create swan component instance
* @author [email protected]
*/

'use strict';

import {createComponent} from './helper/factory';
import {normalizeComponent} from './helper/component';
import {normalizeEventArgs} from './helper/triggerEvent';
import {normalizeEventArgs} from './swan/triggerEvent';
import componentBase from './base/component';

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/okam-core/src/ant/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
'use strict';

import {createComponent} from '../helper/factory';
import {normalizeEventArgs} from './helper/triggerEvent';
import {normalizeComponent} from './helper/component';
import componentBase from './base/component';

/**
* Fix ant component event args
*
* @param {Array} args the event args
* @return {Array}
*/
componentBase.methods.__beforeEmit = function (args) {
return normalizeEventArgs(this, args);
};

export default function extendComponent(componentInfo, refComponents) {
return createComponent(
componentInfo,
Expand Down
15 changes: 15 additions & 0 deletions packages/okam-core/src/ant/base/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

import componentBase from '../../base/component';

/**
* Emit custom component event
*
* @param {...any} args the event arguments
*/
componentBase.methods.$emit = function (...args) {
this.__beforeEmit && this.__beforeEmit(args);
this.$listener.emit.apply(this.$listener, args);

let eventProp = args[0];
eventProp = 'on' + eventProp.charAt(0).toUpperCase() + eventProp.substr(1);
let eventHandler = this.props[eventProp];
eventHandler.call(this, args[1]);
};

export default Object.assign(componentBase, {
didMount() {
this.created();
Expand Down
19 changes: 18 additions & 1 deletion packages/okam-core/src/ant/helper/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

'use strict';

import {normalizeProps} from '../../helper/props';
import {normalizeMethods} from '../../helper/methods';

/**
Expand All @@ -18,6 +17,24 @@ const EXTRA_NORMALIZE_METHODS = [
'created', 'attached', 'ready', 'detached'
];

/**
* Normalize component props data using mini program syntax
*
* @param {Object} props the props data to normalize
* @return {?Object}
*/
function normalizeProps(props) {
Object.keys(props).forEach(k => {
let propValue = props[k];
let defaultValue = null;
if (propValue && propValue.default !== undefined) {
defaultValue = propValue.default;
}
props[k] = defaultValue;
});
return props;
}

/**
* Normalize the component or behavior attribute names to native
*
Expand Down
56 changes: 56 additions & 0 deletions packages/okam-core/src/ant/helper/triggerEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @file Component triggerEvent helper
* @author [email protected]
*/

'use strict';

/**
* Normalize event arguments to fix the native swan framework bug
*
* @param {Component} component current component instance
* @param {Object} args the event args to normalize
* @return {Object}
*/
export function normalizeEventArgs(component, args) {
let eventData = args[1] || {};
if (eventData.currentTarget && eventData.target) {
return args;
}

let propData = component.props;
let dataset = {};
propData && Object.keys(propData).forEach(k => {
if (/^data\-.+$/.test(k)) {
let first = true;
let dataKey = k.replace(
/\-(\w)/g,
(match, char) => {
if (first) {
first = false;
return char.toLowerCase();
}
return char.toUpperCase();
}
);
dataKey = dataKey.substr('data'.length);
dataset[dataKey] = propData[k];
}
});

let eventObj = {
type: args[0],
currentTarget: {
dataset,
id: component.id
},
target: {
dataset,
id: component.id
},
detail: eventData
};
args[1] = eventObj;

return args;
}
2 changes: 1 addition & 1 deletion packages/okam-core/src/base/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default {
/**
* Emit custom component event
*
* @param {...any} args the event arguments
* @param {...any} args the event arguments
*/
$emit(...args) {
this.__beforeEmit && this.__beforeEmit(args);
Expand Down
20 changes: 18 additions & 2 deletions packages/okam-core/src/helper/component.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
/**
* @file Component helper
* @file Component normalizer helper
* @author [email protected]
*/

'use strict';

import {normalizeProps} from './props';
import {normalizeMethods} from './methods';

/**
* Normalize component props data using mini program syntax
*
* @param {Object} props the props data to normalize
* @return {?Object}
*/
function normalizeProps(props) {
Object.keys(props).forEach(k => {
let propValue = props[k];
if (propValue && propValue.default !== undefined) {
propValue.value = propValue.default;
delete propValue.default;
}
});
return props;
}

/**
* Normalize the component or behavior attribute names to native
*
Expand Down
20 changes: 1 addition & 19 deletions packages/okam-core/src/helper/props.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file Component props helper
* @file OKAM component props helper
* @author [email protected]
*/

Expand Down Expand Up @@ -69,21 +69,3 @@ export function normalizeOkamProps(props) {
});
return result;
}

/**
* Normalize component props data using mini program syntax
*
* @param {Object} props the props data to normalize
* @return {?Object}
*/
export function normalizeProps(props) {
Object.keys(props).forEach(k => {
let propValue = props[k];
if (propValue && propValue.default !== undefined) {
propValue.value = propValue.default;
delete propValue.default;
}
});
return props;
}

2 changes: 1 addition & 1 deletion packages/okam-core/src/swan/adapter/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import {normalizeEventArgs} from '../../helper/triggerEvent';
import {normalizeEventArgs} from '../triggerEvent';

/**
* Emit custom component event
Expand Down
File renamed without changes.

0 comments on commit fc53d2f

Please sign in to comment.