Files
NapCatQQ/.pnpm-store/v3/files/ed/b527af67dd381787b0814c83a4573c2428666d9344d2fa1cfde6db0765119745b8bdc1f919903ffa3d4c150715d558a99cd379d3ab42bc1990e8fe694a0139
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

57 lines
2.1 KiB
Plaintext

/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {AriaSwitchProps} from '@react-types/switch';
import {InputHTMLAttributes, LabelHTMLAttributes} from 'react';
import {RefObject} from '@react-types/shared';
import {ToggleState} from '@react-stately/toggle';
import {useToggle} from '@react-aria/toggle';
export interface SwitchAria {
/** Props for the label wrapper element. */
labelProps: LabelHTMLAttributes<HTMLLabelElement>,
/** Props for the input element. */
inputProps: InputHTMLAttributes<HTMLInputElement>,
/** Whether the switch is selected. */
isSelected: boolean,
/** Whether the switch is in a pressed state. */
isPressed: boolean,
/** Whether the switch is disabled. */
isDisabled: boolean,
/** Whether the switch is read only. */
isReadOnly: boolean
}
/**
* Provides the behavior and accessibility implementation for a switch component.
* A switch is similar to a checkbox, but represents on/off values as opposed to selection.
* @param props - Props for the switch.
* @param state - State for the switch, as returned by `useToggleState`.
* @param ref - Ref to the HTML input element.
*/
export function useSwitch(props: AriaSwitchProps, state: ToggleState, ref: RefObject<HTMLInputElement | null>): SwitchAria {
let {labelProps, inputProps, isSelected, isPressed, isDisabled, isReadOnly} = useToggle(props, state, ref);
return {
labelProps,
inputProps: {
...inputProps,
role: 'switch',
checked: isSelected
},
isSelected,
isPressed,
isDisabled,
isReadOnly
};
}