breaking(text-input): dispatch instead of forward input, change events

This commit is contained in:
Eric Liu 2022-02-02 20:06:26 -08:00
commit a15c1f5471

View file

@ -1,4 +1,9 @@
<script> <script>
/**
* @event {null | number | string} change
* @event {null | number | string} input
*/
/** /**
* Set the size of the input * Set the size of the input
* @type {"sm" | "xl"} * @type {"sm" | "xl"}
@ -65,19 +70,33 @@
/** Set to `true` to use the read-only variant */ /** Set to `true` to use the read-only variant */
export let readonly = false; export let readonly = false;
import { getContext } from "svelte"; import { createEventDispatcher, getContext } from "svelte";
import WarningFilled16 from "../icons/WarningFilled16.svelte"; import WarningFilled16 from "../icons/WarningFilled16.svelte";
import WarningAltFilled16 from "../icons/WarningAltFilled16.svelte"; import WarningAltFilled16 from "../icons/WarningAltFilled16.svelte";
import EditOff16 from "../icons/EditOff16.svelte"; import EditOff16 from "../icons/EditOff16.svelte";
const ctx = getContext("Form"); const ctx = getContext("Form");
const dispatch = createEventDispatcher();
function parse(raw) {
if ($$restProps.type !== "number") return raw;
return raw != "" ? Number(raw) : null;
}
/** @type {(e: Event) => void} */
const onInput = (e) => {
value = parse(e.target.value);
dispatch("input", value);
};
/** @type {(e: Event) => void} */
const onChange = (e) => {
dispatch("change", parse(e.target.value));
};
$: isFluid = !!ctx && ctx.isFluid; $: isFluid = !!ctx && ctx.isFluid;
$: errorId = `error-${id}`; $: errorId = `error-${id}`;
$: warnId = `warn-${id}`; $: warnId = `warn-${id}`;
$: if ($$restProps.type === "number") {
value = value !== "" && value !== null ? Number(value) : null;
}
</script> </script>
<!-- svelte-ignore a11y-mouse-events-have-key-events --> <!-- svelte-ignore a11y-mouse-events-have-key-events -->
@ -174,8 +193,8 @@
class:bx--text-input--warn="{warn}" class:bx--text-input--warn="{warn}"
{...$$restProps} {...$$restProps}
class="{size && `bx--text-input--${size}`}" class="{size && `bx--text-input--${size}`}"
on:change on:change="{onChange}"
on:input on:input="{onInput}"
on:keydown on:keydown
on:keyup on:keyup
on:focus on:focus