@carbon/react
Version:
React components for the Carbon Design System
70 lines (62 loc) • 2.23 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
var iconsReact = require('@carbon/icons-react');
require('../components/Text/index.js');
var usePrefix = require('./usePrefix.js');
var Text = require('../components/Text/Text.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
/**
* Returns an object containing normalized properties for an input component.
*
* This hook ensures that only one of `invalid` or `warn` is active (with
* `invalid` taking precedence) and that `readOnly` overrides the `disabled`,
* `invalid`, and `warn` states. It generates unique IDs for error, warning, and
* helper messages, and conditionally provides the appropriate validation
* message and accompanying icon.
*/
const useNormalizedInputProps = ({
id,
readOnly,
disabled,
invalid,
invalidText,
warn,
warnText
}) => {
const prefix = usePrefix.usePrefix();
const normalizedProps = {
disabled: !readOnly && disabled,
invalid: !readOnly && invalid,
invalidId: `${id}-error-msg`,
warn: !readOnly && !invalid && warn,
warnId: `${id}-warn-msg`,
validation: null,
icon: null,
helperId: `${id}-helper-text`
};
if (normalizedProps.invalid) {
normalizedProps.icon = iconsReact.WarningFilled;
normalizedProps.validation = /*#__PURE__*/React__default["default"].createElement(Text.Text, {
as: "div",
className: `${prefix}--form-requirement`,
id: normalizedProps.invalidId
}, invalidText);
} else if (normalizedProps.warn) {
normalizedProps.icon = iconsReact.WarningAltFilled;
normalizedProps.validation = /*#__PURE__*/React__default["default"].createElement(Text.Text, {
as: "div",
className: `${prefix}--form-requirement`,
id: normalizedProps.warnId
}, warnText);
}
return normalizedProps;
};
exports.useNormalizedInputProps = useNormalizedInputProps;