@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
231 lines (230 loc) • 8.13 kB
JavaScript
"use client";
import React, { useContext, useMemo, useCallback, useEffect, useRef } from 'react';
import clsx from 'clsx';
import { Input, Textarea } from "../../../../components/index.js";
import * as z from 'zod';
import InputMasked from "../../../../components/InputMasked.js";
import DataContext from "../../DataContext/Context.js";
import FieldBlockContext from "../../FieldBlock/FieldBlockContext.js";
import FieldBlock from "../../FieldBlock/index.js";
import { useFieldProps } from "../../hooks/index.js";
import { pickSpacingProps } from "../../../../components/flex/utils.js";
import { toCapitalized } from "../../../../shared/component-helper.js";
import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
function StringComponent(props) {
var _props$fromInput, _props$width, _props$ref, _dataContext$props2, _value$toString;
const dataContext = useContext(DataContext);
const fieldBlockContext = useContext(FieldBlockContext);
const schema = 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 = 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 = 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 = useCallback(value => {
if (props.capitalize) {
value = toCapitalized(String(value || ''));
}
return (transform === null || transform === void 0 ? void 0 : transform(value)) || value;
}, [props.capitalize, transform]);
const ref = useRef(undefined);
const preparedProps = {
...props,
schema,
fromInput: (_props$fromInput = props.fromInput) !== null && _props$fromInput !== void 0 ? _props$fromInput : fromInput,
toEvent,
transformValue,
width: (_props$width = props.width) !== null && _props$width !== void 0 ? _props$width : fieldBlockContext !== null && fieldBlockContext !== void 0 && fieldBlockContext.composition ? 'stretch' : 'large',
ref: (_props$ref = props.ref) !== null && _props$ref !== void 0 ? _props$ref : ref
};
const {
id,
name,
className,
ref: inputRef,
inputClassName,
placeholder,
value,
hasError,
disabled,
multiline,
mask,
allowOverflow,
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
} = useFieldProps(preparedProps);
useEffect(() => {
const input = id ? document.getElementById(id) : null;
setDisplayValue(input === null || input === void 0 ? void 0 : input.value);
}, [id, setDisplayValue, value]);
const transformInstantly = useCallback(value => props.capitalize ? toCapitalized(value) : value, [props.capitalize]);
const {
handleSubmit
} = dataContext !== null && dataContext !== void 0 ? dataContext : {};
const handleKeyDown = 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 = clsx('dnb-forms-field-string__input', inputClassName);
const sharedProps = {
id,
name,
autoComplete,
autoCorrect,
spellCheck,
autoFocus,
autoCapitalize,
inputMode,
className: cn,
placeholder,
onFocus: handleFocus,
onBlur: handleBlur,
onChange: handleChange,
onKeyDown: handleKeyDown,
disabled,
...htmlAttributes,
stretch: Boolean(width),
ref: inputRef,
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,
autoResizeMaxRows: autoResizeMaxRows,
autoResize,
characterCounter
};
const inputProps = {
type,
clear,
size,
align,
selectAll,
icon: leftIcon !== null && leftIcon !== void 0 ? leftIcon : rightIcon,
iconPosition: rightIcon && !leftIcon ? 'right' : undefined,
submitElement: submitElement,
keepPlaceholder: keepPlaceholder
};
const fieldBlockProps = {
forId: id,
className: clsx('dnb-forms-field-string', className),
width: width === 'stretch' || fieldBlockContext !== null && fieldBlockContext !== void 0 && fieldBlockContext.composition ? width : undefined,
contentWidth: width !== false ? width : undefined,
...pickSpacingProps(props)
};
return _jsx(FieldBlock, {
...fieldBlockProps,
children: multiline ? _jsx(Textarea, {
...sharedProps,
...textareaProps
}) : mask ? _jsx(InputMasked, {
...sharedProps,
...inputProps,
mask: mask,
allowOverflow: allowOverflow
}) : _jsx(Input, {
...sharedProps,
...inputProps
})
});
}
withComponentMarkers(StringComponent, {
_supportsSpacingProps: true
});
export default StringComponent;
//# sourceMappingURL=String.js.map