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

feat: The tooltip and popover components added defaultOpen props #441

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions components/Popover/__test__/fixture/popover.open.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import KPopover from '../../src';
</script>

<KPopover trigger="click" defaultOpen={true}>
<div slot="triggerEl" cls="mx-2">top</div>
<div slot="contentEl" class="flex flex-col">
<p class="!my-2">有美一人,清扬婉兮</p>
</div>
</KPopover>
21 changes: 21 additions & 0 deletions components/Popover/__test__/popover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KPopoverDisabled from './fixture/popover.disabled.svelte';
import KPopoverChange from './fixture/popover.change.svelte';
import KPopoverArrow from './fixture/popover.arrow.svelte';
import KPopoverDelay from './fixture/popover.delay.svelte';
import KPopoverOpen from './fixture/popover.open.svelte';
let host: HTMLElement;

const initHost = () => {
Expand All @@ -24,6 +25,15 @@ afterEach(() => {
});
// TODO E2E test
describe('Test: KPopover', () => {
vi.mock('svelte', async () => {
const actual = (await vi.importActual('svelte')) as object;
return {
...actual,
// @ts-ignore
onMount: (await import('svelte/internal')).onMount
};
});

test('props: placement', async () => {
//@ts-ignore
const instance = new KPopoverPlacement({
Expand Down Expand Up @@ -84,6 +94,17 @@ describe('Test: KPopover', () => {
expect(host.innerHTML).matchSnapshot();
});

test('props: defaultOpen', async () => {
//@ts-ignore
const instance = new KPopoverOpen({
target: host
});
expect(instance).toBeTruthy();
await tick();
await vi.advanceTimersByTimeAsync(500);
expect(host.innerHTML.includes('有美一人,清扬婉兮')).toBeTruthy();
});

test('props: trigger', async () => {
//@ts-ignore
const instance = new KPopoverTrigger({
Expand Down
8 changes: 7 additions & 1 deletion components/Popover/src/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { getPrefixCls } from '@ikun-ui/utils';
import { clsx } from 'clsx';
import type { KPopoverProps } from './types';
import { createEventDispatcher } from 'svelte';
import { createEventDispatcher, onMount } from 'svelte';

export let placement: KPopoverProps['placement'] = 'top';
// hover click manual
Expand All @@ -16,6 +16,7 @@
export let mouseLeaveDelay: KPopoverProps['mouseLeaveDelay'] = 200;
export let cls: KPopoverProps['cls'] = undefined;
export let clsTrigger: KPopoverProps['clsTrigger'] = undefined;
export let defaultOpen: KPopoverProps['defaultOpen'] = undefined;
/**
* @internal
*/
Expand Down Expand Up @@ -61,6 +62,11 @@
} as PopperOptions<any>);

let isShow = false;
onMount(() => {
if (defaultOpen !== undefined) {
doUpdateShow(!!defaultOpen);
}
});
const handleClick = () => {
if (trigger === 'manual') {
doUpdateShow(!isShow);
Expand Down
1 change: 1 addition & 0 deletions components/Popover/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ClassValue } from 'clsx';
import { IKunPlacement, IKunTrigger } from '@ikun-ui/utils';

export type KPopoverProps = {
defaultOpen?: boolean;
placement: IKunPlacement;
trigger: IKunTrigger;
disabled: boolean;
Expand Down
15 changes: 12 additions & 3 deletions components/Tooltip/src/index.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { KPopover } from '@ikun-ui/popover';
import { KPopover, type KPopoverProps } from '@ikun-ui/popover';
import { clsx } from 'clsx';
import type { KTooltipProps } from './types';

Expand All @@ -12,11 +12,20 @@
export let disabled: KTooltipProps['disabled'] = false;
export let mouseEnterDelay: KTooltipProps['mouseEnterDelay'] = 200;
export let mouseLeaveDelay: KTooltipProps['mouseLeaveDelay'] = 200;

export let defaultOpen: KPopoverProps['defaultOpen'] = undefined;
$: cnames = clsx(cls);
</script>

<KPopover cls={cnames} {disabled} {trigger} {mouseEnterDelay} {mouseLeaveDelay} {placement} {attrs}>
<KPopover
cls={cnames}
{disabled}
{trigger}
{mouseEnterDelay}
{mouseLeaveDelay}
{placement}
{attrs}
{defaultOpen}
>
<span slot="contentEl">{content}</span>
<slot name="triggerEl" slot="triggerEl" />
</KPopover>
1 change: 1 addition & 0 deletions docs/components/KPopover.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Specify different trigger methods through the `trigger` attribute
| disabled | `boolean` | `false` | Disabled the popover |
| mouseEnterDelay | `number` | `200` | Delay in seconds, before `popover` is shown on mouse enter, unit `ms` |
| mouseLeaveDelay | `number` | `200` | Delay in seconds, before `popover` is shown on mouse enter, unit `ms` |
| defaultOpen | `boolean` | `-` | Open `popover` by default |
| arrow | `boolean` | `true` | Display the arrow |
| cls | `string` | `-` | Additional class |
| attrs | `Record<string, string>` | `{}` | Additional attributes |
Expand Down
3 changes: 2 additions & 1 deletion docs/components/KTooltip.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ Specify different trigger methods through the `trigger` attribute
## Tooltip Props

| Name | Type | Default | Description |
| --------------- | ------------------------------------- | ------- | --------------------------------------------------------------------- |
|-----------------|---------------------------------------|---------| --------------------------------------------------------------------- |
| placement | `'top' / 'left' / 'right' / 'bottom'` | `top` | Where the `tooltip` appears |
| trigger | `'manual' / 'click' / 'hover'` | `hover` | How `tooltip` are triggered |
| mouseEnterDelay | `number` | `200` | Delay in seconds, before `tooltip` is shown on mouse enter, unit `ms` |
| mouseLeaveDelay | `number` | `200` | Delay in seconds, before `tooltip` is shown on mouse enter, unit `ms` |
| defaultOpen | `boolean` | `-` | Open `tooltip` by default |
| cls | `string` | `-` | Additional class |
| attrs | `Record<string, string>` | `{}` | Additional attributes |
| content | `string` | `-` | `tooltip`'s content |
Expand Down
Loading