feat: add function setter

This commit is contained in:
zude.hzd 2020-08-18 17:54:51 +08:00
parent 48b24d2c1a
commit 114b6b06ad
2 changed files with 97 additions and 40 deletions

View File

@ -1,36 +1,30 @@
.lc-icon-setter{ .lc-function-setter{
.next-input{
width: auto !important; .function-container{
} position: relative;
i {
min-width: 12px;
}
i:hover {
cursor: pointer;
}
input:hover {
cursor: pointer;
}
} }
.funtion-icon{
.lowcode-icon-setter-popup{
max-width: 354px;
.lowcode-icon-list{
overflow: auto;
li{
float: left; float: left;
margin-bottom: 10px; width: 25px;
width: 40px; height: 25px;
color: #666; }
.function-name{
font-size: 14px;
color:#5584FF;
margin-left:5px;
cursor: pointer; cursor: pointer;
text-align: center;
&:hover{
color: #000;
}
} }
.funtion-operate-icon{
font-size: 16px;
margin-left:5px;
cursor: pointer;
} }
} }

View File

@ -1,8 +1,9 @@
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
// import PropTypes from 'prop-types'; // import PropTypes from 'prop-types';
import { Button } from '@alifd/next'; import { Button, Icon } from '@alifd/next';
import './index.scss'; import './index.scss';
import { timingSafeEqual } from 'crypto';
const SETTER_NAME = 'function-setter' const SETTER_NAME = 'function-setter'
@ -25,24 +26,84 @@ export default class FunctionSetter extends PureComponent<FunctionSetterProps, {
onChange: (icon: string | object) => undefined, onChange: (icon: string | object) => undefined,
}; };
private emitEventName = '';
state = { state = {
firstLoad: true, firstLoad: true,
}; };
componentDidMount() { componentDidMount() {
const { editor } = this.props.field; const { editor } = this.props.field;
editor.on(`${SETTER_NAME}.bindEvent`,(eventName)=>{ this.emitEventName = `${SETTER_NAME}-${this.props.field.id}`;
this.bindEventCallback(eventName); editor.on(`${this.emitEventName}.bindEvent`, this.bindEvent)
})
} }
bindEvent = (eventName) => {
this.bindEventCallback(eventName);
}
componentWillUnmount() {
const { editor } = this.props.field;
editor.off(`${this.emitEventName}.bindEvent`, this.bindEvent)
}
bindFunction = () => { bindFunction = () => {
const { field, value } = this.props; const { field, value } = this.props;
field.editor.emit('eventBindDialog.openDialog',field.name,SETTER_NAME); field.editor.emit('eventBindDialog.openDialog', field.name, this.emitEventName);
} }
bindEventCallback = (eventName) => { removeFunctionBind = () => {
const { field ,onChange} = this.props;
field.parent.clearPropValue(field.name);
}
parseFunctionName = (functionString: String) => {
// 因为函数格式是固定的,所以可以按照字符换去匹配获取函数名
let funNameStr = functionString.split('.')[1];
let endIndex = functionString.indexOf('(');
return funNameStr.substr(0, endIndex);
}
/**
* ()
*/
renderButton = () => {
return <Button type="primary" onClick={() => this.bindFunction()}></Button>
}
focusFunctionName = (functionName) => {
const { editor } = this.props.field;
editor.get('skeleton').getPanel('sourceEditor').show();
setTimeout(() => {
editor.emit('sourceEditor.focusByFunction', {
functionName
})
}, 300)
}
/**
*
*/
renderBindFunction = () => {
const { value } = this.props;
// 解析函数名
let functionName = this.parseFunctionName(value.value);
// let functionName = 'onClick';
return <div className="function-container">
<img className="funtion-icon" src="https://gw.alicdn.com/tfs/TB1os6KRFT7gK0jSZFpXXaTkpXa-200-200.png"></img>
<span className="function-name" onClick={() => this.focusFunctionName(functionName)}>{functionName}</span>
<Icon type="set" size="medium" className="funtion-operate-icon" onClick={this.bindFunction} />
<Icon type="ashbin" size="medium" className="funtion-operate-icon" onClick={this.removeFunctionBind} />
</div>
}
bindEventCallback = (eventName: String) => {
const { onChange } = this.props; const { onChange } = this.props;
onChange({ onChange({
type: 'JSFunction', type: 'JSFunction',
@ -51,9 +112,11 @@ export default class FunctionSetter extends PureComponent<FunctionSetterProps, {
} }
render() { render() {
const { icons, value, defaultValue, onChange, placeholder, hasClear, type } = this.props; const { value } = this.props;
const { firstLoad } = this.state; return <div className="lc-function-setter">
{
return <div className="lc-icon-setter"><Button type="primary" onClick={()=>this.bindFunction()}></Button></div>; value ? this.renderBindFunction() : this.renderButton()
}
</div>;
} }
} }