Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SettingForm 优化 #125

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/designer/src/setters/event-setter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ExpressionModal, getWrappedExpressionCode } from './expression-setter';

enum EventAction {
NoAction = 'noAction',
ConsoleLog = 'consoleLog',
BindExpression = 'bindExpression',
OpenModal = 'openModal',
CloseModal = 'closeModal',
Expand All @@ -24,6 +25,7 @@ const wrapperStyle = css`

const options = [
{ label: '无动作', value: EventAction.NoAction },
{ label: '打印事件', value: EventAction.ConsoleLog },
{ label: '绑定 JS 表达式', value: EventAction.BindExpression },
{ label: '打开页面', value: EventAction.NavigateTo },
{ label: '打开弹窗', value: EventAction.OpenModal },
Expand All @@ -36,7 +38,7 @@ export type EventSetterProps = FormItemComponentProps<string>;
* 事件监听函数绑定器
*/
export function EventSetter(props: EventSetterProps) {
const { value, onChange, modalTitle } = props;
const { value, onChange, modalTitle, toggleIsVariable } = props;
const [type, setType] = useState<EventAction>(); // 事件类型
const [temp, setTemp] = useState(''); // 二级暂存值
const [expModalVisible, setExpModalVisible] = useState(false); // 弹窗是否显示
Expand All @@ -50,6 +52,8 @@ export function EventSetter(props: EventSetterProps) {
const ret = getWrappedExpressionCode(nextValue);
if (ret !== value) {
onChange(ret, ...args);
// 默认切换至源码模式
toggleIsVariable();
}
},
[onChange, value],
Expand All @@ -63,6 +67,9 @@ export function EventSetter(props: EventSetterProps) {
case EventAction.BindExpression:
setExpModalVisible(true);
break;
case EventAction.ConsoleLog:
handleChange('(...args) => console.log(...args)');
break;
case EventAction.NoAction:
handleChange(undefined);
break;
Expand Down
37 changes: 34 additions & 3 deletions packages/designer/src/setters/expression-setter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useCallback } from 'react';
import { Box, Text, css } from 'coral-system';
import { Modal } from 'antd';
import { Dropdown, Modal } from 'antd';
import {
isValidExpressionCode,
isWrappedByExpressionContainer,
Expand All @@ -13,8 +13,14 @@ import {
getValue,
IVariableTreeNode,
} from '@music163/tango-helpers';
import { CloseCircleFilled, ExpandAltOutlined } from '@ant-design/icons';
import { Panel, InputCode, Action } from '@music163/tango-ui';
import {
CloseCircleFilled,
ExpandAltOutlined,
InfoOutlined,
KeyOutlined,
MenuOutlined,
} from '@ant-design/icons';
import { Panel, InputCode, Action, CodeOutlined } from '@music163/tango-ui';
import { FormItemComponentProps } from '@music163/tango-setting-form';
import { useWorkspace, useWorkspaceData } from '@music163/tango-context';
import { VariableTree } from '../components';
Expand Down Expand Up @@ -60,6 +66,8 @@ export function getWrappedExpressionCode(code: string) {
}

const suffixStyle = css`
display: flex;
align-items: center;
.anticon-close-circle {
color: rgba(0, 0, 0, 0.25);
&:hover {
Expand All @@ -73,6 +81,7 @@ export interface ExpressionSetterProps extends FormItemComponentProps<string> {
modalTip?: string;
autoCompleteOptions?: string[];
allowClear?: boolean;
showOptionsDropDown?: boolean;
}

export function ExpressionSetter(props: ExpressionSetterProps) {
Expand All @@ -86,6 +95,7 @@ export function ExpressionSetter(props: ExpressionSetterProps) {
status,
allowClear = true,
newStoreTemplate,
showOptionsDropDown = true,
} = props;
const [visible, { on, off }] = useBoolean();
const [inputValue, setInputValue] = useState(() => {
Expand Down Expand Up @@ -118,6 +128,7 @@ export function ExpressionSetter(props: ExpressionSetterProps) {

return (
<Box className="ExpressionSetter">
{/* 同时支持下拉框展示 */}
<InputCode
placeholder={placeholder}
suffix={
Expand All @@ -130,6 +141,26 @@ export function ExpressionSetter(props: ExpressionSetterProps) {
}}
/>
)}
{showOptionsDropDown && autoCompleteOptions?.length && (
<Dropdown
menu={{
items: autoCompleteOptions.map((option) => ({
key: option,
label: (
<Text
onClick={() => {
change(option);
}}
>
{option}
</Text>
),
})),
}}
>
<Action tooltip="使用预设代码片段" icon={<MenuOutlined />} size="small" />
</Dropdown>
)}
<Action
tooltip="打开表达式变量选择面板"
icon={<ExpandAltOutlined />}
Expand Down
3 changes: 3 additions & 0 deletions packages/designer/src/setters/render-props-setter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function RenderSetter({
text = '自定义渲染为',
options = [],
fallbackOption,
toggleIsVariable,
}: FormItemComponentProps & RenderSetterProps) {
const optionsMap = useMemo(() => {
return options.reduce((prev, cur) => {
Expand All @@ -36,6 +37,8 @@ export function RenderSetter({
const option = optionsMap[key] || fallbackOption;
if (option?.render) {
onChange(option.render, { relatedImports: option.relatedImports });
// 默认切换至源码模式
toggleIsVariable();
} else {
onChange(undefined);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/designer/src/setting-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export const SettingPanel = observer((props: SettingPanelProps) => {
borderLeftColor="line2"
bg="white"
headerProps={headerProps}
bodyProps={{
overflowY: 'hidden',
}}
className="SettingPanel"
>
{prototype ? (
Expand Down
2 changes: 2 additions & 0 deletions packages/setting-form/src/form-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface FormItemComponentProps<T = any> {
onChange: (value: T, detail?: SetterOnChangeDetailType) => void;
readOnly?: boolean;
disabled?: boolean;
toggleIsVariable?: () => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disableVariableSetter 区别是啥?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

选中后自动切换到 源码模式

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分逻辑已经提供了支持,无需增加 toggleIsVariable 的逻辑。先拿掉吧。其他都没啥问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

status?: InputProps['status'];
[prop: string]: any;
}
Expand Down Expand Up @@ -144,6 +145,7 @@ export function createFormItem(options: IFormItemCreateOptions) {
...baseComponentProps,
...setterProps, // setterProps 优先级大于快捷属性
...getSetterProps(model),
toggleIsVariable, // 是否显示源码模式
})
);

Expand Down
2 changes: 2 additions & 0 deletions packages/setting-form/src/form-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function FormControlGroup({
collapsed={collapsed}
onCollapse={setCollapsed}
className="FormControlGroup"
stickyHeader
title={
<Box display="flex" columnGap="s">
<Checkbox
Expand All @@ -109,6 +110,7 @@ export function FormControlGroup({
borderColor="line.normal"
headerProps={{
bg: 'fill1',
zIndex: 2,
}}
>
<Box
Expand Down
15 changes: 12 additions & 3 deletions packages/setting-form/src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@

const formStyle = css`
position: relative;

height: 100%;
display: flex;
flex-direction: column;
> .SettingFormMain > :first-child:is(.FormControl) {
margin-top: var(--tango-space-m);
}
Expand Down Expand Up @@ -193,13 +195,13 @@
if (modelProp && modelProp !== model) {
setModel(modelProp);
}
}, [modelProp]);

Check warning on line 198 in packages/setting-form/src/form.tsx

View workflow job for this annotation

GitHub Actions / CI

React Hook useEffect has a missing dependency: 'model'. Either include it or remove the dependency array

return (
<FormVariableProvider value={{ disableSwitchExpressionSetter, showItemSubtitle }}>
<FormModelProvider value={model}>
<Box className="SettingForm" mb="xl" css={formStyle}>
<Box className="SettingFormHeader" position="sticky" top="0" bg="white" zIndex="2">
<Box className="SettingFormHeader" bg="white">
<Box px="l" py="m">
{showIdentifier && (
<FormHeader
Expand Down Expand Up @@ -247,7 +249,14 @@
</Box>
)}
</Box>
<Box className="SettingFormMain" display="flex" flexDirection="column" rowGap="m">
<Box
className="SettingFormMain"
display="flex"
flexDirection="column"
paddingBottom="m"
rowGap="m"
overflow="auto"
>
{renderProps(filterProps)}
{filterProps.length === 0 && (
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="未找到配置项" />
Expand Down
Loading