diff --git a/packages/okam-core/src/Component.js b/packages/okam-core/src/Component.js index e03d1ffb..5c028373 100644 --- a/packages/okam-core/src/Component.js +++ b/packages/okam-core/src/Component.js @@ -1,5 +1,5 @@ /** - * @file Create component instance + * @file Create swan component instance * @author sparklewhy@gmail.com */ @@ -7,7 +7,7 @@ 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'; /** diff --git a/packages/okam-core/src/ant/Component.js b/packages/okam-core/src/ant/Component.js index e086c75c..357297f4 100644 --- a/packages/okam-core/src/ant/Component.js +++ b/packages/okam-core/src/ant/Component.js @@ -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, diff --git a/packages/okam-core/src/ant/base/component.js b/packages/okam-core/src/ant/base/component.js index 33254e90..6887ed8a 100644 --- a/packages/okam-core/src/ant/base/component.js +++ b/packages/okam-core/src/ant/base/component.js @@ -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(); diff --git a/packages/okam-core/src/ant/helper/component.js b/packages/okam-core/src/ant/helper/component.js index 1f9d9a48..623ab663 100644 --- a/packages/okam-core/src/ant/helper/component.js +++ b/packages/okam-core/src/ant/helper/component.js @@ -5,7 +5,6 @@ 'use strict'; -import {normalizeProps} from '../../helper/props'; import {normalizeMethods} from '../../helper/methods'; /** @@ -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 * diff --git a/packages/okam-core/src/ant/helper/triggerEvent.js b/packages/okam-core/src/ant/helper/triggerEvent.js new file mode 100644 index 00000000..20cc66f7 --- /dev/null +++ b/packages/okam-core/src/ant/helper/triggerEvent.js @@ -0,0 +1,56 @@ +/** + * @file Component triggerEvent helper + * @author sparklewhy@gmail.com + */ + +'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; +} diff --git a/packages/okam-core/src/base/component.js b/packages/okam-core/src/base/component.js index 87522652..266dd64d 100644 --- a/packages/okam-core/src/base/component.js +++ b/packages/okam-core/src/base/component.js @@ -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); diff --git a/packages/okam-core/src/helper/component.js b/packages/okam-core/src/helper/component.js index 75301b36..5db73c2d 100644 --- a/packages/okam-core/src/helper/component.js +++ b/packages/okam-core/src/helper/component.js @@ -1,13 +1,29 @@ /** - * @file Component helper + * @file Component normalizer helper * @author sparklewhy@gmail.com */ '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 * diff --git a/packages/okam-core/src/helper/props.js b/packages/okam-core/src/helper/props.js index 6d4f4ecd..02a820ab 100644 --- a/packages/okam-core/src/helper/props.js +++ b/packages/okam-core/src/helper/props.js @@ -1,5 +1,5 @@ /** - * @file Component props helper + * @file OKAM component props helper * @author sparklewhy@gmail.com */ @@ -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; -} - diff --git a/packages/okam-core/src/swan/adapter/component.js b/packages/okam-core/src/swan/adapter/component.js index a0ac9091..cea4eba1 100644 --- a/packages/okam-core/src/swan/adapter/component.js +++ b/packages/okam-core/src/swan/adapter/component.js @@ -5,7 +5,7 @@ 'use strict'; -import {normalizeEventArgs} from '../../helper/triggerEvent'; +import {normalizeEventArgs} from '../triggerEvent'; /** * Emit custom component event diff --git a/packages/okam-core/src/helper/triggerEvent.js b/packages/okam-core/src/swan/triggerEvent.js similarity index 100% rename from packages/okam-core/src/helper/triggerEvent.js rename to packages/okam-core/src/swan/triggerEvent.js