@i-novus/ui-core
Version:
Базовые UI компоненты
30 lines (29 loc) • 1.81 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { cloneElement, isValidElement, Children } from 'react';
import classNames from 'classnames';
import { useConfigProvider } from '../core';
export const Field = ({ prefix, className, style, label, required = false, children, error, labelStyle, labelPosition = 'top-left', labelClassName, errorClassName, errorStyle, inputStyle, visible = true, }) => {
const { getPrefix } = useConfigProvider();
const prefixCls = getPrefix(prefix);
if (!isValidElement(children) || !visible) {
return null;
}
return (_jsxs("div", { style: style, className: classNames(`${prefixCls}-field`, {
[`${prefixCls}-field_required`]: required,
[`${prefixCls}-field_label_${labelPosition}`]: Boolean(label),
[`${prefixCls}-field_error`]: Boolean(error),
}, className), children: [label && (_jsx("span", { className: classNames(`${prefixCls}-field__label`, labelClassName), style: labelStyle, children: label })), error && (_jsx("span", { className: classNames(errorClassName, `${prefixCls}-field__error`), style: errorStyle, children: error })), Children.map(children, (child) => {
if (child && typeof child === 'object' && Object.hasOwn(child, 'props')) {
const childInput = child;
return cloneElement(childInput, {
...childInput.props,
error,
required,
className: classNames(`${prefixCls}-field__control`, childInput.props?.className),
style: { ...childInput.props?.style, ...inputStyle },
});
}
return null;
})] }));
};
Field.displayName = 'Field';