365 lines
No EOL
9.3 KiB
JavaScript
365 lines
No EOL
9.3 KiB
JavaScript
"use strict";
|
|
'use client';
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = void 0;
|
|
var React = _interopRequireWildcard(require("react"));
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
var _refType = _interopRequireDefault(require("@mui/utils/refType"));
|
|
var _composeClasses = _interopRequireDefault(require("@mui/utils/composeClasses"));
|
|
var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
|
|
var _rootShouldForwardProp = _interopRequireDefault(require("../styles/rootShouldForwardProp"));
|
|
var _zeroStyled = require("../zero-styled");
|
|
var _useControlled = _interopRequireDefault(require("../utils/useControlled"));
|
|
var _useFormControl = _interopRequireDefault(require("../FormControl/useFormControl"));
|
|
var _ButtonBase = _interopRequireDefault(require("../ButtonBase"));
|
|
var _switchBaseClasses = require("./switchBaseClasses");
|
|
var _useSlot = _interopRequireDefault(require("../utils/useSlot"));
|
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
const useUtilityClasses = ownerState => {
|
|
const {
|
|
classes,
|
|
checked,
|
|
disabled,
|
|
edge
|
|
} = ownerState;
|
|
const slots = {
|
|
root: ['root', checked && 'checked', disabled && 'disabled', edge && `edge${(0, _capitalize.default)(edge)}`],
|
|
input: ['input']
|
|
};
|
|
return (0, _composeClasses.default)(slots, _switchBaseClasses.getSwitchBaseUtilityClass, classes);
|
|
};
|
|
const SwitchBaseRoot = (0, _zeroStyled.styled)(_ButtonBase.default, {
|
|
name: 'MuiSwitchBase'
|
|
})({
|
|
padding: 9,
|
|
borderRadius: '50%',
|
|
variants: [{
|
|
props: {
|
|
edge: 'start',
|
|
size: 'small'
|
|
},
|
|
style: {
|
|
marginLeft: -3
|
|
}
|
|
}, {
|
|
props: ({
|
|
edge,
|
|
ownerState
|
|
}) => edge === 'start' && ownerState.size !== 'small',
|
|
style: {
|
|
marginLeft: -12
|
|
}
|
|
}, {
|
|
props: {
|
|
edge: 'end',
|
|
size: 'small'
|
|
},
|
|
style: {
|
|
marginRight: -3
|
|
}
|
|
}, {
|
|
props: ({
|
|
edge,
|
|
ownerState
|
|
}) => edge === 'end' && ownerState.size !== 'small',
|
|
style: {
|
|
marginRight: -12
|
|
}
|
|
}]
|
|
});
|
|
const SwitchBaseInput = (0, _zeroStyled.styled)('input', {
|
|
name: 'MuiSwitchBase',
|
|
shouldForwardProp: _rootShouldForwardProp.default
|
|
})({
|
|
cursor: 'inherit',
|
|
position: 'absolute',
|
|
opacity: 0,
|
|
width: '100%',
|
|
height: '100%',
|
|
top: 0,
|
|
left: 0,
|
|
margin: 0,
|
|
padding: 0,
|
|
zIndex: 1
|
|
});
|
|
|
|
/**
|
|
* @ignore - internal component.
|
|
*/
|
|
const SwitchBase = /*#__PURE__*/React.forwardRef(function SwitchBase(props, ref) {
|
|
const {
|
|
autoFocus,
|
|
checked: checkedProp,
|
|
checkedIcon,
|
|
defaultChecked,
|
|
disabled: disabledProp,
|
|
disableFocusRipple = false,
|
|
edge = false,
|
|
icon,
|
|
id,
|
|
inputProps,
|
|
inputRef,
|
|
name,
|
|
onBlur,
|
|
onChange,
|
|
onFocus,
|
|
readOnly,
|
|
required = false,
|
|
tabIndex,
|
|
type,
|
|
value,
|
|
slots = {},
|
|
slotProps = {},
|
|
...other
|
|
} = props;
|
|
const [checked, setCheckedState] = (0, _useControlled.default)({
|
|
controlled: checkedProp,
|
|
default: Boolean(defaultChecked),
|
|
name: 'SwitchBase',
|
|
state: 'checked'
|
|
});
|
|
const muiFormControl = (0, _useFormControl.default)();
|
|
const handleFocus = event => {
|
|
if (onFocus) {
|
|
onFocus(event);
|
|
}
|
|
if (muiFormControl && muiFormControl.onFocus) {
|
|
muiFormControl.onFocus(event);
|
|
}
|
|
};
|
|
const handleBlur = event => {
|
|
if (onBlur) {
|
|
onBlur(event);
|
|
}
|
|
if (muiFormControl && muiFormControl.onBlur) {
|
|
muiFormControl.onBlur(event);
|
|
}
|
|
};
|
|
const handleInputChange = event => {
|
|
// Workaround for https://github.com/facebook/react/issues/9023
|
|
if (event.nativeEvent.defaultPrevented) {
|
|
return;
|
|
}
|
|
const newChecked = event.target.checked;
|
|
setCheckedState(newChecked);
|
|
if (onChange) {
|
|
// TODO v6: remove the second argument.
|
|
onChange(event, newChecked);
|
|
}
|
|
};
|
|
let disabled = disabledProp;
|
|
if (muiFormControl) {
|
|
if (typeof disabled === 'undefined') {
|
|
disabled = muiFormControl.disabled;
|
|
}
|
|
}
|
|
const hasLabelFor = type === 'checkbox' || type === 'radio';
|
|
const ownerState = {
|
|
...props,
|
|
checked,
|
|
disabled,
|
|
disableFocusRipple,
|
|
edge
|
|
};
|
|
const classes = useUtilityClasses(ownerState);
|
|
const externalForwardedProps = {
|
|
slots,
|
|
slotProps: {
|
|
input: inputProps,
|
|
...slotProps
|
|
}
|
|
};
|
|
const [RootSlot, rootSlotProps] = (0, _useSlot.default)('root', {
|
|
ref,
|
|
elementType: SwitchBaseRoot,
|
|
className: classes.root,
|
|
shouldForwardComponentProp: true,
|
|
externalForwardedProps: {
|
|
...externalForwardedProps,
|
|
component: 'span',
|
|
...other
|
|
},
|
|
getSlotProps: handlers => ({
|
|
...handlers,
|
|
onFocus: event => {
|
|
handlers.onFocus?.(event);
|
|
handleFocus(event);
|
|
},
|
|
onBlur: event => {
|
|
handlers.onBlur?.(event);
|
|
handleBlur(event);
|
|
}
|
|
}),
|
|
ownerState,
|
|
additionalProps: {
|
|
centerRipple: true,
|
|
focusRipple: !disableFocusRipple,
|
|
disabled,
|
|
role: undefined,
|
|
tabIndex: null
|
|
}
|
|
});
|
|
const [InputSlot, inputSlotProps] = (0, _useSlot.default)('input', {
|
|
ref: inputRef,
|
|
elementType: SwitchBaseInput,
|
|
className: classes.input,
|
|
externalForwardedProps,
|
|
getSlotProps: handlers => ({
|
|
...handlers,
|
|
onChange: event => {
|
|
handlers.onChange?.(event);
|
|
handleInputChange(event);
|
|
}
|
|
}),
|
|
ownerState,
|
|
additionalProps: {
|
|
autoFocus,
|
|
checked: checkedProp,
|
|
defaultChecked,
|
|
disabled,
|
|
id: hasLabelFor ? id : undefined,
|
|
name,
|
|
readOnly,
|
|
required,
|
|
tabIndex,
|
|
type,
|
|
...(type === 'checkbox' && value === undefined ? {} : {
|
|
value
|
|
})
|
|
}
|
|
});
|
|
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(RootSlot, {
|
|
...rootSlotProps,
|
|
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(InputSlot, {
|
|
...inputSlotProps
|
|
}), checked ? checkedIcon : icon]
|
|
});
|
|
});
|
|
|
|
// NB: If changed, please update Checkbox, Switch and Radio
|
|
// so that the API documentation is updated.
|
|
process.env.NODE_ENV !== "production" ? SwitchBase.propTypes = {
|
|
/**
|
|
* If `true`, the `input` element is focused during the first mount.
|
|
*/
|
|
autoFocus: _propTypes.default.bool,
|
|
/**
|
|
* If `true`, the component is checked.
|
|
*/
|
|
checked: _propTypes.default.bool,
|
|
/**
|
|
* The icon to display when the component is checked.
|
|
*/
|
|
checkedIcon: _propTypes.default.node.isRequired,
|
|
/**
|
|
* Override or extend the styles applied to the component.
|
|
*/
|
|
classes: _propTypes.default.object,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
className: _propTypes.default.string,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
defaultChecked: _propTypes.default.bool,
|
|
/**
|
|
* If `true`, the component is disabled.
|
|
*/
|
|
disabled: _propTypes.default.bool,
|
|
/**
|
|
* If `true`, the keyboard focus ripple is disabled.
|
|
* @default false
|
|
*/
|
|
disableFocusRipple: _propTypes.default.bool,
|
|
/**
|
|
* If given, uses a negative margin to counteract the padding on one
|
|
* side (this is often helpful for aligning the left or right
|
|
* side of the icon with content above or below, without ruining the border
|
|
* size and shape).
|
|
* @default false
|
|
*/
|
|
edge: _propTypes.default.oneOf(['end', 'start', false]),
|
|
/**
|
|
* The icon to display when the component is unchecked.
|
|
*/
|
|
icon: _propTypes.default.node.isRequired,
|
|
/**
|
|
* The id of the `input` element.
|
|
*/
|
|
id: _propTypes.default.string,
|
|
/**
|
|
* [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#attributes) applied to the `input` element.
|
|
*/
|
|
inputProps: _propTypes.default.object,
|
|
/**
|
|
* Pass a ref to the `input` element.
|
|
*/
|
|
inputRef: _refType.default,
|
|
/*
|
|
* @ignore
|
|
*/
|
|
name: _propTypes.default.string,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onBlur: _propTypes.default.func,
|
|
/**
|
|
* Callback fired when the state is changed.
|
|
*
|
|
* @param {object} event The event source of the callback.
|
|
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
|
|
*/
|
|
onChange: _propTypes.default.func,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onFocus: _propTypes.default.func,
|
|
/**
|
|
* It prevents the user from changing the value of the field
|
|
* (not from interacting with the field).
|
|
*/
|
|
readOnly: _propTypes.default.bool,
|
|
/**
|
|
* If `true`, the `input` element is required.
|
|
*/
|
|
required: _propTypes.default.bool,
|
|
/**
|
|
* The props used for each slot inside.
|
|
* @default {}
|
|
*/
|
|
slotProps: _propTypes.default.shape({
|
|
input: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
|
|
root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
|
|
}),
|
|
/**
|
|
* The components used for each slot inside.
|
|
* @default {}
|
|
*/
|
|
slots: _propTypes.default.shape({
|
|
input: _propTypes.default.elementType,
|
|
root: _propTypes.default.elementType
|
|
}),
|
|
/**
|
|
* The system prop that allows defining system overrides as well as additional CSS styles.
|
|
*/
|
|
sx: _propTypes.default.object,
|
|
/**
|
|
* @ignore
|
|
*/
|
|
tabIndex: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
|
|
/**
|
|
* The input component prop `type`.
|
|
*/
|
|
type: _propTypes.default.string.isRequired,
|
|
/**
|
|
* The value of the component.
|
|
*/
|
|
value: _propTypes.default.any
|
|
} : void 0;
|
|
var _default = exports.default = SwitchBase; |