UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

170 lines (147 loc) 5.33 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import React from 'react'; import PropTypes from 'prop-types'; import debounce from '../utils/debounce'; import { useForkRef } from '../utils/reactHelpers'; function getStyleValue(computedStyle, property) { return parseInt(computedStyle[property], 10) || 0; } var useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect; var styles = { /* Styles applied to the shadow textarea element. */ shadow: { // Visibility needed to hide the extra text area on iPads visibility: 'hidden', // Remove from the content flow position: 'absolute', // Ignore the scrollbar width overflow: 'hidden', height: 0, top: 0 } }; var TextareaAutosize = React.forwardRef(function TextareaAutosize(props, ref) { var onChange = props.onChange, rows = props.rows, rowsMax = props.rowsMax, style = props.style, value = props.value, other = _objectWithoutProperties(props, ["onChange", "rows", "rowsMax", "style", "value"]); var _React$useRef = React.useRef(value != null), isControlled = _React$useRef.current; var inputRef = React.useRef(null); var handleRef = useForkRef(ref, inputRef); var shadowRef = React.useRef(null); var _React$useState = React.useState({}), _React$useState2 = _slicedToArray(_React$useState, 2), state = _React$useState2[0], setState = _React$useState2[1]; var syncHeight = React.useCallback(function () { var input = inputRef.current; var computedStyle = window.getComputedStyle(input); var inputShallow = shadowRef.current; inputShallow.style.width = computedStyle.width; inputShallow.value = input.value || props.placeholder || 'x'; var boxSizing = computedStyle['box-sizing']; var padding = getStyleValue(computedStyle, 'padding-bottom') + getStyleValue(computedStyle, 'padding-top'); var border = getStyleValue(computedStyle, 'border-bottom-width') + getStyleValue(computedStyle, 'border-top-width'); // The height of the inner content var innerHeight = inputShallow.scrollHeight - padding; // Measure height of a textarea with a single row inputShallow.value = 'x'; var singleRowHeight = inputShallow.scrollHeight - padding; // The height of the outer content var outerHeight = innerHeight; if (rows != null) { outerHeight = Math.max(Number(rows) * singleRowHeight, outerHeight); } if (rowsMax != null) { outerHeight = Math.min(Number(rowsMax) * singleRowHeight, outerHeight); } outerHeight = Math.max(outerHeight, singleRowHeight); // Take the box sizing into account for applying this value as a style. var outerHeightStyle = outerHeight + (boxSizing === 'border-box' ? padding + border : 0); setState(function (prevState) { // Need a large enough different to update the height. // This prevents infinite rendering loop. if (outerHeightStyle > 0 && Math.abs((prevState.outerHeightStyle || 0) - outerHeightStyle) > 1) { return { innerHeight: innerHeight, outerHeight: outerHeight, outerHeightStyle: outerHeightStyle }; } return prevState; }); }, [setState, rows, rowsMax, props.placeholder]); React.useEffect(function () { var handleResize = debounce(function () { syncHeight(); }); window.addEventListener('resize', handleResize); return function () { handleResize.clear(); window.removeEventListener('resize', handleResize); }; }, [syncHeight]); useEnhancedEffect(function () { syncHeight(); }); var handleChange = function handleChange(event) { if (!isControlled) { syncHeight(); } if (onChange) { onChange(event); } }; return React.createElement(React.Fragment, null, React.createElement("textarea", _extends({ value: value, onChange: handleChange, ref: handleRef // Apply the rows prop to get a "correct" first SSR paint , rows: rows || 1, style: _extends({ height: state.outerHeightStyle, // Need a large enough different to allow scrolling. // This prevents infinite rendering loop. overflow: Math.abs(state.outerHeight - state.innerHeight) <= 1 ? 'hidden' : null }, style) }, other)), React.createElement("textarea", { "aria-hidden": true, className: props.className, readOnly: true, ref: shadowRef, tabIndex: -1, style: _extends({}, styles.shadow, {}, style) })); }); process.env.NODE_ENV !== "production" ? TextareaAutosize.propTypes = { /** * @ignore */ className: PropTypes.string, /** * @ignore */ onChange: PropTypes.func, /** * @ignore */ placeholder: PropTypes.string, /** * Minimum umber of rows to display. */ rows: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** * Maximum number of rows to display. */ rowsMax: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** * @ignore */ style: PropTypes.object, /** * @ignore */ value: PropTypes.any } : void 0; export default TextareaAutosize;