UNPKG

6.38 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2import _extends from "@babel/runtime/helpers/esm/extends";
3
4var _span;
5
6const _excluded = ["children", "className", "component", "disablePointerEvents", "disableTypography", "position", "variant"];
7import * as React from 'react';
8import PropTypes from 'prop-types';
9import clsx from 'clsx';
10import { unstable_composeClasses as composeClasses } from '@mui/base';
11import capitalize from '../utils/capitalize';
12import Typography from '../Typography';
13import FormControlContext from '../FormControl/FormControlContext';
14import useFormControl from '../FormControl/useFormControl';
15import styled from '../styles/styled';
16import inputAdornmentClasses, { getInputAdornmentUtilityClass } from './inputAdornmentClasses';
17import useThemeProps from '../styles/useThemeProps';
18import { jsx as _jsx } from "react/jsx-runtime";
19import { jsxs as _jsxs } from "react/jsx-runtime";
20
21const overridesResolver = (props, styles) => {
22 const {
23 ownerState
24 } = props;
25 return [styles.root, styles[`position${capitalize(ownerState.position)}`], ownerState.disablePointerEvents === true && styles.disablePointerEvents, styles[ownerState.variant]];
26};
27
28const useUtilityClasses = ownerState => {
29 const {
30 classes,
31 disablePointerEvents,
32 hiddenLabel,
33 position,
34 size,
35 variant
36 } = ownerState;
37 const slots = {
38 root: ['root', disablePointerEvents && 'disablePointerEvents', position && `position${capitalize(position)}`, variant, hiddenLabel && 'hiddenLabel', size && `size${capitalize(size)}`]
39 };
40 return composeClasses(slots, getInputAdornmentUtilityClass, classes);
41};
42
43const InputAdornmentRoot = styled('div', {
44 name: 'MuiInputAdornment',
45 slot: 'Root',
46 overridesResolver
47})(({
48 theme,
49 ownerState
50}) => _extends({
51 display: 'flex',
52 height: '0.01em',
53 // Fix IE11 flexbox alignment. To remove at some point.
54 maxHeight: '2em',
55 alignItems: 'center',
56 whiteSpace: 'nowrap',
57 color: (theme.vars || theme).palette.action.active
58}, ownerState.variant === 'filled' && {
59 // Styles applied to the root element if `variant="filled"`.
60 [`&.${inputAdornmentClasses.positionStart}&:not(.${inputAdornmentClasses.hiddenLabel})`]: {
61 marginTop: 16
62 }
63}, ownerState.position === 'start' && {
64 // Styles applied to the root element if `position="start"`.
65 marginRight: 8
66}, ownerState.position === 'end' && {
67 // Styles applied to the root element if `position="end"`.
68 marginLeft: 8
69}, ownerState.disablePointerEvents === true && {
70 // Styles applied to the root element if `disablePointerEvents={true}`.
71 pointerEvents: 'none'
72}));
73const InputAdornment = /*#__PURE__*/React.forwardRef(function InputAdornment(inProps, ref) {
74 const props = useThemeProps({
75 props: inProps,
76 name: 'MuiInputAdornment'
77 });
78
79 const {
80 children,
81 className,
82 component = 'div',
83 disablePointerEvents = false,
84 disableTypography = false,
85 position,
86 variant: variantProp
87 } = props,
88 other = _objectWithoutPropertiesLoose(props, _excluded);
89
90 const muiFormControl = useFormControl() || {};
91 let variant = variantProp;
92
93 if (variantProp && muiFormControl.variant) {
94 if (process.env.NODE_ENV !== 'production') {
95 if (variantProp === muiFormControl.variant) {
96 console.error('MUI: The `InputAdornment` variant infers the variant prop ' + 'you do not have to provide one.');
97 }
98 }
99 }
100
101 if (muiFormControl && !variant) {
102 variant = muiFormControl.variant;
103 }
104
105 const ownerState = _extends({}, props, {
106 hiddenLabel: muiFormControl.hiddenLabel,
107 size: muiFormControl.size,
108 disablePointerEvents,
109 position,
110 variant
111 });
112
113 const classes = useUtilityClasses(ownerState);
114 return /*#__PURE__*/_jsx(FormControlContext.Provider, {
115 value: null,
116 children: /*#__PURE__*/_jsx(InputAdornmentRoot, _extends({
117 as: component,
118 ownerState: ownerState,
119 className: clsx(classes.root, className),
120 ref: ref
121 }, other, {
122 children: typeof children === 'string' && !disableTypography ? /*#__PURE__*/_jsx(Typography, {
123 color: "text.secondary",
124 children: children
125 }) : /*#__PURE__*/_jsxs(React.Fragment, {
126 children: [position === 'start' ?
127 /* notranslate needed while Google Translate will not fix zero-width space issue */
128 _span || (_span = /*#__PURE__*/_jsx("span", {
129 className: "notranslate",
130 children: "\u200B"
131 })) : null, children]
132 })
133 }))
134 });
135});
136process.env.NODE_ENV !== "production" ? InputAdornment.propTypes
137/* remove-proptypes */
138= {
139 // ----------------------------- Warning --------------------------------
140 // | These PropTypes are generated from the TypeScript type definitions |
141 // | To update them edit the d.ts file and run "yarn proptypes" |
142 // ----------------------------------------------------------------------
143
144 /**
145 * The content of the component, normally an `IconButton` or string.
146 */
147 children: PropTypes.node,
148
149 /**
150 * Override or extend the styles applied to the component.
151 */
152 classes: PropTypes.object,
153
154 /**
155 * @ignore
156 */
157 className: PropTypes.string,
158
159 /**
160 * The component used for the root node.
161 * Either a string to use a HTML element or a component.
162 */
163 component: PropTypes.elementType,
164
165 /**
166 * Disable pointer events on the root.
167 * This allows for the content of the adornment to focus the `input` on click.
168 * @default false
169 */
170 disablePointerEvents: PropTypes.bool,
171
172 /**
173 * If children is a string then disable wrapping in a Typography component.
174 * @default false
175 */
176 disableTypography: PropTypes.bool,
177
178 /**
179 * The position this adornment should appear relative to the `Input`.
180 */
181 position: PropTypes.oneOf(['end', 'start']).isRequired,
182
183 /**
184 * The system prop that allows defining system overrides as well as additional CSS styles.
185 */
186 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
187
188 /**
189 * The variant to use.
190 * Note: If you are using the `TextField` component or the `FormControl` component
191 * you do not have to set this manually.
192 */
193 variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
194} : void 0;
195export default InputAdornment;
\No newline at end of file