1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ToggleBase = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var React = require("react");
|
6 | var react_hooks_1 = require("@fluentui/react-hooks");
|
7 | var utilities_1 = require("@fluentui/utilities");
|
8 | var Label_1 = require("../Label/Label");
|
9 | var getClassNames = utilities_1.classNamesFunction();
|
10 | var COMPONENT_NAME = 'Toggle';
|
11 | exports.ToggleBase = React.forwardRef(function (props, forwardedRef) {
|
12 | var _a = props.as, RootType = _a === void 0 ? 'div' : _a, ariaLabel = props.ariaLabel, controlledChecked = props.checked, className = props.className, _b = props.defaultChecked, defaultChecked = _b === void 0 ? false : _b, disabled = props.disabled, inlineLabel = props.inlineLabel, label = props.label,
|
13 |
|
14 | offAriaLabel = props.offAriaLabel, offText = props.offText,
|
15 |
|
16 | onAriaLabel = props.onAriaLabel, onChange = props.onChange,
|
17 |
|
18 | onChanged = props.onChanged, onToggleClick = props.onClick, onText = props.onText, role = props.role, styles = props.styles, theme = props.theme;
|
19 | var _c = react_hooks_1.useControllableValue(controlledChecked, defaultChecked, React.useCallback(function (ev, isChecked) {
|
20 | onChange === null || onChange === void 0 ? void 0 : onChange(ev, isChecked);
|
21 | onChanged === null || onChanged === void 0 ? void 0 : onChanged(isChecked);
|
22 | }, [onChange, onChanged])), checked = _c[0], setChecked = _c[1];
|
23 | var classNames = getClassNames(styles, {
|
24 | theme: theme,
|
25 | className: className,
|
26 | disabled: disabled,
|
27 | checked: checked,
|
28 | inlineLabel: inlineLabel,
|
29 | onOffMissing: !onText && !offText,
|
30 | });
|
31 | var badAriaLabel = checked ? onAriaLabel : offAriaLabel;
|
32 | var id = react_hooks_1.useId(COMPONENT_NAME, props.id);
|
33 | var labelId = id + "-label";
|
34 | var stateTextId = id + "-stateText";
|
35 | var stateText = checked ? onText : offText;
|
36 | var toggleNativeProps = utilities_1.getNativeProps(props, utilities_1.inputProperties, [
|
37 | 'defaultChecked',
|
38 | ]);
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | var labelledById = undefined;
|
44 | if (!ariaLabel && !badAriaLabel) {
|
45 | if (label) {
|
46 | labelledById = labelId;
|
47 | }
|
48 | if (stateText && !labelledById) {
|
49 | labelledById = stateTextId;
|
50 | }
|
51 | }
|
52 | var toggleButton = React.useRef(null);
|
53 | utilities_1.useFocusRects(toggleButton);
|
54 | useComponentRef(props, checked, toggleButton);
|
55 | if (process.env.NODE_ENV !== 'production') {
|
56 |
|
57 | react_hooks_1.useWarnings({
|
58 | name: COMPONENT_NAME,
|
59 | props: props,
|
60 | deprecations: {
|
61 | offAriaLabel: undefined,
|
62 | onAriaLabel: 'ariaLabel',
|
63 | onChanged: 'onChange',
|
64 | },
|
65 | mutuallyExclusive: { checked: 'defaultChecked' },
|
66 | });
|
67 | }
|
68 | var onClick = function (ev) {
|
69 | if (!disabled) {
|
70 | setChecked(!checked, ev);
|
71 | if (onToggleClick) {
|
72 | onToggleClick(ev);
|
73 | }
|
74 | }
|
75 | };
|
76 | var slotProps = {
|
77 | root: {
|
78 | className: classNames.root,
|
79 | hidden: toggleNativeProps.hidden,
|
80 | },
|
81 | label: {
|
82 | children: label,
|
83 | className: classNames.label,
|
84 | htmlFor: id,
|
85 | id: labelId,
|
86 | },
|
87 | container: {
|
88 | className: classNames.container,
|
89 | },
|
90 | pill: tslib_1.__assign(tslib_1.__assign({}, toggleNativeProps), { 'aria-disabled': disabled, 'aria-checked': checked, 'aria-label': ariaLabel ? ariaLabel : badAriaLabel, 'aria-labelledby': labelledById, className: classNames.pill, 'data-is-focusable': true, 'data-ktp-target': true, disabled: disabled, id: id, onClick: onClick, ref: toggleButton, role: role ? role : 'switch', type: 'button' }),
|
91 | thumb: {
|
92 | className: classNames.thumb,
|
93 | },
|
94 | stateText: {
|
95 | children: stateText,
|
96 | className: classNames.text,
|
97 | htmlFor: id,
|
98 | id: stateTextId,
|
99 | },
|
100 | };
|
101 | return (React.createElement(RootType, tslib_1.__assign({ ref: forwardedRef }, slotProps.root),
|
102 | label && React.createElement(Label_1.Label, tslib_1.__assign({}, slotProps.label)),
|
103 | React.createElement("div", tslib_1.__assign({}, slotProps.container),
|
104 | React.createElement("button", tslib_1.__assign({}, slotProps.pill),
|
105 | React.createElement("span", tslib_1.__assign({}, slotProps.thumb))),
|
106 | ((checked && onText) || offText) && React.createElement(Label_1.Label, tslib_1.__assign({}, slotProps.stateText)))));
|
107 | });
|
108 | exports.ToggleBase.displayName = COMPONENT_NAME + 'Base';
|
109 | var useComponentRef = function (props, isChecked, toggleButtonRef) {
|
110 | React.useImperativeHandle(props.componentRef, function () { return ({
|
111 | get checked() {
|
112 | return !!isChecked;
|
113 | },
|
114 | focus: function () {
|
115 | if (toggleButtonRef.current) {
|
116 | toggleButtonRef.current.focus();
|
117 | }
|
118 | },
|
119 | }); }, [isChecked, toggleButtonRef]);
|
120 | };
|
121 |
|
\ | No newline at end of file |