diff --git a/src/renderers/Chart.tsx b/src/renderers/Chart.tsx index 6c754c54..e238e894 100644 --- a/src/renderers/Chart.tsx +++ b/src/renderers/Chart.tsx @@ -7,7 +7,7 @@ import {filter, evalExpression} from '../utils/tpl'; import cx from 'classnames'; import LazyComponent from '../components/LazyComponent'; import {resizeSensor} from '../utils/resize-sensor'; -import {resolveVariableAndFilter} from '../utils/tpl-builtin'; +import {resolveVariableAndFilter, isPureVariable} from '../utils/tpl-builtin'; import {isApiOutdated, isEffectiveApi} from '../utils/api'; import {ScopedContext, IScopedContext} from '../Scoped'; @@ -51,7 +51,7 @@ export class Chart extends React.Component { this.mounted = true; - if (source && /^\$(?:([a-z0-9_.]+)|{.+})$/.test(source)) { + if (source && isPureVariable(source)) { const ret = resolveVariableAndFilter(source, data, '| raw'); ret && this.renderChart(ret); } else if (api && initFetch !== false) { @@ -68,10 +68,7 @@ export class Chart extends React.Component { if (isApiOutdated(prevProps.api, props.api, prevProps.data, props.data)) { this.reload(); - } else if ( - props.source && - /^\$(?:([a-z0-9_.]+)|{.+})$/.test(props.source) - ) { + } else if (props.source && isPureVariable(props.source)) { const prevRet = prevProps.source ? resolveVariableAndFilter(prevProps.source, prevProps.data, '| raw') : null; diff --git a/src/renderers/Form/DiffEditor.tsx b/src/renderers/Form/DiffEditor.tsx index da0579db..b475fb61 100644 --- a/src/renderers/Form/DiffEditor.tsx +++ b/src/renderers/Form/DiffEditor.tsx @@ -5,6 +5,7 @@ import {filter} from '../../utils/tpl'; import cx from 'classnames'; import LazyComponent from '../../components/LazyComponent'; import debouce = require('lodash/debounce'); +import {isPureVariable} from '../../utils/tpl-builtin'; function loadComponent(): Promise { return new Promise(resolve => @@ -101,7 +102,7 @@ export class DiffEditor extends React.Component { this.originalEditor .getModel() .setValue( - /^\$(?:([a-z0-9_.]+)|{.+})$/.test(diffValue as string) + isPureVariable(diffValue as string) ? filter(normalizeValue(diffValue || ''), data, '| raw') : normalizeValue(diffValue) ); @@ -143,7 +144,7 @@ export class DiffEditor extends React.Component { this.editor.setModel({ original: this.monaco.editor.createModel( - /^\$(?:([a-z0-9_.]+)|{.+})$/.test(diffValue as string) + isPureVariable(diffValue as string) ? filter(normalizeValue(diffValue || ''), data, '| raw') : normalizeValue(diffValue), language diff --git a/src/renderers/Form/Options.tsx b/src/renderers/Form/Options.tsx index 50cb964c..63ee63b7 100644 --- a/src/renderers/Form/Options.tsx +++ b/src/renderers/Form/Options.tsx @@ -2,9 +2,9 @@ * @file 所有列表选择类控件的父级,比如 Select、Radios、Checkboxes、 * List、ButtonGroup 等等 */ -import { Api, Schema } from "../../types"; -import { isEffectiveApi, isApiOutdated } from "../../utils/api"; -import { isAlive } from "mobx-state-tree"; +import {Api, Schema} from '../../types'; +import {isEffectiveApi, isApiOutdated} from '../../utils/api'; +import {isAlive} from 'mobx-state-tree'; import { anyChanged, autobind, @@ -13,29 +13,32 @@ import { spliceTree, findTreeIndex, getTree -} from "../../utils/helper"; -import { reaction } from "mobx"; +} from '../../utils/helper'; +import {reaction} from 'mobx'; import { FormControlProps, registerFormItem, FormItemBasicConfig, detectProps as itemDetectProps -} from "./Item"; -import { IFormItemStore } from "../../store/formItem"; +} from './Item'; +import {IFormItemStore} from '../../store/formItem'; export type OptionsControlComponent = React.ComponentType; -import React from "react"; -import { resolveVariableAndFilter } from "../../utils/tpl-builtin"; +import React from 'react'; +import { + resolveVariableAndFilter, + isPureVariable +} from '../../utils/tpl-builtin'; import { Option, OptionProps, normalizeOptions, optionValueCompare -} from "../../components/Select"; -import { filter } from "../../utils/tpl"; -import findIndex from "lodash/findIndex"; +} from '../../components/Select'; +import {filter} from '../../utils/tpl'; +import findIndex from 'lodash/findIndex'; -export { Option }; +export {Option}; export interface OptionsBasicConfig extends FormItemBasicConfig { autoLoadOptionsFromSource?: boolean; @@ -87,18 +90,18 @@ export interface OptionsProps extends FormControlProps, OptionProps { } export const detectProps = itemDetectProps.concat([ - "options", - "size", - "buttons", - "columnsCount", - "multiple", - "hideRoot", - "checkAll", - "showIcon", - "showRadio", - "btnDisabled", - "joinValues", - "extractValue" + 'options', + 'size', + 'buttons', + 'columnsCount', + 'multiple', + 'hideRoot', + 'checkAll', + 'showIcon', + 'showRadio', + 'btnDisabled', + 'joinValues', + 'extractValue' ]); export function registerOptionsControl(config: OptionsConfig) { @@ -107,15 +110,15 @@ export function registerOptionsControl(config: OptionsConfig) { class FormOptionsItem extends React.Component { static displayName = `OptionsControl(${config.type})`; static defaultProps = { - delimiter: ",", - labelField: "label", - valueField: "value", + delimiter: ',', + labelField: 'label', + valueField: 'value', joinValues: true, extractValue: false, multiple: false, - placeholder: "请选择", - resetValue: "", - deleteConfirmText: "确定要删除?", + placeholder: '请选择', + resetValue: '', + deleteConfirmText: '确定要删除?', ...Control.defaultProps }; static propsList: any = (Control as any).propsList @@ -155,10 +158,10 @@ export function registerOptionsControl(config: OptionsConfig) { let loadOptions: boolean = initFetch !== false; - if (/^\$(?:([a-z0-9_.]+)|{.+})$/.test(source as string) && formItem) { + if (isPureVariable(source as string) && formItem) { formItem.setOptions( normalizeOptions( - resolveVariableAndFilter(source as string, data, "| raw") || [] + resolveVariableAndFilter(source as string, data, '| raw') || [] ) ); loadOptions = false; @@ -170,7 +173,7 @@ export function registerOptionsControl(config: OptionsConfig) { .getSelectedOptions(value) .map( (selectedOption: Option) => - selectedOption[valueField || "value"] + selectedOption[valueField || 'value'] ) : formItem.getSelectedOptions(value); setPrinstineValue( @@ -182,7 +185,7 @@ export function registerOptionsControl(config: OptionsConfig) { config.autoLoadOptionsFromSource !== false && (formInited ? this.reload() - : addHook && addHook(this.initOptions, "init")); + : addHook && addHook(this.initOptions, 'init')); } componentDidMount() { @@ -225,16 +228,16 @@ export function registerOptionsControl(config: OptionsConfig) { formItem && (prevProps.source !== props.source || prevProps.data !== props.data) ) { - if (/^\$(?:([a-z0-9_.]+)|{.+})$/.test(props.source as string)) { + if (isPureVariable(props.source as string)) { const prevOptions = resolveVariableAndFilter( prevProps.source as string, prevProps.data, - "| raw" + '| raw' ); const options = resolveVariableAndFilter( props.source as string, props.data, - "| raw" + '| raw' ); prevOptions !== options && formItem.setOptions(normalizeOptions(options || [])); @@ -260,7 +263,7 @@ export function registerOptionsControl(config: OptionsConfig) { } componentWillUnmount() { - this.props.removeHook && this.props.removeHook(this.reload, "init"); + this.props.removeHook && this.props.removeHook(this.reload, 'init'); this.reaction && this.reaction(); } @@ -280,7 +283,7 @@ export function registerOptionsControl(config: OptionsConfig) { if ( extractValue === false && - (typeof value === "string" || typeof value === "number") + (typeof value === 'string' || typeof value === 'number') ) { const selectedOptions = formItem.getSelectedOptions(value); formItem.changeValue( @@ -292,16 +295,16 @@ export function registerOptionsControl(config: OptionsConfig) { !( (Array.isArray(value) && value.every( - val => typeof val === "string" || typeof val === "number" + val => typeof val === 'string' || typeof val === 'number' )) || - typeof value === "string" || - typeof value === "number" + typeof value === 'string' || + typeof value === 'number' ) ) { const selectedOptions = formItem .getSelectedOptions(value) .map( - (selectedOption: Option) => selectedOption[valueField || "value"] + (selectedOption: Option) => selectedOption[valueField || 'value'] ); formItem.changeValue( multiple ? selectedOptions.concat() : selectedOptions[0] @@ -344,9 +347,9 @@ export function registerOptionsControl(config: OptionsConfig) { let valueArray = formItem.getSelectedOptions(value).concat(); const idx = findIndex( valueArray, - optionValueCompare(option.value, valueField || "value") + optionValueCompare(option.value, valueField || 'value') ); - let newValue: string | Array