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: added tooltip format for Slider Component #395

Merged
merged 8 commits into from
Jan 8, 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
25 changes: 25 additions & 0 deletions components/Slider/__test__/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ describe('Test: KSlider', () => {
expect(host.innerHTML).matchSnapshot();
});

test('props: format', async () => {
const instance = new KSlider({
target: host,
props: {
value: 10,
format: (value) => {
return 'Ikun' + value;
}
}
});
expect(instance).toBeTruthy();
await tick();
const sliderButtonWrapperElm = host.querySelector('.k-slider--button-wrapper') as HTMLElement;
const triggerElm = sliderButtonWrapperElm.children[0];
triggerElm.dispatchEvent(
new MouseEvent('mouseenter', {
cancelable: true
})
);
vi.runAllTimers();
await tick();
expect(host.innerHTML.includes('data-popper-arrow-bottom')).toBeTruthy();
expect(host.innerHTML.includes('Ikun10')).toBeTruthy();
});

test('props: vertical', async () => {
const instance = new KSlider({
target: host,
Expand Down
19 changes: 17 additions & 2 deletions components/Slider/src/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
export let attrs: KSliderProps['attrs'] = {};
export let cls: KSliderProps['cls'] = undefined;
export let showTooltip: KSliderProps['showTooltip'] = true;
export let format: KSliderProps['format'];

/*********************** KForm logic start ************************/
let disabledFrom = false;
Expand Down Expand Up @@ -52,6 +53,19 @@
}
/*********************** KForm logic end ************************/

function handleFormat(value: number) {
if (format) {
const formattedValue = format(value);
if (typeof formattedValue === 'number' || typeof formattedValue === 'string') {
return formattedValue;
} else {
return value;
}
} else {
return value;
}
}

// current value
let isDragging: boolean = false;
let startX: number = 0;
Expand All @@ -67,6 +81,7 @@
$: percentage = `${((value - min) / (max - min)) * 100}%`;
$: barStyle = vertical ? `height: ${percentage}; bottom: 0%` : `width: ${percentage}; left: 0%`;
$: btnStyle = vertical ? `bottom: ${percentage}` : `left: ${percentage}`;
$: formatContent = String(handleFormat(value));

// element
let runwayRef: null | HTMLElement = null;
Expand Down Expand Up @@ -194,13 +209,13 @@
style={btnStyle}
>
{#if $$slots.buttonRender}
<KTooltip disabled={!showTooltip} placement="top" content={String(value)}>
<KTooltip disabled={!showTooltip} placement="top" content={formatContent}>
<div slot="triggerEl" class={buttonCls}>
<slot name="buttonRender" />
</div>
</KTooltip>
{:else}
<KTooltip disabled={!showTooltip} placement="top" content={String(value)}>
<KTooltip disabled={!showTooltip} placement="top" content={formatContent}>
<div slot="triggerEl" class={buttonCls}></div>
</KTooltip>
{/if}
Expand Down
1 change: 1 addition & 0 deletions components/Slider/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type KSliderProps = {
disabled: boolean;
vertical: boolean;
showTooltip: boolean;
format: (value: number) => number | string;
cls: ClassValue;
attrs: Record<string, string>;
};
23 changes: 12 additions & 11 deletions docs/components/KSlider.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ Add `size` attribute to change the size of Slider. It supports `sm`, `md` and `l

## Slider Props

| Name | Type | Default | Description |
| -------- | ------------------------ | ------- | -------------------------- |
| value | `number` | `0` | Binding value |
| vertical | `boolean` | `false` | Vertical mode |
| min | `number` | `0` | Minimum value |
| max | `number` | `100` | Maximum value |
| step | `number` | `1` | Step size |
| disabled | `boolean` | `false` | Whether Slider is disabled |
| size | `sm \| md \| lg` | `md` | Size of `slider`. |
| cls | `string` | `-` | Additional class |
| attrs | `Record<string, string>` | `{}` | Additional attributes |
| Name | Type | Default | Description |
| -------- | ------------------------------------- | -------------------------- | -------------------------- |
| value | `number` | `0` | Binding value |
| vertical | `boolean` | `false` | Vertical mode |
| format | `(value: number) => number \| string` | `(value: number) => value` | Tooltip format value |
| min | `number` | `0` | Minimum value |
| max | `number` | `100` | Maximum value |
| step | `number` | `1` | Step size |
| disabled | `boolean` | `false` | Whether Slider is disabled |
| size | `sm \| md \| lg` | `md` | Size of `slider`. |
| cls | `string` | `-` | Additional class |
| attrs | `Record<string, string>` | `{}` | Additional attributes |

## Slider Events

Expand Down
8 changes: 8 additions & 0 deletions docs/example/slider/basic.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
let step = 10;
let value1 = 0;
let value2 = 0;
let format = (value) => {
return 'Ikun' + value;
};
const handleInput1 = (event) => {
value1 = event.detail;
};
Expand All @@ -17,6 +20,11 @@
<p class="px-2">value: {value1}</p>
</div>

<div>
<KSlider {step} on:input={handleInput2} {format} value={value2}></KSlider>
<p class="px-2">format: {"(value) => {return 'Ikun'+value}"}</p>
</div>

<div>
<KSlider {step} on:input={handleInput2} value={value2}></KSlider>
<p class="px-2">value: {value2}</p>
Expand Down