@spaced-out/ui-design-system
Version:
Sense UI components library
196 lines (195 loc) • 7.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Input = exports.INPUT_TYPES = exports.EXPONENT_CHARACTER_LIST = void 0;
var React = _interopRequireWildcard(require("react"));
var _classify = require("../../utils/classify");
var _Icon = require("../Icon");
var _Text = require("../Text");
var _InputModule = _interopRequireDefault(require("./Input.module.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const EXPONENT_CHARACTER_LIST = exports.EXPONENT_CHARACTER_LIST = ['E', 'e'];
const INPUT_TYPES = exports.INPUT_TYPES = Object.freeze({
text: 'text',
number: 'number',
password: 'password',
email: 'email',
tel: 'tel',
url: 'url',
date: 'date',
'datetime-local': 'datetime-local',
time: 'time',
week: 'week',
month: 'month',
color: 'color',
search: 'search'
});
const Input_ = (props, ref) => {
const {
value,
type,
onChange,
onFocus,
onBlur,
onIconRightClick,
onContainerClick,
name,
disabled,
placeholder,
error,
locked,
errorText,
label,
helperText,
classNames,
size = 'medium',
iconLeftName = '',
iconLeftType = 'regular',
iconRightName = '',
iconRightType = 'regular',
required,
readOnly,
boxRef,
onKeyDown,
disallowExponents,
hideNumberSpinner,
hidePasswordToggleIcon,
...inputProps
} = props;
const [showPassword, setShowPassword] = React.useState(false);
const controlledInputFilled = value !== '';
const handleRightIconClick = e => {
if (locked || disabled) {
return;
}
if (type === 'password') {
setShowPassword(!showPassword);
}
onIconRightClick && onIconRightClick(e);
};
const handleKeyDown = e => {
if (type === INPUT_TYPES.number && disallowExponents) {
if (EXPONENT_CHARACTER_LIST.includes(e.key)) {
e.preventDefault();
}
}
onKeyDown?.(e);
};
return /*#__PURE__*/React.createElement("div", {
className: (0, _classify.classify)(_InputModule.default.wrapper, {
[_InputModule.default.filled]: controlledInputFilled ?? false,
[_InputModule.default.withError]: error ?? false
}, classNames?.wrapper)
}, Boolean(label) && /*#__PURE__*/React.createElement("div", {
className: _InputModule.default.info
}, /*#__PURE__*/React.createElement("div", {
className: _InputModule.default.infoContent
}, /*#__PURE__*/React.createElement(_Text.FormLabelSmall, {
color: "secondary"
}, label ?? ''), "\xA0", required && /*#__PURE__*/React.createElement(_Text.FormLabelSmall, {
color: "danger"
}, '*'))), /*#__PURE__*/React.createElement("div", {
className: (0, _classify.classify)(_InputModule.default.box, {
[_InputModule.default.inputDisabled]: disabled ?? false,
[_InputModule.default.medium]: size === 'medium',
[_InputModule.default.small]: size === 'small',
[_InputModule.default.locked]: locked,
[_InputModule.default.color]: type === 'color',
[_InputModule.default.hideNumberSpinner]: hideNumberSpinner
}, classNames?.box),
onClick: !(disabled || locked) ? onContainerClick : null,
ref: boxRef
}, iconLeftName && /*#__PURE__*/React.createElement(_Icon.Icon, {
className: (0, _classify.classify)(classNames?.iconLeft),
name: iconLeftName,
color: disabled ? 'disabled' : 'secondary',
size: "small",
type: iconLeftType
}), /*#__PURE__*/React.createElement("input", _extends({}, inputProps, {
disabled: locked || disabled,
name: name,
ref: ref,
placeholder: placeholder,
value: value,
onChange: onChange,
onFocus: onFocus,
onBlur: onBlur,
type: showPassword ? 'text' : type,
readOnly: readOnly && 'readOnly',
onKeyDown: handleKeyDown
})), type === 'color' && /*#__PURE__*/React.createElement("div", {
className: (0, _classify.classify)(_InputModule.default.colorText, _InputModule.default[size], {
[_InputModule.default.hasValue]: value
})
}, value ? value : placeholder), /*#__PURE__*/React.createElement(RightInputIcon, {
isEmail: type === 'email',
isPassword: type === 'password',
showPassword: showPassword,
showPasswordToggleIcon: !hidePasswordToggleIcon,
isLocked: locked,
isDisabled: disabled,
onClick: handleRightIconClick,
iconRightName: iconRightName,
iconRightType: iconRightType,
className: classNames?.iconRight
})), (Boolean(helperText) || error) && /*#__PURE__*/React.createElement("div", {
className: _InputModule.default.info
}, error && errorText ? /*#__PURE__*/React.createElement(_Text.BodySmall, {
color: "danger"
}, errorText) : typeof helperText === 'string' ? /*#__PURE__*/React.createElement(_Text.BodySmall, {
color: disabled ? 'disabled' : 'secondary'
}, helperText) : helperText));
};
const RightInputIcon = _ref => {
let {
isEmail,
isPassword,
showPassword,
showPasswordToggleIcon,
isLocked,
isDisabled,
iconRightName,
iconRightType,
...rightIconProps
} = _ref;
if (isLocked) {
return /*#__PURE__*/React.createElement(_Icon.Icon, _extends({
name: "lock",
color: isDisabled ? 'disabled' : 'secondary',
size: "small"
}, rightIconProps));
}
if (isEmail) {
return /*#__PURE__*/React.createElement(_Icon.Icon, _extends({
name: "at",
color: isDisabled ? 'disabled' : 'secondary',
size: "small",
type: iconRightType
}, rightIconProps));
}
if (isPassword && showPasswordToggleIcon) {
return /*#__PURE__*/React.createElement(_Icon.Icon, _extends({
name: showPassword ? 'eye-slash' : 'eye',
color: isDisabled ? 'disabled' : 'secondary',
size: "small",
type: iconRightType
}, rightIconProps, {
className: (0, _classify.classify)(_InputModule.default.rightClickableIcon, {
[_InputModule.default.disabled]: isDisabled || isLocked
})
}));
}
if (iconRightName) {
return /*#__PURE__*/React.createElement(_Icon.Icon, _extends({
name: iconRightName || '',
color: isDisabled ? 'disabled' : 'secondary',
size: "small",
type: iconRightType
}, rightIconProps));
}
return /*#__PURE__*/React.createElement(React.Fragment, null);
};
const Input = exports.Input = /*#__PURE__*/React.forwardRef(Input_);