File size: 6,070 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { addEventListener, addMeltEventListener, createElHelpers, disabledAttr, effect, executeCallbacks, getDirectionalKeys, getElemDirection, isHTMLElement, kbd, makeElement, omit, overridable, toWritableStores, } from '../../internal/helpers/index.js';
import { safeOnMount } from '../../internal/helpers/lifecycle.js';
import { derived, writable } from 'svelte/store';
import { createHiddenInput } from '../hidden-input/create.js';
const defaults = {
    orientation: 'vertical',
    loop: true,
    disabled: false,
    required: false,
    defaultValue: undefined,
};
const prefix = 'radio-group';
const { name, selector } = createElHelpers(prefix);
export function createRadioGroup(props) {
    const withDefaults = { ...defaults, ...props };
    // options
    const options = toWritableStores(omit(withDefaults, 'value'));
    const { disabled, required, loop, orientation } = options;
    const valueWritable = withDefaults.value ?? writable(withDefaults.defaultValue);
    const value = overridable(valueWritable, withDefaults?.onValueChange);
    /** Lifecycle & Effects */
    const focusedHistory = {
        prev: null,
        curr: null,
    };
    safeOnMount(() => {
        return addEventListener(document, 'focus', (e) => {
            const focusedItem = e.target;
            if (!isHTMLElement(focusedItem))
                return;
            focusedHistory.prev = focusedHistory.curr;
            focusedHistory.curr = focusedItem;
        });
    });
    let hasActiveTabIndex = false;
    effect(value, ($value) => {
        if ($value === undefined) {
            hasActiveTabIndex = false;
        }
        else {
            hasActiveTabIndex = true;
        }
    });
    /* Helpers */
    const selectItem = (item) => {
        const disabled = item.dataset.disabled === 'true';
        const itemValue = item.dataset.value;
        if (disabled || itemValue === undefined)
            return;
        value.set(itemValue);
    };
    /** Elements */
    const root = makeElement(name(), {
        stores: [required, orientation],
        returned: ([$required, $orientation]) => {
            return {
                role: 'radiogroup',
                'aria-required': $required,
                'data-orientation': $orientation,
            };
        },
    });
    const item = makeElement(name('item'), {
        stores: [value, orientation, disabled],
        returned: ([$value, $orientation, $disabled]) => {
            return (props) => {
                const itemValue = typeof props === 'string' ? props : props.value;
                const argDisabled = typeof props === 'string' ? false : !!props.disabled;
                const disabled = $disabled || argDisabled;
                const checked = $value === itemValue;
                const tabindex = !hasActiveTabIndex ? 0 : checked ? 0 : -1;
                hasActiveTabIndex = true;
                return {
                    disabled,
                    'data-value': itemValue,
                    'data-orientation': $orientation,
                    'data-disabled': disabledAttr(disabled),
                    'data-state': checked ? 'checked' : 'unchecked',
                    'aria-checked': checked,
                    type: 'button',
                    role: 'radio',
                    tabindex,
                };
            };
        },
        action: (node) => {
            const unsub = executeCallbacks(addMeltEventListener(node, 'click', () => {
                selectItem(node);
            }), addMeltEventListener(node, 'keydown', (e) => {
                const el = e.currentTarget;
                if (!isHTMLElement(el))
                    return;
                const root = el.closest(selector());
                if (!isHTMLElement(root))
                    return;
                const items = Array.from(root.querySelectorAll(selector('item'))).filter((el) => isHTMLElement(el) && !el.hasAttribute('data-disabled'));
                const currentIndex = items.indexOf(el);
                const dir = getElemDirection(root);
                const { nextKey, prevKey } = getDirectionalKeys(dir, orientation.get());
                const $loop = loop.get();
                let itemToFocus = null;
                if (e.key === nextKey) {
                    e.preventDefault();
                    const nextIndex = currentIndex + 1;
                    if (nextIndex >= items.length && $loop) {
                        itemToFocus = items[0];
                    }
                    else {
                        itemToFocus = items[nextIndex];
                    }
                }
                else if (e.key === prevKey) {
                    e.preventDefault();
                    const prevIndex = currentIndex - 1;
                    if (prevIndex < 0 && $loop) {
                        itemToFocus = items[items.length - 1];
                    }
                    else {
                        itemToFocus = items[prevIndex];
                    }
                }
                else if (e.key === kbd.HOME) {
                    e.preventDefault();
                    itemToFocus = items[0];
                }
                else if (e.key === kbd.END) {
                    e.preventDefault();
                    itemToFocus = items[items.length - 1];
                }
                if (itemToFocus) {
                    itemToFocus.focus();
                    selectItem(itemToFocus);
                }
            }));
            return {
                destroy: unsub,
            };
        },
    });
    const hiddenInput = createHiddenInput({
        value,
        disabled,
        required,
    });
    const isChecked = derived(value, ($value) => {
        return (itemValue) => {
            return $value === itemValue;
        };
    });
    return {
        elements: {
            root,
            item,
            hiddenInput,
        },
        states: {
            value,
        },
        helpers: {
            isChecked,
        },
        options,
    };
}