@@ -77,7 +77,6 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
77 | 77 | __compScopes: Record<string, any> = {}; |
78 | 78 | __instanceMap: Record<string, any> = {}; |
79 | 79 | __dataHelper: any; |
80 | | - __showPlaceholder: boolean = false; |
81 | 80 | __customMethodsList: any[] = []; |
82 | 81 | dataSourceMap: Record<string, any> = {}; |
83 | 82 | __ref: any; |
@@ -106,7 +105,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
106 | 105 | this.__compScopes = {}; |
107 | 106 | this.__instanceMap = {}; |
108 | 107 | this.__bindCustomMethods(props); |
109 | | - this.__initI18nAPIs(); |
| 108 | + this.__initI18nAPIs(props); |
110 | 109 | } |
111 | 110 |
|
112 | 111 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
@@ -159,23 +158,17 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
159 | 158 | reloadDataSource = () => new Promise((resolve, reject) => { |
160 | 159 | this.__debug('reload data source'); |
161 | 160 | if (!this.__dataHelper) { |
162 | | - this.__showPlaceholder = false; |
163 | 161 | return resolve({}); |
164 | 162 | } |
165 | 163 | this.__dataHelper.getInitData() |
166 | 164 | .then((res: any) => { |
167 | | - this.__showPlaceholder = false; |
168 | 165 | if (isEmpty(res)) { |
169 | 166 | this.forceUpdate(); |
170 | 167 | return resolve({}); |
171 | 168 | } |
172 | 169 | this.setState(res, resolve as () => void); |
173 | 170 | }) |
174 | 171 | .catch((err: Error) => { |
175 | | - if (this.__showPlaceholder) { |
176 | | - this.__showPlaceholder = false; |
177 | | - this.forceUpdate(); |
178 | | - } |
179 | 172 | reject(err); |
180 | 173 | }); |
181 | 174 | }); |
@@ -193,12 +186,15 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
193 | 186 | super.forceUpdate(); |
194 | 187 | } |
195 | 188 | } |
196 | | - |
| 189 | + /** |
| 190 | + * execute method in schema.lifeCycles |
| 191 | + * @PRIVATE |
| 192 | + */ |
197 | 193 | __excuteLifeCycleMethod = (method: string, args?: any) => { |
198 | 194 | const lifeCycleMethods = getValue(this.props.__schema, 'lifeCycles', {}); |
199 | 195 | let fn = lifeCycleMethods[method]; |
200 | 196 | if (fn) { |
201 | | - // TODO, cache |
| 197 | + // TODO: cache |
202 | 198 | if (isJSExpression(fn) || isJSFunction(fn)) { |
203 | 199 | fn = this.parseExpression(fn, this); |
204 | 200 | } |
@@ -214,6 +210,10 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
214 | 210 | } |
215 | 211 | }; |
216 | 212 |
|
| 213 | + /** |
| 214 | + * this method is for legacy purpose only, which used _ prefix instead of __ as private for some historical reasons |
| 215 | + * @LEGACY |
| 216 | + */ |
217 | 217 | _getComponentView = (componentName: string) => { |
218 | 218 | const { __components } = this.props; |
219 | 219 | if (!__components) { |
@@ -262,14 +262,18 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
262 | 262 | return parseData(data, ctx || __ctx || this, { thisRequiredInJSE }); |
263 | 263 | }; |
264 | 264 |
|
265 | | - __initDataSource = (props = this.props) => { |
| 265 | + __initDataSource = (props: IBaseRendererProps) => { |
| 266 | + if (!props) { |
| 267 | + return; |
| 268 | + } |
266 | 269 | const schema = props.__schema || {}; |
267 | 270 | const defaultDataSource: DataSource = { |
268 | 271 | list: [], |
269 | 272 | }; |
270 | | - const dataSource = (schema && schema?.dataSource) || defaultDataSource; |
| 273 | + const dataSource = schema.dataSource || defaultDataSource; |
271 | 274 | // requestHandlersMap 存在才走数据源引擎方案 |
272 | | - if (props?.__appHelper?.requestHandlersMap) { |
| 275 | + const useDataSourceEngine = !!(props.__appHelper?.requestHandlersMap); |
| 276 | + if (useDataSourceEngine) { |
273 | 277 | this.__dataHelper = { |
274 | 278 | updateConfig: (updateDataSource: any) => { |
275 | 279 | const { dataSourceMap, reloadDataSource } = createDataSourceEngine( |
@@ -280,11 +284,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
280 | 284 |
|
281 | 285 | this.reloadDataSource = () => new Promise((resolve) => { |
282 | 286 | this.__debug('reload data source'); |
283 | | - // this.__showPlaceholder = true; |
284 | 287 | reloadDataSource().then(() => { |
285 | | - // this.__showPlaceholder = false; |
286 | | - // @TODO 是否需要 forceUpate |
287 | | - // this.forceUpdate(); |
288 | 288 | resolve({}); |
289 | 289 | }); |
290 | 290 | }); |
@@ -299,41 +299,41 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
299 | 299 | this.reloadDataSource = () => new Promise((resolve, reject) => { |
300 | 300 | this.__debug('reload data source'); |
301 | 301 | if (!this.__dataHelper) { |
302 | | - // this.__showPlaceholder = false; |
303 | 302 | return resolve({}); |
304 | 303 | } |
305 | 304 | this.__dataHelper.getInitData() |
306 | 305 | .then((res: any) => { |
307 | | - // this.__showPlaceholder = false; |
308 | 306 | if (isEmpty(res)) { |
309 | 307 | this.forceUpdate(); |
310 | 308 | return resolve({}); |
311 | 309 | } |
312 | 310 | this.setState(res, resolve as () => void); |
313 | 311 | }) |
314 | 312 | .catch((err: Error) => { |
315 | | - if (this.__showPlaceholder) { |
316 | | - this.__showPlaceholder = false; |
317 | | - this.forceUpdate(); |
318 | | - } |
319 | 313 | reject(err); |
320 | 314 | }); |
321 | 315 | }); |
322 | 316 | } |
323 | | - // 设置容器组件占位,若设置占位则在初始异步请求完成之前用loading占位且不渲染容器组件内部内容 |
324 | | - // @TODO __showPlaceholder 的逻辑一旦开启就关不掉,先注释掉了 |
325 | | - /* this.__showPlaceholder = this.__parseData(schema.props && schema.props.autoLoading) && (dataSource.list || []).some( |
326 | | - (item) => !!this.__parseData(item.isInit), |
327 | | - ); */ |
328 | 317 | }; |
329 | 318 |
|
330 | | - __initI18nAPIs = () => { |
| 319 | + /** |
| 320 | + * init i18n apis |
| 321 | + * @PRIVATE |
| 322 | + */ |
| 323 | + __initI18nAPIs = (props: IBaseRendererProps) => { |
331 | 324 | this.i18n = (key: string, values = {}) => { |
332 | | - const { locale, messages } = this.props; |
| 325 | + const { locale, messages } = props; |
333 | 326 | return getI18n(key, values, locale, messages); |
334 | 327 | }; |
335 | | - this.getLocale = () => this.props.locale; |
336 | | - this.setLocale = (loc: string) => this.appHelper?.utils?.i18n?.setLocale && this.appHelper?.utils?.i18n?.setLocale(loc); |
| 328 | + this.getLocale = () => props.locale; |
| 329 | + this.setLocale = (loc: string) => { |
| 330 | + const setLocaleFn = this.appHelper?.utils?.i18n?.setLocale; |
| 331 | + if (!setLocaleFn || typeof setLocaleFn !== 'function') { |
| 332 | + console.warn('initI18nAPIs Failed, i18n only works when appHelper.utils.i18n.setLocale() exists'); |
| 333 | + return undefined; |
| 334 | + } |
| 335 | + return setLocaleFn(loc); |
| 336 | + }; |
337 | 337 | }; |
338 | 338 |
|
339 | 339 | __writeCss = () => { |
|