2020-10-12 10:29:09 +08:00

101 lines
2.5 KiB
TypeScript

import { Component } from 'react';
import { isJSSlot } from '@ali/lowcode-types';
import { Button, Input, Icon } from '@alifd/next';
import './index.less';
export default class SlotSetter extends Component<{
value: any;
onChange?: (value: any) => void;
onInitial?: () => void;
// 是否支持设置入参
supportParams?: boolean;
}> {
private handleInitial = () => {
const { onChange, onInitial } = this.props;
if (onInitial) {
onInitial();
return;
}
if (!onChange) {
return;
}
onChange({
type: 'JSSlot',
value: null,
});
};
render() {
const { value, onChange, supportParams } = this.props;
if (!isJSSlot(value)) {
return (
<Button type="primary" onClick={this.handleInitial}>
</Button>
);
}
const hasParams = value.params && Array.isArray(value.params);
return (
<div className="lc-setter-slot lc-setter-slot-column">
<Button
onClick={() => {
// TODO: use slot first child literal value pad
onChange && onChange(null);
}}
type="secondary"
>
</Button>
{hasParams ? (
<Input
className="lc-slot-params"
addonTextBefore="入参"
placeholder="插槽入参,以逗号风格"
value={value.params!.join(',')}
autoFocus
onChange={(val) => {
val = val.trim();
const params = val ? val.split(/ *, */) : [];
onChange &&
onChange({
...value,
params: params.length == 0 ? [''] : params,
});
}}
addonAfter={
<Button
type="secondary"
onClick={() => {
onChange &&
onChange({
...value,
params: [''],
});
}}
>
<Icon type="close" />
</Button>
}
/>
) : supportParams ? (
<Button
className="lc-slot-params"
type="primary"
onClick={() => {
onChange &&
onChange({
...value,
params: [],
});
}}
>
</Button>
) : null}
</div>
);
}
}