Skip to content

Commit

Permalink
✨ feat: TipGuide add controlled api
Browse files Browse the repository at this point in the history
  • Loading branch information
ONLY-yours committed Aug 31, 2023
1 parent 01f6325 commit a5df92a
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 69 deletions.
37 changes: 37 additions & 0 deletions src/TipGuide/demos/controlled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { TipGuide } from '@ant-design/pro-editor';

import { Button, List, Space } from 'antd';
import { useState } from 'react';

export default () => {
const guideKey = 'DEMO_KEY123';

const [open, setOpen] = useState(true);

return (
<Space>
<Button
onClick={() => {
setOpen(true);
}}
>
Open
</Button>
<Button
onClick={() => {
setOpen(false);
}}
>
Close
</Button>
<TipGuide
guideKey={guideKey}
open={open}
placement="right"
title={'现在这个 List 是一个空数据,需要你添加数据'}
>
<List />
</TipGuide>
</Space>
);
};
42 changes: 42 additions & 0 deletions src/TipGuide/demos/footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { TipGuide } from '@ant-design/pro-editor';

import { Button, Space } from 'antd';
import { useState } from 'react';

export default () => {
const guideKey = 'DEMO_KEY';
const [open, setOpen] = useState(true);

return (
<TipGuide
guideKey={guideKey}
open={open}
placement="right"
footerRender={() => {
return (
<Space>
<Button type="primary" disabled>
Clear
</Button>
<Button
onClick={() => {
setOpen(false);
}}
>
Close
</Button>
</Space>
);
}}
title={'This Is Footer Render Demo !'}
>
<Button
onClick={() => {
setOpen(true);
}}
>
Open
</Button>
</TipGuide>
);
};
9 changes: 1 addition & 8 deletions src/TipGuide/demos/normal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ export default () => {
'道可道,非常道;名可名,非常名。无名天地之始,有名万物之母。故常无欲,以观其妙;常有欲,以观其徼(jiào)。此两者同出而异名,同谓之玄,玄之又玄,众妙之门。'
}
>
<Button
onClick={() => {
localStorage.removeItem(guideKey);
location.reload();
}}
>
清理引导缓存
</Button>
<Button onClick={() => {}}>Button</Button>
</TipGuide>
);
};
28 changes: 21 additions & 7 deletions src/TipGuide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ group: 基础组件

<code src="./demos/normal.tsx" ></code>

### 受控

使用 open 进行受控

<code src="./demos/controlled.tsx" ></code>

### 自定义 footer

<code src="./demos/footer.tsx" ></code>

### 偏移

可以通过 `offsetY` 将引导点进行垂直方向偏移。
Expand All @@ -26,10 +36,14 @@ group: 基础组件

## API

| 参数 | 说明 | 类型 | 默认值 |
| :-------- | :----------------------------- | :-------------------------- | :------- |
| title | 引导标题 | `string` | - |
| placement | ToolTip 位置 | `TooltipProps['placement']` | 'bottom' |
| offsetY | 纵向偏移值 | number | - |
| maxWidth | 最大宽度 | number | 300 |
| guideKey | 最用于持久化该引导值状态, 必填 | string | - |
| 参数 | 说明 | 类型 | 默认值 |
| :----------- | :---------------------------------- | :---------------------------------------- | :------- |
| title | 引导标题 | `string` | - |
| placement | ToolTip 位置 | `TooltipProps['placement']` | 'bottom' |
| offsetY | 纵向偏移值 | number | - |
| maxWidth | 最大宽度 | number | 300 |
| guideKey | 最用于持久化该引导值状态, 必填 | string | - |
| open | 受控的 open 属性 | boolean | - |
| onOpenChange | 当 open 属性变化时候的触发 | (open: boolean) => void | - |
| defaultOpen | 默认时候的打开状态 | boolean | - |
| footerRender | 用于自定义 footer 部分的 render api | (dom: React.ReactNode) => React.ReactNode | - |
82 changes: 55 additions & 27 deletions src/TipGuide/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { TooltipProps } from 'antd';
import { Badge, Button, Checkbox, Popover } from 'antd';
import type { FC, ReactNode } from 'react';
import { Badge, Button, Popover } from 'antd';
import useMergedState from 'rc-util/lib/hooks/useMergedState';

Check failure on line 3 in src/TipGuide/index.tsx

View workflow job for this annotation

GitHub Actions / test

Cannot find module 'rc-util/lib/hooks/useMergedState' or its corresponding type declarations.

Check failure on line 3 in src/TipGuide/index.tsx

View workflow job for this annotation

GitHub Actions / test

Cannot find module 'rc-util/lib/hooks/useMergedState' or its corresponding type declarations.
import { type FC, type ReactNode } from 'react';
import { Flexbox } from 'react-layout-kit';

import { ConfigProvider } from '../ConfigProvider';
import { getPrefixCls, useToken } from '../theme';
import { useStyle } from './style';
import { useTipGuide } from './useTipGuide';

export interface TipGuideProps {
/**
Expand Down Expand Up @@ -45,6 +44,22 @@ export interface TipGuideProps {
* 类名
*/
className?: string;
/**
* 受控的 open 属性
*/
open?: boolean;
/**
* 当 open 属性变化时候的触发
*/
onOpenChange?: (open: boolean) => void;
/**
* 默认时候的打开状态
*/
defaultOpen?: boolean;
/**
* 用于自定义 footer 部分的 render api
*/
footerRender?: (dom: React.ReactNode) => React.ReactNode;
}

const TipGuide: FC<TipGuideProps> = ({
Expand All @@ -57,13 +72,39 @@ const TipGuide: FC<TipGuideProps> = ({
className,
style,
guideKey,
defaultOpen = true,
open: outOpen,
onOpenChange = () => {},
footerRender,
}) => {
const prefixCls = getPrefixCls('tip-guide', customPrefixCls);

const { noLongerShow, toggleChecked, closeGuide, visible, checked } =
useTipGuide(guideKey);
const token = useToken();
const { styles, cx } = useStyle(prefixCls);
const [open, setOpen] = useMergedState<boolean>(defaultOpen, {
value: outOpen,
onChange: onOpenChange,
});

const FooterDom = () => {
const defalutFooter = (
<div className={styles.footer}>
<Button
type={'primary'}
size={'small'}
onClick={() => {
setOpen(!open);
}}
>
我知道了
</Button>
</div>
);
if (footerRender) {
return footerRender(defalutFooter);
}

return defalutFooter;
};
return (
<ConfigProvider
componentToken={{
Expand All @@ -76,10 +117,8 @@ const TipGuide: FC<TipGuideProps> = ({
Button: { colorPrimary: token['blue-7'] },
}}
>
{noLongerShow || !visible ? (
<>{children}</>
) : (
<div className={styles.container}>
{open ? (
<div className={styles.container} key={guideKey}>
{children}
<div
className={styles.tip}
Expand All @@ -88,25 +127,12 @@ const TipGuide: FC<TipGuideProps> = ({
}}
>
<Popover
open={visible}
open={open}
content={
<Flexbox className={`${prefixCls}-guide-content`} gap={24}>
<div>{title}</div>
<Flexbox
direction={'horizontal'}
distribution={'space-between'}
gap={8}
>
<Checkbox onChange={toggleChecked} checked={checked}>
不再显示
</Checkbox>
<Button
type={'primary'}
size={'small'}
onClick={closeGuide}
>
我知道了
</Button>
<Flexbox direction={'horizontal'} distribution={'space-between'} gap={8}>
<FooterDom />
</Flexbox>
</Flexbox>
}
Expand All @@ -121,6 +147,8 @@ const TipGuide: FC<TipGuideProps> = ({
</Popover>
</div>
</div>
) : (
<>{children}</>
)}
</ConfigProvider>
);
Expand Down
10 changes: 10 additions & 0 deletions src/TipGuide/style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createStyles } from '../theme';

export const useStyle = createStyles(({ css, cx }, prefixCls: string) => {
console.log('prefixCls', prefixCls);

return {
container: cx(
prefixCls,
Expand All @@ -9,6 +11,14 @@ export const useStyle = createStyles(({ css, cx }, prefixCls: string) => {
width: fit-content;
`,
),
footer: cx(
`${prefixCls}-footer`,
css`
width: 100%;
display: flex;
justify-content: end;
`,
),
tip: cx(
`${prefixCls}-tip`,
css`
Expand Down
27 changes: 0 additions & 27 deletions src/TipGuide/useTipGuide.ts

This file was deleted.

0 comments on commit a5df92a

Please sign in to comment.