nishant-design-system
Version:
Sense UI components library
180 lines (179 loc) • 7.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Input = exports.INPUT_TYPES = 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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const 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'
});
exports.INPUT_TYPES = INPUT_TYPES;
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,
...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);
};
return /*#__PURE__*/React.createElement("div", {
className: (0, _classify.classify)(_InputModule.default.wrapper, {
[_InputModule.default.filled]: controlledInputFilled ?? false,
[_InputModule.default.withError]: error ?? false
})
}, 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, classNames?.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'
}),
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'
})), 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,
isLocked: locked,
isDisabled: disabled,
onClick: handleRightIconClick,
iconRightName: iconRightName,
iconRightType: iconRightType,
className: classNames?.iconRight
})), (Boolean(helperText) || error) && /*#__PURE__*/React.createElement("div", {
className: _InputModule.default.info
}, /*#__PURE__*/React.createElement(_Text.BodySmall, {
color: error && errorText ? 'danger' : disabled ? 'disabled' : 'secondary'
}, error && errorText ? errorText : helperText ?? '')));
};
const RightInputIcon = _ref => {
let {
isEmail,
isPassword,
showPassword,
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) {
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 = /*#__PURE__*/React.forwardRef(Input_);
exports.Input = Input;