Skip to content

Commit 80b996b

Browse files
committed
fix(switch): add activeValue and inactiveValue
1 parent 6d6f2c6 commit 80b996b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/switch/src/lib/switch.svelte

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { SvelIcon, Loading } from '@svelement-ui/icon';
33
import a2s from '@svelement-ui/util-array-2-class-string';
44
import a2st from '@svelement-ui/util-array-2-style-string';
5-
import { getContext, createEventDispatcher, tick } from 'svelte';
5+
import { getContext, createEventDispatcher, tick, onMount } from 'svelte';
66
77
const dispatch = createEventDispatcher();
88
@@ -24,7 +24,6 @@
2424
/** @type {String, Number} */
2525
export let tabindex;
2626
export let beforeChange = null;
27-
2827
$: size = size || getContext('svel-size');
2928
$: isControlled = value !== false;
3029
$: actualValue = value;
@@ -61,6 +60,12 @@
6160
}
6261
}
6362
63+
if (actualValue !== activeValue && actualValue !== inactiveValue) {
64+
value = inactiveValue;
65+
dispatch('change', value);
66+
dispatch('input', value);
67+
}
68+
6469
let input;
6570
6671
function handleChange() {
@@ -72,6 +77,10 @@
7277
input.checked = checked;
7378
});
7479
}
80+
81+
function handleInputKeydown({ key }) {
82+
console.log(key);
83+
}
7584
</script>
7685

7786
<div class={switchKls} on:click={switchValue} {style}>
@@ -83,6 +92,8 @@
8392
disabled={switchDisabled}
8493
false-value={inactiveValue}
8594
{name}
95+
on:change={handleChange}
96+
on:keydown={handleInputKeydown}
8697
role="switch"
8798
{tabindex}
8899
true-value={activeValue}

packages/switch/src/routes/+page.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import SvelSwitch from '$lib/index.js';
33
44
let value;
5+
let value2;
56
</script>
67

78
{value}
@@ -16,3 +17,8 @@
1617

1718
<div />
1819
<SvelSwitch activeText="Pay by month" inactiveText="Pay by year" />
20+
21+
<div />
22+
23+
<SvelSwitch activeValue="100" bind:value={value2} inactiveValue="0" />
24+
{value2}

0 commit comments

Comments
 (0)