This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
<script lang="ts">
|
||||
import HiddenInput from "../../utilities/hidden-input.svelte";
|
||||
import { RadioGroupInputState } from "../radio-group.svelte.js";
|
||||
|
||||
const inputState = RadioGroupInputState.create();
|
||||
</script>
|
||||
|
||||
{#if inputState.shouldRender}
|
||||
<HiddenInput {...inputState.props} />
|
||||
{/if}
|
||||
Generated
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
||||
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
||||
$$bindings?: Bindings;
|
||||
} & Exports;
|
||||
(internal: unknown, props: {
|
||||
$$events?: Events;
|
||||
$$slots?: Slots;
|
||||
}): Exports & {
|
||||
$set?: any;
|
||||
$on?: any;
|
||||
};
|
||||
z_$$bindings?: Bindings;
|
||||
}
|
||||
declare const RadioGroupInput: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
||||
[evt: string]: CustomEvent<any>;
|
||||
}, {}, {}, string>;
|
||||
type RadioGroupInput = InstanceType<typeof RadioGroupInput>;
|
||||
export default RadioGroupInput;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<script lang="ts">
|
||||
import { boxWith, mergeProps } from "svelte-toolbelt";
|
||||
import type { RadioGroupItemProps } from "../types.js";
|
||||
import { RadioGroupItemState } from "../radio-group.svelte.js";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
id = createId(uid),
|
||||
children,
|
||||
child,
|
||||
value,
|
||||
disabled = false,
|
||||
ref = $bindable(null),
|
||||
...restProps
|
||||
}: RadioGroupItemProps = $props();
|
||||
|
||||
const itemState = RadioGroupItemState.create({
|
||||
value: boxWith(() => value),
|
||||
disabled: boxWith(() => disabled ?? false),
|
||||
id: boxWith(() => id),
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, itemState.props));
|
||||
</script>
|
||||
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps, ...itemState.snippetProps })}
|
||||
{:else}
|
||||
<button {...mergedProps}>
|
||||
{@render children?.(itemState.snippetProps)}
|
||||
</button>
|
||||
{/if}
|
||||
Generated
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
import type { RadioGroupItemProps } from "../types.js";
|
||||
declare const RadioGroupItem: import("svelte").Component<RadioGroupItemProps, {}, "ref">;
|
||||
type RadioGroupItem = ReturnType<typeof RadioGroupItem>;
|
||||
export default RadioGroupItem;
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<script lang="ts">
|
||||
import { boxWith, mergeProps } from "svelte-toolbelt";
|
||||
import type { RadioGroupRootProps } from "../types.js";
|
||||
import { RadioGroupRootState } from "../radio-group.svelte.js";
|
||||
import RadioGroupInput from "./radio-group-input.svelte";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
import { noop } from "../../../internal/noop.js";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
disabled = false,
|
||||
children,
|
||||
child,
|
||||
value = $bindable(""),
|
||||
ref = $bindable(null),
|
||||
orientation = "vertical",
|
||||
loop = true,
|
||||
name = undefined,
|
||||
required = false,
|
||||
readonly = false,
|
||||
id = createId(uid),
|
||||
onValueChange = noop,
|
||||
...restProps
|
||||
}: RadioGroupRootProps = $props();
|
||||
|
||||
const rootState = RadioGroupRootState.create({
|
||||
orientation: boxWith(() => orientation),
|
||||
disabled: boxWith(() => disabled),
|
||||
loop: boxWith(() => loop),
|
||||
name: boxWith(() => name),
|
||||
required: boxWith(() => required),
|
||||
readonly: boxWith(() => readonly),
|
||||
id: boxWith(() => id),
|
||||
value: boxWith(
|
||||
() => value,
|
||||
(v) => {
|
||||
if (v === value) return;
|
||||
value = v;
|
||||
onValueChange?.(v);
|
||||
}
|
||||
),
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, rootState.props));
|
||||
</script>
|
||||
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps })}
|
||||
{:else}
|
||||
<div {...mergedProps}>
|
||||
{@render children?.()}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<RadioGroupInput />
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { RadioGroupRootProps } from "../types.js";
|
||||
declare const RadioGroup: import("svelte").Component<RadioGroupRootProps, {}, "value" | "ref">;
|
||||
type RadioGroup = ReturnType<typeof RadioGroup>;
|
||||
export default RadioGroup;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { default as Root } from "./components/radio-group.svelte";
|
||||
export { default as Item } from "./components/radio-group-item.svelte";
|
||||
export type { RadioGroupRootProps as RootProps, RadioGroupItemProps as ItemProps, } from "./types.js";
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { default as Root } from "./components/radio-group.svelte";
|
||||
export { default as Item } from "./components/radio-group-item.svelte";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as RadioGroup from "./exports.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as RadioGroup from "./exports.js";
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
import { type ReadableBoxedValues, type WritableBoxedValues } from "svelte-toolbelt";
|
||||
import type { BitsFocusEvent, BitsKeyboardEvent, BitsMouseEvent, RefAttachment, WithRefOpts } from "../../internal/types.js";
|
||||
import type { Orientation } from "../../shared/index.js";
|
||||
import { RovingFocusGroup } from "../../internal/roving-focus-group.js";
|
||||
interface RadioGroupRootStateOpts extends WithRefOpts, ReadableBoxedValues<{
|
||||
disabled: boolean;
|
||||
required: boolean;
|
||||
loop: boolean;
|
||||
orientation: Orientation;
|
||||
name: string | undefined;
|
||||
readonly: boolean;
|
||||
}>, WritableBoxedValues<{
|
||||
value: string;
|
||||
}> {
|
||||
}
|
||||
export declare class RadioGroupRootState {
|
||||
static create(opts: RadioGroupRootStateOpts): RadioGroupRootState;
|
||||
readonly opts: RadioGroupRootStateOpts;
|
||||
readonly hasValue: boolean;
|
||||
readonly rovingFocusGroup: RovingFocusGroup;
|
||||
readonly attachment: RefAttachment;
|
||||
constructor(opts: RadioGroupRootStateOpts);
|
||||
isChecked(value: string): boolean;
|
||||
setValue(value: string): void;
|
||||
readonly props: {
|
||||
readonly id: string;
|
||||
readonly role: "radiogroup";
|
||||
readonly "aria-required": "true" | "false";
|
||||
readonly "aria-disabled": "true" | "false";
|
||||
readonly "aria-readonly": "true" | undefined;
|
||||
readonly "data-disabled": "" | undefined;
|
||||
readonly "data-readonly": "" | undefined;
|
||||
readonly "data-orientation": Orientation;
|
||||
};
|
||||
}
|
||||
interface RadioGroupItemStateOpts extends WithRefOpts, ReadableBoxedValues<{
|
||||
disabled: boolean;
|
||||
value: string;
|
||||
}> {
|
||||
}
|
||||
export declare class RadioGroupItemState {
|
||||
#private;
|
||||
static create(opts: RadioGroupItemStateOpts): RadioGroupItemState;
|
||||
readonly opts: RadioGroupItemStateOpts;
|
||||
readonly root: RadioGroupRootState;
|
||||
readonly attachment: RefAttachment;
|
||||
readonly checked: boolean;
|
||||
constructor(opts: RadioGroupItemStateOpts, root: RadioGroupRootState);
|
||||
onclick(_: BitsMouseEvent): void;
|
||||
onfocus(_: BitsFocusEvent): void;
|
||||
onkeydown(e: BitsKeyboardEvent): void;
|
||||
readonly snippetProps: {
|
||||
checked: boolean;
|
||||
};
|
||||
readonly props: {
|
||||
readonly id: string;
|
||||
readonly disabled: true | undefined;
|
||||
readonly "data-value": string;
|
||||
readonly "data-orientation": Orientation;
|
||||
readonly "data-disabled": "" | undefined;
|
||||
readonly "data-readonly": "" | undefined;
|
||||
readonly "data-state": "checked" | "unchecked";
|
||||
readonly "aria-checked": "true" | "false" | "mixed";
|
||||
readonly type: "button";
|
||||
readonly role: "radio";
|
||||
readonly tabindex: number;
|
||||
readonly onkeydown: (e: BitsKeyboardEvent) => void;
|
||||
readonly onfocus: (_: BitsFocusEvent) => void;
|
||||
readonly onclick: (_: BitsMouseEvent) => void;
|
||||
};
|
||||
}
|
||||
export declare class RadioGroupInputState {
|
||||
static create(): RadioGroupInputState;
|
||||
readonly root: RadioGroupRootState;
|
||||
readonly shouldRender: boolean;
|
||||
constructor(root: RadioGroupRootState);
|
||||
onfocus(_: BitsFocusEvent): void;
|
||||
readonly props: {
|
||||
readonly name: string | undefined;
|
||||
readonly value: string;
|
||||
readonly required: boolean;
|
||||
readonly disabled: boolean;
|
||||
readonly onfocus: (_: BitsFocusEvent) => void;
|
||||
};
|
||||
}
|
||||
export {};
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
import { attachRef } from "svelte-toolbelt";
|
||||
import { Context, watch } from "runed";
|
||||
import { createBitsAttrs, getAriaChecked, boolToEmptyStrOrUndef, boolToStr, } from "../../internal/attrs.js";
|
||||
import { kbd } from "../../internal/kbd.js";
|
||||
import { RovingFocusGroup } from "../../internal/roving-focus-group.js";
|
||||
const radioGroupAttrs = createBitsAttrs({
|
||||
component: "radio-group",
|
||||
parts: ["root", "item"],
|
||||
});
|
||||
const RadioGroupRootContext = new Context("RadioGroup.Root");
|
||||
export class RadioGroupRootState {
|
||||
static create(opts) {
|
||||
return RadioGroupRootContext.set(new RadioGroupRootState(opts));
|
||||
}
|
||||
opts;
|
||||
hasValue = $derived.by(() => this.opts.value.current !== "");
|
||||
rovingFocusGroup;
|
||||
attachment;
|
||||
constructor(opts) {
|
||||
this.opts = opts;
|
||||
this.attachment = attachRef(this.opts.ref);
|
||||
this.rovingFocusGroup = new RovingFocusGroup({
|
||||
rootNode: this.opts.ref,
|
||||
candidateAttr: radioGroupAttrs.item,
|
||||
loop: this.opts.loop,
|
||||
orientation: this.opts.orientation,
|
||||
});
|
||||
}
|
||||
isChecked(value) {
|
||||
return this.opts.value.current === value;
|
||||
}
|
||||
setValue(value) {
|
||||
this.opts.value.current = value;
|
||||
}
|
||||
props = $derived.by(() => ({
|
||||
id: this.opts.id.current,
|
||||
role: "radiogroup",
|
||||
"aria-required": boolToStr(this.opts.required.current),
|
||||
"aria-disabled": boolToStr(this.opts.disabled.current),
|
||||
"aria-readonly": this.opts.readonly.current ? "true" : undefined,
|
||||
"data-disabled": boolToEmptyStrOrUndef(this.opts.disabled.current),
|
||||
"data-readonly": boolToEmptyStrOrUndef(this.opts.readonly.current),
|
||||
"data-orientation": this.opts.orientation.current,
|
||||
[radioGroupAttrs.root]: "",
|
||||
...this.attachment,
|
||||
}));
|
||||
}
|
||||
export class RadioGroupItemState {
|
||||
static create(opts) {
|
||||
return new RadioGroupItemState(opts, RadioGroupRootContext.get());
|
||||
}
|
||||
opts;
|
||||
root;
|
||||
attachment;
|
||||
checked = $derived.by(() => this.root.opts.value.current === this.opts.value.current);
|
||||
#isDisabled = $derived.by(() => this.opts.disabled.current || this.root.opts.disabled.current);
|
||||
#isReadonly = $derived.by(() => this.root.opts.readonly.current);
|
||||
#isChecked = $derived.by(() => this.root.isChecked(this.opts.value.current));
|
||||
#tabIndex = $state(-1);
|
||||
constructor(opts, root) {
|
||||
this.opts = opts;
|
||||
this.root = root;
|
||||
this.attachment = attachRef(this.opts.ref);
|
||||
if (this.opts.value.current === this.root.opts.value.current) {
|
||||
this.root.rovingFocusGroup.setCurrentTabStopId(this.opts.id.current);
|
||||
this.#tabIndex = 0;
|
||||
}
|
||||
else if (!this.root.opts.value.current) {
|
||||
this.#tabIndex = 0;
|
||||
}
|
||||
$effect(() => {
|
||||
this.#tabIndex = this.root.rovingFocusGroup.getTabIndex(this.opts.ref.current);
|
||||
});
|
||||
watch([() => this.opts.value.current, () => this.root.opts.value.current], () => {
|
||||
if (this.opts.value.current === this.root.opts.value.current) {
|
||||
this.root.rovingFocusGroup.setCurrentTabStopId(this.opts.id.current);
|
||||
this.#tabIndex = 0;
|
||||
}
|
||||
});
|
||||
this.onclick = this.onclick.bind(this);
|
||||
this.onkeydown = this.onkeydown.bind(this);
|
||||
this.onfocus = this.onfocus.bind(this);
|
||||
}
|
||||
onclick(_) {
|
||||
if (this.opts.disabled.current || this.#isReadonly)
|
||||
return;
|
||||
this.root.setValue(this.opts.value.current);
|
||||
}
|
||||
onfocus(_) {
|
||||
if (!this.root.hasValue || this.#isReadonly)
|
||||
return;
|
||||
this.root.setValue(this.opts.value.current);
|
||||
}
|
||||
onkeydown(e) {
|
||||
if (this.#isDisabled)
|
||||
return;
|
||||
if (e.key === kbd.SPACE) {
|
||||
e.preventDefault();
|
||||
if (!this.#isReadonly) {
|
||||
this.root.setValue(this.opts.value.current);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.root.rovingFocusGroup.handleKeydown(this.opts.ref.current, e, true);
|
||||
}
|
||||
snippetProps = $derived.by(() => ({ checked: this.#isChecked }));
|
||||
props = $derived.by(() => ({
|
||||
id: this.opts.id.current,
|
||||
disabled: this.#isDisabled ? true : undefined,
|
||||
"data-value": this.opts.value.current,
|
||||
"data-orientation": this.root.opts.orientation.current,
|
||||
"data-disabled": boolToEmptyStrOrUndef(this.#isDisabled),
|
||||
"data-readonly": boolToEmptyStrOrUndef(this.#isReadonly),
|
||||
"data-state": this.#isChecked ? "checked" : "unchecked",
|
||||
"aria-checked": getAriaChecked(this.#isChecked, false),
|
||||
[radioGroupAttrs.item]: "",
|
||||
type: "button",
|
||||
role: "radio",
|
||||
tabindex: this.#tabIndex,
|
||||
//
|
||||
onkeydown: this.onkeydown,
|
||||
onfocus: this.onfocus,
|
||||
onclick: this.onclick,
|
||||
...this.attachment,
|
||||
}));
|
||||
}
|
||||
export class RadioGroupInputState {
|
||||
static create() {
|
||||
return new RadioGroupInputState(RadioGroupRootContext.get());
|
||||
}
|
||||
root;
|
||||
shouldRender = $derived.by(() => this.root.opts.name.current !== undefined);
|
||||
constructor(root) {
|
||||
this.root = root;
|
||||
this.onfocus = this.onfocus.bind(this);
|
||||
}
|
||||
onfocus(_) {
|
||||
this.root.rovingFocusGroup.focusCurrentTabStop();
|
||||
}
|
||||
props = $derived.by(() => ({
|
||||
name: this.root.opts.name.current,
|
||||
value: this.root.opts.value.current,
|
||||
required: this.root.opts.required.current,
|
||||
disabled: this.root.opts.disabled.current,
|
||||
onfocus: this.onfocus,
|
||||
}));
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
import type { OnChangeFn, WithChild, Without } from "../../internal/types.js";
|
||||
import type { Orientation } from "../../index.js";
|
||||
import type { BitsPrimitiveButtonAttributes, BitsPrimitiveDivAttributes } from "../../shared/attributes.js";
|
||||
export type RadioGroupRootPropsWithoutHTML = WithChild<{
|
||||
/**
|
||||
* The orientation of the radio group. Used to determine
|
||||
* how keyboard navigation should work.
|
||||
*
|
||||
* @default "vertical"
|
||||
*/
|
||||
orientation?: Orientation;
|
||||
/**
|
||||
* Whether to loop around the radio items when navigating
|
||||
* with the keyboard.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
loop?: boolean;
|
||||
/**
|
||||
* The value of the selected radio item.
|
||||
*
|
||||
* @default ""
|
||||
*/
|
||||
value?: string;
|
||||
/**
|
||||
* The callback to call when the selected radio item changes.
|
||||
*/
|
||||
onValueChange?: OnChangeFn<string>;
|
||||
/**
|
||||
* The name to apply to the radio group's input element for
|
||||
* form submission. If not provided, a hidden input will not
|
||||
* be rendered and the radio group will not be part of a form.
|
||||
*
|
||||
* @default undefined
|
||||
*/
|
||||
name?: string;
|
||||
/**
|
||||
* Whether the radio group is disabled.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
disabled?: boolean;
|
||||
/**
|
||||
* Whether the radio group is required for form submission.
|
||||
* If `true`, ensure you provide a `name` prop so the hidden
|
||||
* input is rendered.
|
||||
*/
|
||||
required?: boolean;
|
||||
/**
|
||||
* Whether the radio group is readonly. When readonly, users can
|
||||
* focus and navigate through items but cannot change the value.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
readonly?: boolean;
|
||||
}>;
|
||||
export type RadioGroupRootProps = RadioGroupRootPropsWithoutHTML & Without<BitsPrimitiveDivAttributes, RadioGroupRootPropsWithoutHTML>;
|
||||
export type RadioGroupItemSnippetProps = {
|
||||
checked: boolean;
|
||||
};
|
||||
export type RadioGroupItemPropsWithoutHTML = WithChild<{
|
||||
/**
|
||||
* The value of the radio item.
|
||||
*/
|
||||
value: string;
|
||||
/**
|
||||
* Whether the radio item is disabled.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
disabled?: boolean | null | undefined;
|
||||
}, RadioGroupItemSnippetProps>;
|
||||
export type RadioGroupItemProps = RadioGroupItemPropsWithoutHTML & Without<BitsPrimitiveButtonAttributes, RadioGroupItemPropsWithoutHTML>;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in New Issue
Block a user