UNPKG

react-lightning-design-system

Version:

Salesforce Lightning Design System components built with React

175 lines (170 loc) 6.02 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["id", "className", "label", "required", "error", "readOnly", "cols", "type", "bare", "value", "defaultValue", "htmlReadOnly", "iconLeft", "iconRight", "addonLeft", "addonRight", "symbolPattern", "tooltip", "tooltipIcon", "elementRef", "inputRef", "onKeyDown", "onChange", "onValueChange"]; import React, { useId, useEffect, useContext, useRef } from 'react'; import classnames from 'classnames'; import keycoder from 'keycoder'; import { Icon } from './Icon'; import { FormElement } from './FormElement'; import { Text } from './Text'; import { FieldSetColumnContext } from './FieldSet'; import { registerStyle } from './util'; import { useEventCallback } from './hooks'; import { createFC } from './common'; /** * */ function useInitComponentStyle() { useEffect(function () { registerStyle('input-icons', [ // fix styles of double-iconed input ['.slds-input-has-icon_left-right .react-slds-icon.slds-input__icon_right', '{ left: auto; }']]); }, []); } /** * */ var InputAddon = function InputAddon(_ref) { var content = _ref.content; return /*#__PURE__*/React.createElement(Text, { tag: "span", className: "slds-form-element__addon", category: "body", type: "regular" }, content); }; /** * */ var InputIcon = function InputIcon(_ref2) { var icon = _ref2.icon, align = _ref2.align; return /*#__PURE__*/React.isValidElement(icon) ? icon : typeof icon === 'string' ? /*#__PURE__*/React.createElement(Icon, { icon: typeof icon === 'string' ? icon : '', className: classnames('slds-input__icon', "slds-input__icon_".concat(align), 'slds-icon-text-default') }) : /*#__PURE__*/React.createElement(React.Fragment, null); }; /** * */ /** * */ export var Input = createFC(function (props) { var id = props.id, className = props.className, label = props.label, required = props.required, error = props.error, readOnly = props.readOnly, cols = props.cols, type = props.type, bare = props.bare, value = props.value, defaultValue = props.defaultValue, htmlReadOnly = props.htmlReadOnly, iconLeft = props.iconLeft, iconRight = props.iconRight, addonLeft = props.addonLeft, addonRight = props.addonRight, symbolPattern = props.symbolPattern, tooltip = props.tooltip, tooltipIcon = props.tooltipIcon, elementRef = props.elementRef, inputRef = props.inputRef, onKeyDown_ = props.onKeyDown, onChange_ = props.onChange, onValueChange = props.onValueChange, rprops = _objectWithoutProperties(props, _excluded); useInitComponentStyle(); var prevValueRef = useRef(); var onKeyDown = useEventCallback(function (e) { if (symbolPattern) { var keyCode = e.keyCode, shiftKey = e.shiftKey; var _value = keycoder.toCharacter(keyCode, shiftKey); if (_value && !_value.match(new RegExp(symbolPattern))) { e.preventDefault(); return; } } onKeyDown_ === null || onKeyDown_ === void 0 || onKeyDown_(e); }); var onChange = useEventCallback(function (e) { onChange_ === null || onChange_ === void 0 || onChange_(e); onValueChange === null || onValueChange === void 0 || onValueChange(e.target.value, prevValueRef.current); prevValueRef.current = e.target.value; }); var prefix = useId(); var rawTextId = id !== null && id !== void 0 ? id : "".concat(prefix, "-raw-text-id"); var inputId = id !== null && id !== void 0 ? id : "".concat(prefix, "-input-id"); var labelForId = readOnly ? rawTextId : inputId; var errorId = "".concat(prefix, "-error-id"); var _useContext = useContext(FieldSetColumnContext), isFieldSetColumn = _useContext.isFieldSetColumn; var inputClassNames = classnames(className, bare ? 'slds-input_bare' : 'slds-input'); var inputElem = readOnly ? /*#__PURE__*/React.createElement(Text, { id: rawTextId, type: "regular", category: "body" }, value) : /*#__PURE__*/React.createElement("input", _extends({ ref: inputRef, className: inputClassNames, id: inputId, type: type, value: value, defaultValue: defaultValue, readOnly: htmlReadOnly }, rprops, { onChange: onChange, onKeyDown: onKeyDown, "aria-describedby": error ? errorId : undefined, "aria-invalid": error ? true : undefined })); var contentElem = inputElem; if (iconLeft || iconRight || addonLeft || addonRight) { var wrapperClassName = classnames('slds-form-element__control', { 'slds-input-has-icon': iconLeft || iconRight }, { 'slds-input-has-icon_left-right': iconLeft && iconRight }, { 'slds-input-has-icon_left': iconLeft }, { 'slds-input-has-icon_right': iconRight }, { 'slds-input-has-fixed-addon': addonLeft || addonRight }); contentElem = /*#__PURE__*/React.createElement("div", { className: wrapperClassName }, addonLeft ? /*#__PURE__*/React.createElement(InputAddon, { content: addonLeft }) : undefined, iconLeft ? /*#__PURE__*/React.createElement(InputIcon, { icon: iconLeft, align: "left" }) : undefined, inputElem, iconRight ? /*#__PURE__*/React.createElement(InputIcon, { icon: iconRight, align: "right" }) : undefined, addonRight ? /*#__PURE__*/React.createElement(InputAddon, { content: addonRight }) : undefined); } if (isFieldSetColumn || label || required || error || cols) { var formElemProps = { controlId: labelForId, label: label, required: required, error: error, errorId: errorId, readOnly: readOnly, cols: cols, tooltip: tooltip, tooltipIcon: tooltipIcon, elementRef: elementRef }; return /*#__PURE__*/React.createElement(FormElement, formElemProps, contentElem); } return contentElem; }, { isFormElement: true }); //# sourceMappingURL=Input.js.map