-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathuseToolbarRadioButton.ts
More file actions
72 lines (65 loc) · 2.47 KB
/
useToolbarRadioButton.ts
File metadata and controls
72 lines (65 loc) · 2.47 KB
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
'use client';
import type * as React from 'react';
import { useEventCallback } from '@fluentui/react-utilities';
import { useToggleButtonBase_unstable } from '@fluentui/react-button';
import { useToolbarContext_unstable } from '../Toolbar/ToolbarContext';
import type {
ToolbarRadioButtonProps,
ToolbarRadioButtonState,
ToolbarRadioButtonBaseProps,
ToolbarRadioButtonBaseState,
} from './ToolbarRadioButton.types';
/**
* Given user props, defines default props for the RadioButton, calls useButtonState and useChecked, and returns
* processed state.
* @param props - User provided props to the RadioButton component.
* @param ref - User provided ref to be passed to the RadioButton component.
*/
export const useToolbarRadioButton_unstable = (
props: ToolbarRadioButtonProps,
ref: React.Ref<HTMLButtonElement | HTMLAnchorElement>,
): ToolbarRadioButtonState => {
const { appearance = 'secondary', size, ...baseProps } = props;
const contextSize = useToolbarContext_unstable(ctx => ctx.size);
const state = useToolbarRadioButtonBase_unstable(baseProps, ref);
return {
...state,
appearance,
size: size || contextSize,
shape: 'rounded',
};
};
/**
* Base hook that builds Toolbar RadioButton state for behavior and structure only.
* It does not provide any design-related defaults.
*
* @internal
* @param props - User provided props to the RadioButton component.
* @param ref - User provided ref to be passed to the RadioButton component.
*/
export const useToolbarRadioButtonBase_unstable = (
props: ToolbarRadioButtonBaseProps,
ref: React.Ref<HTMLButtonElement | HTMLAnchorElement>,
): ToolbarRadioButtonBaseState => {
const handleRadio = useToolbarContext_unstable(ctx => ctx.handleRadio);
const checked = useToolbarContext_unstable(ctx => !!ctx.checkedValues[props.name]?.includes(props.value));
const { onClick: onClickOriginal } = props;
const toggleButtonState = useToggleButtonBase_unstable(
{ checked, role: 'radio', 'aria-checked': checked, ...props },
ref,
);
const state: ToolbarRadioButtonBaseState = {
...toggleButtonState,
name: props.name,
value: props.value,
};
const handleOnClick = useEventCallback(
(e: React.MouseEvent<HTMLButtonElement, MouseEvent> & React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
handleRadio?.(e, state.name, state.value, state.checked);
onClickOriginal?.(e);
},
);
state.root['aria-pressed'] = undefined;
state.root.onClick = handleOnClick;
return state;
};