UNPKG

ultra-design

Version:
110 lines (94 loc) 4.38 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties'; import React, { useMemo, useState } from 'react'; import { textareaWithLabelStyles, textareaStyles } from './textarea-style.js'; import clsx from 'clsx'; import { useMergeProps } from '../utils/mergeProps.js'; import withStyle from '../utils/withStyle.js'; import { jsxs, jsx } from '@emotion/react/jsx-runtime'; var _excluded = ["defaultValue", "disabled", "readOnly", "onFocus", "onBlur", "onChange", "value", "placeholder", "resize", "className", "label", "style"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } var defaultProps = { resize: 'none' }; var TextareaComponent = function TextareaComponent(p, ref) { var props = useMergeProps(defaultProps, p); var defaultValue = props.defaultValue, disabled = props.disabled, readOnly = props.readOnly, onFocus = props.onFocus, onBlur = props.onBlur, onChange = props.onChange, value = props.value, placeholder = props.placeholder, resize = props.resize, className = props.className, label = props.label, style = props.style, rest = _objectWithoutProperties(props, _excluded); var isControlledComponent = useMemo(function () { return value !== undefined; }, [value]); var _useState = useState(defaultValue), _useState2 = _slicedToArray(_useState, 2), textareaValue = _useState2[0], setTextareaValue = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), focus = _useState4[0], setFocus = _useState4[1]; var handleFocus = function handleFocus(e) { setFocus(true); onFocus === null || onFocus === void 0 ? void 0 : onFocus(e); }; var handleBlur = function handleBlur(e) { setFocus(false); onBlur === null || onBlur === void 0 ? void 0 : onBlur(e); }; var handleChange = function handleChange(e) { if (disabled || readOnly) return; setTextareaValue(e.target.value); onChange === null || onChange === void 0 ? void 0 : onChange(e.target.value, e); }; var textareaRender = function textareaRender() { return jsx("div", { className: "ultra-textarea-with_label", css: textareaWithLabelStyles(props), children: jsx("div", { className: clsx(['ultra-textarea', focus && 'ultra-textarea--focused', disabled && 'ultra-textarea--disabled', className]), css: textareaStyles(props), style: style, children: jsx("textarea", _objectSpread(_objectSpread({ ref: ref, disabled: disabled, readOnly: readOnly, placeholder: placeholder, onChange: handleChange, onFocus: handleFocus, onBlur: handleBlur }, isControlledComponent ? { value: textareaValue } : { defaultValue: defaultValue }), {}, { style: { resize: resize } }, rest)) }) }); }; return label ? jsxs("div", { className: "ultra-textarea-with_label", css: textareaWithLabelStyles(props), children: [label && jsx("span", { className: "ultra-textarea__label", children: label }), textareaRender()] }) : textareaRender(); }; var Textarea = /*#__PURE__*/React.forwardRef(TextareaComponent); var Textarea$1 = withStyle(Textarea); export { Textarea$1 as default };