@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
222 lines (221 loc) • 9.36 kB
JavaScript
"use strict";
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _index = require("../../../../components/index.js");
var z = _interopRequireWildcard(require("zod"));
var _InputMasked = _interopRequireDefault(require("../../../../components/InputMasked.js"));
var _Context = _interopRequireDefault(require("../../DataContext/Context.js"));
var _FieldBlockContext = _interopRequireDefault(require("../../FieldBlock/FieldBlockContext.js"));
var _index2 = _interopRequireDefault(require("../../FieldBlock/index.js"));
var _index3 = require("../../hooks/index.js");
var _utils = require("../../../../components/flex/utils.js");
var _componentHelper = require("../../../../shared/component-helper.js");
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); }
function StringComponent(props) {
var _props$width, _props$innerRef, _dataContext$props2, _value$toString;
const dataContext = (0, _react.useContext)(_Context.default);
const fieldBlockContext = (0, _react.useContext)(_FieldBlockContext.default);
const schema = (0, _react.useMemo)(() => {
var _props$schema;
return ((_props$schema = props.schema) !== null && _props$schema !== void 0 ? _props$schema : p => {
return z.string().superRefine((val, ctx) => {
if (p.minLength !== undefined && val.length < p.minLength) {
ctx.addIssue({
code: 'too_small',
minimum: p.minLength,
type: 'string',
inclusive: true,
origin: 'string',
message: 'StringField.errorMinLength',
messageValues: {
minLength: String(p.minLength)
}
});
}
if (p.maxLength !== undefined && val.length > p.maxLength) {
ctx.addIssue({
code: 'too_big',
maximum: p.maxLength,
type: 'string',
inclusive: true,
origin: 'string',
message: 'StringField.errorMaxLength',
messageValues: {
maxLength: String(p.maxLength)
}
});
}
if (p.pattern && !new RegExp(p.pattern, 'u').test(val)) {
ctx.addIssue({
code: 'invalid_format',
validation: 'regex',
format: 'regex',
message: 'Field.errorPattern',
messageValues: {
pattern: p.pattern
}
});
}
});
}
);
}, [props.schema, props.minLength, props.maxLength, props.pattern]);
const fromInput = (0, _react.useCallback)(event => {
var _event, _event$cleanedValue, _event2, _event3;
if (typeof event === 'string') {
event = {
value: event
};
}
if (((_event = event) === null || _event === void 0 ? void 0 : _event.value) === '') {
return props.emptyValue;
}
return (_event$cleanedValue = (_event2 = event) === null || _event2 === void 0 ? void 0 : _event2.cleanedValue) !== null && _event$cleanedValue !== void 0 ? _event$cleanedValue : (_event3 = event) === null || _event3 === void 0 ? void 0 : _event3.value;
}, [props.emptyValue]);
const toEvent = (0, _react.useCallback)((value, type) => {
if (props.trim && type === 'onBlur') {
const spaces = '[\\s ]';
if (new RegExp(`^${spaces}|${spaces}$`).test(value)) {
value = value.replace(new RegExp(`^${spaces}+|${spaces}+$`, 'g'), '');
handleChange(value);
}
}
return value;
}, [props.trim]);
const transform = props.transformValue;
const transformValue = (0, _react.useCallback)(value => {
if (props.capitalize) {
value = (0, _componentHelper.toCapitalized)(String(value || ''));
}
return (transform === null || transform === void 0 ? void 0 : transform(value)) || value;
}, [props.capitalize, transform]);
const ref = (0, _react.useRef)();
const preparedProps = {
...props,
schema,
fromInput,
toEvent,
transformValue,
width: (_props$width = props.width) !== null && _props$width !== void 0 ? _props$width : fieldBlockContext !== null && fieldBlockContext !== void 0 && fieldBlockContext.composition ? 'stretch' : 'large',
innerRef: (_props$innerRef = props.innerRef) !== null && _props$innerRef !== void 0 ? _props$innerRef : ref
};
const {
id,
name,
className,
innerRef,
inputClassName,
placeholder,
value,
hasError,
disabled,
multiline,
mask,
leftIcon,
rightIcon,
width,
htmlAttributes,
submitElement,
type,
clear,
align,
size,
selectall,
keepPlaceholder,
rows,
autoresizeMaxRows = 6,
autoresize = true,
characterCounter,
autoComplete,
inputMode,
autoCorrect,
spellCheck,
autoFocus,
autoCapitalize,
handleFocus,
handleBlur,
handleChange,
setDisplayValue,
onKeyDown
} = (0, _index3.useFieldProps)(preparedProps);
(0, _react.useEffect)(() => {
var _innerRef$current;
setDisplayValue((_innerRef$current = innerRef.current) === null || _innerRef$current === void 0 ? void 0 : _innerRef$current.value);
}, [innerRef, setDisplayValue, value]);
const transformInstantly = (0, _react.useCallback)(value => props.capitalize ? (0, _componentHelper.toCapitalized)(value) : value, [props.capitalize]);
const {
handleSubmit
} = dataContext !== null && dataContext !== void 0 ? dataContext : {};
const handleKeyDown = (0, _react.useCallback)(({
event
}) => {
var _dataContext$props;
if (!multiline && dataContext !== null && dataContext !== void 0 && (_dataContext$props = dataContext.props) !== null && _dataContext$props !== void 0 && _dataContext$props.isolate && event.key === 'Enter') {
var _event$preventDefault;
handleSubmit();
(_event$preventDefault = event.preventDefault) === null || _event$preventDefault === void 0 || _event$preventDefault.call(event);
}
onKeyDown === null || onKeyDown === void 0 || onKeyDown(event);
}, [handleSubmit, dataContext === null || dataContext === void 0 || (_dataContext$props2 = dataContext.props) === null || _dataContext$props2 === void 0 ? void 0 : _dataContext$props2.isolate, multiline, onKeyDown]);
const cn = (0, _classnames.default)('dnb-forms-field-string__input', inputClassName);
const sharedProps = {
id,
name,
autoComplete,
autoCorrect,
spellCheck,
autoFocus,
autoCapitalize,
inputMode,
className: cn,
placeholder,
on_focus: handleFocus,
on_blur: handleBlur,
on_change: handleChange,
on_key_down: handleKeyDown,
disabled,
...htmlAttributes,
stretch: Boolean(width),
inner_ref: innerRef,
status: hasError ? 'error' : undefined,
value: transformInstantly((_value$toString = value === null || value === void 0 ? void 0 : value.toString()) !== null && _value$toString !== void 0 ? _value$toString : '')
};
const textareaProps = {
keepPlaceholder,
rows,
autoresize_max_rows: autoresizeMaxRows,
autoresize,
characterCounter
};
const inputProps = {
type,
clear,
size,
align,
selectall,
icon: leftIcon !== null && leftIcon !== void 0 ? leftIcon : rightIcon,
icon_position: rightIcon && !leftIcon ? 'right' : undefined,
submit_element: submitElement,
keep_placeholder: keepPlaceholder
};
const fieldBlockProps = {
forId: id,
className: (0, _classnames.default)('dnb-forms-field-string', className),
width: width === 'stretch' || fieldBlockContext !== null && fieldBlockContext !== void 0 && fieldBlockContext.composition ? width : undefined,
contentWidth: width !== false ? width : undefined,
...(0, _utils.pickSpacingProps)(props)
};
return _react.default.createElement(_index2.default, fieldBlockProps, multiline ? _react.default.createElement(_index.Textarea, _extends({}, sharedProps, textareaProps)) : mask ? _react.default.createElement(_InputMasked.default, _extends({}, sharedProps, inputProps, {
mask: mask
})) : _react.default.createElement(_index.Input, _extends({}, sharedProps, inputProps)));
}
StringComponent._supportsSpacingProps = true;
var _default = exports.default = StringComponent;
//# sourceMappingURL=String.js.map