Browse Source

feat: 以函数的方式打开 modal

pull/170/head
cp3hnu 11 months ago
parent
commit
13560fc65d
10 changed files with 154 additions and 14 deletions
  1. +1
    -1
      react-ui/.storybook/main.ts
  2. +2
    -5
      react-ui/.storybook/mock/umijs.mock.tsx
  3. +1
    -0
      react-ui/.storybook/preview.tsx
  4. +2
    -3
      react-ui/src/components/CodeSelect/index.tsx
  5. +6
    -2
      react-ui/src/components/CodeSelectorModal/index.tsx
  6. +1
    -1
      react-ui/src/components/ParameterInput/index.tsx
  7. +9
    -2
      react-ui/src/components/ResourceSelectorModal/index.tsx
  8. +28
    -0
      react-ui/src/stories/CodeSelect.stories.tsx
  9. +81
    -0
      react-ui/src/stories/CodeSelectorModal.stories.tsx
  10. +23
    -0
      react-ui/src/stories/KFModal.stories.tsx

+ 1
- 1
react-ui/.storybook/main.ts View File

@@ -21,7 +21,7 @@ const config: StorybookConfig = {
config.resolve.alias = { config.resolve.alias = {
...config.resolve.alias, ...config.resolve.alias,
'@': path.resolve(__dirname, '../src'), '@': path.resolve(__dirname, '../src'),
'@umijs/max$': path.resolve(__dirname, './mock/umijs.tsx'),
'@umijs/max$': path.resolve(__dirname, './mock/umijs.mock.tsx'),
}; };
} }
if (config.module && config.module.rules) { if (config.module && config.module.rules) {


react-ui/.storybook/mock/umijs.tsx → react-ui/.storybook/mock/umijs.mock.tsx View File

@@ -4,9 +4,6 @@ export const Link = ({ to, children, ...props }: any) => (
</a> </a>
); );


export const request = () => {
return Promise.resolve({
success: true,
data: { message: 'Mocked response' },
});
export const request = (url: string, options: any) => {
return fetch(url, options).then((res) => res.json());
}; };

+ 1
- 0
react-ui/.storybook/preview.tsx View File

@@ -20,6 +20,7 @@ const preview: Preview = {
<ConfigProvider <ConfigProvider
locale={zhCN} locale={zhCN}
theme={{ theme={{
cssVar: true,
token: { token: {
colorPrimary: themes['primaryColor'], colorPrimary: themes['primaryColor'],
colorSuccess: themes['successColor'], colorSuccess: themes['successColor'],


+ 2
- 3
react-ui/src/components/CodeSelect/index.tsx View File

@@ -13,9 +13,8 @@ import './index.less';


export { requiredValidator, type ParameterInputObject } from '../ParameterInput'; export { requiredValidator, type ParameterInputObject } from '../ParameterInput';


type CodeSelectProps = ParameterInputProps;

function CodeSelect({ value, onChange, disabled, ...rest }: CodeSelectProps) {
/** 代码配置选择表单组件 */
function CodeSelect({ value, onChange, disabled, ...rest }: ParameterInputProps) {
const selectResource = () => { const selectResource = () => {
const { close } = openAntdModal(CodeSelectorModal, { const { close } = openAntdModal(CodeSelectorModal, {
onOk: (res) => { onOk: (res) => {


+ 6
- 2
react-ui/src/components/CodeSelectorModal/index.tsx View File

@@ -4,11 +4,11 @@
* @Description: 选择代码 * @Description: 选择代码
*/ */


import KFIcon from '@/components/KFIcon';
import KFModal from '@/components/KFModal'; import KFModal from '@/components/KFModal';
import { type CodeConfigData } from '@/pages/CodeConfig/List'; import { type CodeConfigData } from '@/pages/CodeConfig/List';
import { getCodeConfigListReq } from '@/services/codeConfig'; import { getCodeConfigListReq } from '@/services/codeConfig';
import { to } from '@/utils/promise'; import { to } from '@/utils/promise';
import { Icon } from '@umijs/max';
import type { ModalProps, PaginationProps } from 'antd'; import type { ModalProps, PaginationProps } from 'antd';
import { Empty, Input, Pagination } from 'antd'; import { Empty, Input, Pagination } from 'antd';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
@@ -21,6 +21,7 @@ export interface CodeSelectorModalProps extends Omit<ModalProps, 'onOk'> {
onOk?: (params: CodeConfigData | undefined) => void; onOk?: (params: CodeConfigData | undefined) => void;
} }


/** 代码配置选择弹窗 */
function CodeSelectorModal({ onOk, ...rest }: CodeSelectorModalProps) { function CodeSelectorModal({ onOk, ...rest }: CodeSelectorModalProps) {
const [dataList, setDataList] = useState<CodeConfigData[]>([]); const [dataList, setDataList] = useState<CodeConfigData[]>([]);
const [total, setTotal] = useState(0); const [total, setTotal] = useState(0);
@@ -90,8 +91,11 @@ function CodeSelectorModal({ onOk, ...rest }: CodeSelectorModalProps) {
suffix={null} suffix={null}
value={inputText} value={inputText}
prefix={ prefix={
<Icon icon="local:magnifying-glass" style={{ marginLeft: '10px', marginTop: '2px' }} />
<KFIcon type="icon-sousuo" color="rgba(22,100,255,0.4" style={{ marginLeft: '10px' }} />
} }
// prefix={
// <Icon icon="local:magnifying-glass" style={{ marginLeft: '10px', marginTop: '2px' }} />
// }
/> />
{dataList?.length !== 0 ? ( {dataList?.length !== 0 ? (
<> <>


+ 1
- 1
react-ui/src/components/ParameterInput/index.tsx View File

@@ -1,7 +1,7 @@
/* /*
* @Author: 赵伟 * @Author: 赵伟
* @Date: 2024-04-16 08:42:57 * @Date: 2024-04-16 08:42:57
* @Description: 参数输入组件,支持手动输入,选择全局参数,选择数据集/模型/镜像
* @Description: 参数输入表单组件,支持手动输入,也支持选择全局参数
*/ */


import { CommonTabKeys } from '@/enums'; import { CommonTabKeys } from '@/enums';


+ 9
- 2
react-ui/src/components/ResourceSelectorModal/index.tsx View File

@@ -4,11 +4,11 @@
* @Description: 选择数据集、模型、镜像 * @Description: 选择数据集、模型、镜像
*/ */


import KFIcon from '@/components/KFIcon';
import KFModal from '@/components/KFModal'; import KFModal from '@/components/KFModal';
import { CommonTabKeys } from '@/enums'; import { CommonTabKeys } from '@/enums';
import { ResourceFileData } from '@/pages/Dataset/config'; import { ResourceFileData } from '@/pages/Dataset/config';
import { to } from '@/utils/promise'; import { to } from '@/utils/promise';
import { Icon } from '@umijs/max';
import type { GetRef, ModalProps, TreeDataNode, TreeProps } from 'antd'; import type { GetRef, ModalProps, TreeDataNode, TreeProps } from 'antd';
import { Input, Tabs, Tree } from 'antd'; import { Input, Tabs, Tree } from 'antd';
import React, { useEffect, useMemo, useRef, useState } from 'react'; import React, { useEffect, useMemo, useRef, useState } from 'react';
@@ -267,7 +267,14 @@ function ResourceSelectorModal({
variant="borderless" variant="borderless"
value={searchText} value={searchText}
onChange={(e) => setSearchText(e.target.value)} onChange={(e) => setSearchText(e.target.value)}
prefix={<Icon icon="local:magnifying-glass" style={{ height: '15px' }} />}
prefix={
<KFIcon
type="icon-sousuo"
color="rgba(22,100,255,0.4)"
style={{ height: '15px' }}
/>
}
// prefix={<Icon icon="local:magnifying-glass" style={{ height: '15px' }} />}
/> />
<Tree <Tree
ref={treeRef} ref={treeRef}


+ 28
- 0
react-ui/src/stories/CodeSelect.stories.tsx View File

@@ -0,0 +1,28 @@
import CodeSelect from '@/components/CodeSelect';
import type { Meta, StoryObj } from '@storybook/react';

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
title: 'Components/CodeSelect',
component: CodeSelect,
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
// layout: 'centered',
},
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
tags: ['autodocs'],
// More on argTypes: https://storybook.js.org/docs/api/argtypes
argTypes: {
// backgroundColor: { control: 'color' },
},
// Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
// args: { onClick: fn() },
} satisfies Meta<typeof CodeSelect>;

export default meta;
type Story = StoryObj<typeof meta>;

// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
export const Primary: Story = {
args: {},
};

+ 81
- 0
react-ui/src/stories/CodeSelectorModal.stories.tsx View File

@@ -0,0 +1,81 @@
import CodeSelectorModal from '@/components/CodeSelectorModal';
import { openAntdModal } from '@/utils/modal';
import { useArgs } from '@storybook/preview-api';
import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';
import { Button } from 'antd';

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
title: 'Components/CodeSelectorModal',
component: CodeSelectorModal,
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
layout: 'centered',
},
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
tags: ['autodocs'],
// More on argTypes: https://storybook.js.org/docs/api/argtypes
argTypes: {
// backgroundColor: { control: 'color' },
title: {
description: '标题',
},
open: {
description: '对话框是否可见',
},
},
// Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
args: { onCancel: fn(), onOk: fn() },
} satisfies Meta<typeof CodeSelectorModal>;

export default meta;
type Story = StoryObj<typeof meta>;

// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
export const Primary: Story = {
args: {
open: false,
},
render: function Render(args) {
const [{ open }, updateArgs] = useArgs();
function onClick() {
updateArgs({ open: true });
}
function onOk() {
updateArgs({ open: false });
}

function onCancel() {
updateArgs({ open: false });
}

return (
<>
<Button type="primary" onClick={onClick}>
选择代码配置
</Button>
<CodeSelectorModal {...args} open={open} onOk={onOk} onCancel={onCancel} />
</>
);
},
};

const OpenModalByFunction = () => {
const handleOnChange = () => {
const { close } = openAntdModal(CodeSelectorModal, {
onOk: () => {
close();
},
});
};
return (
<Button type="primary" onClick={handleOnChange}>
以函数的方式打开
</Button>
);
};

export const OpenInFunction: Story = {
render: () => <OpenModalByFunction />,
};

+ 23
- 0
react-ui/src/stories/KFModal.stories.tsx View File

@@ -1,5 +1,6 @@
import CreateExperiment from '@/assets/img/create-experiment.png'; import CreateExperiment from '@/assets/img/create-experiment.png';
import KFModal from '@/components/KFModal'; import KFModal from '@/components/KFModal';
import { openAntdModal } from '@/utils/modal';
import { useArgs } from '@storybook/preview-api'; import { useArgs } from '@storybook/preview-api';
import type { Meta, StoryObj } from '@storybook/react'; import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test'; import { fn } from '@storybook/test';
@@ -63,3 +64,25 @@ export const Primary: Story = {
); );
}, },
}; };

const OpenModalByFunction = () => {
const handleOnChange = () => {
const { close } = openAntdModal(KFModal, {
title: '创建实验',
image: CreateExperiment,
children: '这是一个模态框',
onOk: () => {
close();
},
});
};
return (
<Button type="primary" onClick={handleOnChange}>
以函数的方式打开
</Button>
);
};

export const OpenInFunction: Story = {
render: () => <OpenModalByFunction />,
};

Loading…
Cancel
Save