Propagate name and required from TileGroup.

Remove `name` from `RadioTile`.
This commit is contained in:
Harald Brunner 2022-01-05 14:19:41 +01:00
commit 7579028246
2 changed files with 19 additions and 5 deletions

View file

@ -20,13 +20,11 @@
/** Set an id for the input element */ /** Set an id for the input element */
export let id = "ccs-" + Math.random().toString(36); export let id = "ccs-" + Math.random().toString(36);
/** Specify a name attribute for the input */
export let name = "";
import { getContext } from "svelte"; import { getContext } from "svelte";
import CheckmarkFilled16 from "../icons/CheckmarkFilled16.svelte"; import CheckmarkFilled16 from "../icons/CheckmarkFilled16.svelte";
const { add, update, selectedValue } = getContext("TileGroup"); const { add, update, selectedValue, name, required } =
getContext("TileGroup");
add({ value, checked }); add({ value, checked });
@ -36,11 +34,12 @@
<input <input
type="radio" type="radio"
id="{id}" id="{id}"
name="{name}" name="{$name}"
value="{value}" value="{value}"
checked="{checked}" checked="{checked}"
tabindex="{disabled ? undefined : tabindex}" tabindex="{disabled ? undefined : tabindex}"
disabled="{disabled}" disabled="{disabled}"
required="{$required}"
class:bx--tile-input="{true}" class:bx--tile-input="{true}"
on:change on:change
on:change="{() => { on:change="{() => {

View file

@ -8,6 +8,15 @@
/** Set to `true` to disable the tile group */ /** Set to `true` to disable the tile group */
export let disabled = false; export let disabled = false;
/**
* Set to `true` to mark the selection as required.
* For validation to trigger, a `name` has to be specified as well.
*/
export let required = false;
/** Specify a name attribute for the radio inputs */
export let name = "";
/** Specify the legend text */ /** Specify the legend text */
export let legend = ""; export let legend = "";
@ -16,9 +25,13 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
const selectedValue = writable(selected); const selectedValue = writable(selected);
const inputsName = writable(name);
const inputsRequired = writable(required);
setContext("TileGroup", { setContext("TileGroup", {
selectedValue, selectedValue,
name: { subscribe: inputsName.subscribe },
required: { subscribe: inputsRequired.subscribe },
add: ({ checked, value }) => { add: ({ checked, value }) => {
if (checked) { if (checked) {
selectedValue.set(value); selectedValue.set(value);
@ -30,6 +43,8 @@
}); });
$: selected = $selectedValue; $: selected = $selectedValue;
$: inputsName.set(name);
$: inputsRequired.set(required);
$: dispatch("select", $selectedValue); $: dispatch("select", $selectedValue);
</script> </script>