pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export { useSwitch } from './useSwitch';
|
||||
export * from './useSwitch.types';
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
'use client';
|
||||
|
||||
export { useSwitch } from './useSwitch';
|
||||
export * from './useSwitch.types';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useSwitch/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import { UseSwitchParameters, UseSwitchReturnValue } from './useSwitch.types';
|
||||
/**
|
||||
* The basic building block for creating custom switches.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Switch](https://mui.com/base-ui/react-switch/#hook)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useSwitch API](https://mui.com/base-ui/react-switch/hooks-api/#use-switch)
|
||||
*/
|
||||
export declare function useSwitch(props: UseSwitchParameters): UseSwitchReturnValue;
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
'use client';
|
||||
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import * as React from 'react';
|
||||
import { unstable_useControlled as useControlled, unstable_useForkRef as useForkRef, unstable_useIsFocusVisible as useIsFocusVisible } from '@mui/utils';
|
||||
/**
|
||||
* The basic building block for creating custom switches.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Switch](https://mui.com/base-ui/react-switch/#hook)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useSwitch API](https://mui.com/base-ui/react-switch/hooks-api/#use-switch)
|
||||
*/
|
||||
export function useSwitch(props) {
|
||||
const {
|
||||
checked: checkedProp,
|
||||
defaultChecked,
|
||||
disabled,
|
||||
onBlur,
|
||||
onChange,
|
||||
onFocus,
|
||||
onFocusVisible,
|
||||
readOnly,
|
||||
required
|
||||
} = props;
|
||||
const [checked, setCheckedState] = useControlled({
|
||||
controlled: checkedProp,
|
||||
default: Boolean(defaultChecked),
|
||||
name: 'Switch',
|
||||
state: 'checked'
|
||||
});
|
||||
const createHandleInputChange = otherProps => event => {
|
||||
var _otherProps$onChange;
|
||||
// Workaround for https://github.com/facebook/react/issues/9023
|
||||
if (event.nativeEvent.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
setCheckedState(event.target.checked);
|
||||
onChange == null || onChange(event);
|
||||
(_otherProps$onChange = otherProps.onChange) == null || _otherProps$onChange.call(otherProps, event);
|
||||
};
|
||||
const {
|
||||
isFocusVisibleRef,
|
||||
onBlur: handleBlurVisible,
|
||||
onFocus: handleFocusVisible,
|
||||
ref: focusVisibleRef
|
||||
} = useIsFocusVisible();
|
||||
const [focusVisible, setFocusVisible] = React.useState(false);
|
||||
if (disabled && focusVisible) {
|
||||
setFocusVisible(false);
|
||||
}
|
||||
React.useEffect(() => {
|
||||
isFocusVisibleRef.current = focusVisible;
|
||||
}, [focusVisible, isFocusVisibleRef]);
|
||||
const inputRef = React.useRef(null);
|
||||
const createHandleFocus = otherProps => event => {
|
||||
var _otherProps$onFocus;
|
||||
// Fix for https://github.com/facebook/react/issues/7769
|
||||
if (!inputRef.current) {
|
||||
inputRef.current = event.currentTarget;
|
||||
}
|
||||
handleFocusVisible(event);
|
||||
if (isFocusVisibleRef.current === true) {
|
||||
setFocusVisible(true);
|
||||
onFocusVisible == null || onFocusVisible(event);
|
||||
}
|
||||
onFocus == null || onFocus(event);
|
||||
(_otherProps$onFocus = otherProps.onFocus) == null || _otherProps$onFocus.call(otherProps, event);
|
||||
};
|
||||
const createHandleBlur = otherProps => event => {
|
||||
var _otherProps$onBlur;
|
||||
handleBlurVisible(event);
|
||||
if (isFocusVisibleRef.current === false) {
|
||||
setFocusVisible(false);
|
||||
}
|
||||
onBlur == null || onBlur(event);
|
||||
(_otherProps$onBlur = otherProps.onBlur) == null || _otherProps$onBlur.call(otherProps, event);
|
||||
};
|
||||
const handleInputRef = useForkRef(focusVisibleRef, inputRef);
|
||||
const getInputProps = (otherProps = {}) => _extends({
|
||||
checked: checkedProp,
|
||||
defaultChecked,
|
||||
disabled,
|
||||
readOnly,
|
||||
ref: handleInputRef,
|
||||
required,
|
||||
type: 'checkbox',
|
||||
role: 'switch',
|
||||
'aria-checked': checkedProp
|
||||
}, otherProps, {
|
||||
onChange: createHandleInputChange(otherProps),
|
||||
onFocus: createHandleFocus(otherProps),
|
||||
onBlur: createHandleBlur(otherProps)
|
||||
});
|
||||
return {
|
||||
checked,
|
||||
disabled: Boolean(disabled),
|
||||
focusVisible,
|
||||
getInputProps,
|
||||
inputRef: handleInputRef,
|
||||
readOnly: Boolean(readOnly)
|
||||
};
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
import * as React from 'react';
|
||||
export interface UseSwitchParameters {
|
||||
/**
|
||||
* If `true`, the component is checked.
|
||||
*/
|
||||
checked?: boolean;
|
||||
/**
|
||||
* The default checked state. Use when the component is not controlled.
|
||||
*/
|
||||
defaultChecked?: boolean;
|
||||
/**
|
||||
* If `true`, the component is disabled.
|
||||
*/
|
||||
disabled?: boolean;
|
||||
onBlur?: React.FocusEventHandler;
|
||||
/**
|
||||
* Callback fired when the state is changed.
|
||||
*
|
||||
* @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
|
||||
* You can pull out the new value by accessing `event.target.value` (string).
|
||||
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
|
||||
*/
|
||||
onChange?: React.ChangeEventHandler<HTMLInputElement>;
|
||||
onFocus?: React.FocusEventHandler;
|
||||
onFocusVisible?: React.FocusEventHandler;
|
||||
/**
|
||||
* If `true`, the component is read only.
|
||||
*/
|
||||
readOnly?: boolean;
|
||||
/**
|
||||
* If `true`, the `input` element is required.
|
||||
*/
|
||||
required?: boolean;
|
||||
}
|
||||
interface UseSwitchInputSlotOwnProps {
|
||||
checked?: boolean;
|
||||
defaultChecked?: boolean;
|
||||
disabled?: boolean;
|
||||
onBlur: React.FocusEventHandler;
|
||||
onChange: React.ChangeEventHandler<HTMLInputElement>;
|
||||
onFocus: React.FocusEventHandler;
|
||||
readOnly?: boolean;
|
||||
ref: React.RefCallback<HTMLInputElement> | null;
|
||||
required?: boolean;
|
||||
type: React.HTMLInputTypeAttribute;
|
||||
}
|
||||
export type UseSwitchInputSlotProps<TOther = {}> = Omit<TOther, keyof UseSwitchInputSlotOwnProps> & UseSwitchInputSlotOwnProps;
|
||||
export interface UseSwitchReturnValue {
|
||||
/**
|
||||
* If `true`, the component will be checked.
|
||||
*/
|
||||
checked: boolean;
|
||||
/**
|
||||
* If `true`, the component will be disabled.
|
||||
*/
|
||||
disabled: boolean;
|
||||
/**
|
||||
* If `true`, it indicates that the component is being focused using keyboard.
|
||||
*/
|
||||
focusVisible: boolean;
|
||||
/**
|
||||
* Resolver for the input slot's props.
|
||||
* @param externalProps props for the input slot
|
||||
* @returns props that should be spread on the input slot
|
||||
*/
|
||||
getInputProps: (externalProps?: React.HTMLAttributes<HTMLInputElement>) => UseSwitchInputSlotProps;
|
||||
/**
|
||||
* Ref to the input slot's DOM node.
|
||||
*/
|
||||
inputRef: React.RefCallback<HTMLInputElement> | null;
|
||||
/**
|
||||
* If `true`, the component will be read only.
|
||||
*/
|
||||
readOnly: boolean;
|
||||
}
|
||||
export {};
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in new issue
Block a user