UNPKG

antd-mini

Version:

antd-mini 是支付宝小程序 UI 组件库,遵循 Ant Design 规范。

147 lines (146 loc) 5.2 kB
import { __assign, __spreadArray } from "tslib"; import fmtEvent from './fmtEvent'; function removeNullProps(props) { var newProps = {}; for (var key in props) { if (props[key] !== null) { newProps[key] = props[key]; } } return newProps; } function buildProperties(props) { var newProperties = {}; for (var key in props) { var type = null; switch (typeof props[key]) { case 'string': type = String; break; case 'number': type = Number; break; case 'boolean': type = Boolean; break; } newProperties[key] = { type: type, value: props[key], }; } return newProperties; } function mergeDefaultProps(defaultProps) { if (defaultProps === void 0) { defaultProps = {}; } return __assign({}, defaultProps); } export var ComponentWithSignalStoreImpl = function (storeOptions, defaultProps, methods, data, mixins, instanceMethods) { var storeBinder = new StoreBinder(storeOptions); var defaultOnInit = function () { storeBinder.init(this); }; var instanceMethodsCopy = __assign({}, instanceMethods); // 确保 instanceMethods 存在 // 备份原有的 onInit 和 didUnmount 方法 var onInitBackup = instanceMethodsCopy.onInit || (function () { }); var onDidUnmountBackup = instanceMethodsCopy.didUnmount || (function () { }); instanceMethodsCopy.onInit = function () { defaultOnInit.call(this); if (onInitBackup) { onInitBackup.call(this); } }; instanceMethodsCopy.didUnmount = function () { if (onDidUnmountBackup) { onDidUnmountBackup.call(this); } storeBinder.dispose(); }; // 这里确保 instanceMethodsCopy.onInit 被正确执行 if (!instanceMethodsCopy.onInit) { instanceMethodsCopy.onInit = defaultOnInit; } Component(__assign({ props: removeNullProps(mergeDefaultProps(defaultProps)), methods: methods, mixins: mixins || [], data: data }, (instanceMethodsCopy || {}))); }; var StoreBinder = /** @class */ (function () { function StoreBinder(storeOptions) { this.storeOptions = storeOptions; this.disposeStore = undefined; } /** * 绑定和 store 的关系 */ StoreBinder.prototype.init = function (theThis) { var _this = this; var store = this.storeOptions.store(); var disposes = Object.keys(this.storeOptions.mapState).map(function (key) { return _this.storeOptions.updateHook(function () { var _a; theThis.setData((_a = {}, _a[key] = _this.storeOptions.mapState[key]({ store: store }), _a)); }); }); theThis.$store = store; this.disposeStore = function () { return disposes.forEach(function (d) { return d(); }); }; }; /** * 释放和 store 的关系 */ StoreBinder.prototype.dispose = function () { if (this.disposeStore) { this.disposeStore(); } }; return StoreBinder; }()); export { StoreBinder }; function ComponentImpl(defaultProps, methods, data, mixins, instanceMethods) { Component(__assign({ props: removeNullProps(mergeDefaultProps(defaultProps)), methods: methods, mixins: mixins || [], data: data }, instanceMethods)); } export function triggerEvent(instance, eventName, value, e) { // 首字母大写,然后加上 on var alipayCallbackName = 'on' + eventName.charAt(0).toUpperCase() + eventName.slice(1); var props = instance.props; if (props[alipayCallbackName]) { props[alipayCallbackName](value, fmtEvent(props, e)); } } export function triggerEventOnly(instance, eventName, e) { // 首字母大写,然后加上 on var alipayCallbackName = 'on' + eventName.charAt(0).toUpperCase() + eventName.slice(1); var props = instance.props; if (props[alipayCallbackName]) { props[alipayCallbackName](fmtEvent(props, e)); } } export function triggerEventValues(instance, eventName, values, e) { // 首字母大写,然后加上 on var alipayCallbackName = 'on' + eventName.charAt(0).toUpperCase() + eventName.slice(1); var props = instance.props; if (props[alipayCallbackName]) { props[alipayCallbackName].apply(props, __spreadArray(__spreadArray([], values, false), [fmtEvent(props, e)], false)); } } export function triggerCatchEvent(instance, eventName, e) { var props = instance.props; if (props[eventName]) { props[eventName](fmtEvent(props, e)); } } export function getValueFromProps(instance, propName) { var value; var props = instance.props; if (!propName) { return props; } if (typeof propName === 'string') { value = props[propName]; } if (Array.isArray(propName)) { value = propName.map(function (name) { return props[name]; }); } return value; } export { ComponentWithSignalStoreImpl as ComponentWithSignalStore, ComponentImpl as Component, };