@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
212 lines (211 loc) • 6.87 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import React, { useContext, useMemo, useCallback, useEffect, useRef } from 'react';
import classnames from 'classnames';
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";
function StringComponent(props) {
var _props$width, _props$innerRef, _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$cleanedValue;
if (typeof event === 'string') {
event = {
value: event
};
}
if (event?.value === '') {
return props.emptyValue;
}
return (_event$cleanedValue = event?.cleanedValue) !== null && _event$cleanedValue !== void 0 ? _event$cleanedValue : event?.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?.(value) || value;
}, [props.capitalize, transform]);
const ref = useRef();
const preparedProps = {
...props,
schema,
fromInput,
toEvent,
transformValue,
width: (_props$width = props.width) !== null && _props$width !== void 0 ? _props$width : 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
} = useFieldProps(preparedProps);
useEffect(() => {
setDisplayValue(innerRef.current?.value);
}, [innerRef, 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
}) => {
if (!multiline && dataContext?.props?.isolate && event.key === 'Enter') {
handleSubmit();
event.preventDefault?.();
}
onKeyDown?.(event);
}, [handleSubmit, dataContext?.props?.isolate, multiline, onKeyDown]);
const cn = classnames('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?.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: classnames('dnb-forms-field-string', className),
width: width === 'stretch' || fieldBlockContext?.composition ? width : undefined,
contentWidth: width !== false ? width : undefined,
...pickSpacingProps(props)
};
return React.createElement(FieldBlock, fieldBlockProps, multiline ? React.createElement(Textarea, _extends({}, sharedProps, textareaProps)) : mask ? React.createElement(InputMasked, _extends({}, sharedProps, inputProps, {
mask: mask
})) : React.createElement(Input, _extends({}, sharedProps, inputProps)));
}
StringComponent._supportsSpacingProps = true;
export default StringComponent;
//# sourceMappingURL=String.js.map