mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 19:46:36 +00:00
feat(select): support number type
This commit is contained in:
parent
f8b1c8a23a
commit
12045756ca
2 changed files with 23 additions and 8 deletions
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify the selected item value
|
* Specify the selected item value
|
||||||
* @type {string}
|
* @type {string | number}
|
||||||
*/
|
*/
|
||||||
export let selected = undefined;
|
export let selected = undefined;
|
||||||
|
|
||||||
|
@ -73,6 +73,7 @@
|
||||||
const selectedValue = writable(selected);
|
const selectedValue = writable(selected);
|
||||||
const defaultSelectId = writable(null);
|
const defaultSelectId = writable(null);
|
||||||
const defaultValue = writable(null);
|
const defaultValue = writable(null);
|
||||||
|
const itemTypesByValue = writable({});
|
||||||
|
|
||||||
setContext("Select", {
|
setContext("Select", {
|
||||||
selectedValue,
|
selectedValue,
|
||||||
|
@ -89,9 +90,24 @@
|
||||||
selectedValue.set(value);
|
selectedValue.set(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itemTypesByValue.update((types) => ({
|
||||||
|
...types,
|
||||||
|
[value]: typeof value,
|
||||||
|
}));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const handleChange = ({ target }) => {
|
||||||
|
let value = target.value;
|
||||||
|
|
||||||
|
if ($itemTypesByValue[value] === "number") {
|
||||||
|
value = Number(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedValue.set(value);
|
||||||
|
};
|
||||||
|
|
||||||
afterUpdate(() => {
|
afterUpdate(() => {
|
||||||
selected = $selectedValue;
|
selected = $selectedValue;
|
||||||
dispatch("change", $selectedValue);
|
dispatch("change", $selectedValue);
|
||||||
|
@ -139,9 +155,7 @@
|
||||||
class:bx--select-input="{true}"
|
class:bx--select-input="{true}"
|
||||||
class:bx--select-input--sm="{size === 'sm'}"
|
class:bx--select-input--sm="{size === 'sm'}"
|
||||||
class:bx--select-input--xl="{size === 'xl'}"
|
class:bx--select-input--xl="{size === 'xl'}"
|
||||||
on:change="{({ target }) => {
|
on:change="{handleChange}"
|
||||||
selectedValue.set(target.value);
|
|
||||||
}}"
|
|
||||||
on:input
|
on:input
|
||||||
on:focus
|
on:focus
|
||||||
on:blur
|
on:blur
|
||||||
|
@ -184,9 +198,7 @@
|
||||||
class:bx--select-input="{true}"
|
class:bx--select-input="{true}"
|
||||||
class:bx--select-input--sm="{size === 'sm'}"
|
class:bx--select-input--sm="{size === 'sm'}"
|
||||||
class:bx--select-input--xl="{size === 'xl'}"
|
class:bx--select-input--xl="{size === 'xl'}"
|
||||||
on:change="{({ target }) => {
|
on:change="{handleChange}"
|
||||||
selectedValue.set(target.value);
|
|
||||||
}}"
|
|
||||||
on:input
|
on:input
|
||||||
on:focus
|
on:focus
|
||||||
on:blur
|
on:blur
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
<script>
|
<script>
|
||||||
/** Specify the option value */
|
/**
|
||||||
|
* Specify the option value
|
||||||
|
* @type {string | number}
|
||||||
|
*/
|
||||||
export let value = "";
|
export let value = "";
|
||||||
|
|
||||||
/** Specify the option text */
|
/** Specify the option text */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue