@carbon/react
Version:
React components for the Carbon Design System
64 lines (62 loc) • 2.36 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const require_runtime = require("../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("./usePrefix.js");
const require_Text = require("../components/Text/Text.js");
let react = require("react");
react = require_runtime.__toESM(react);
let react_jsx_runtime = require("react/jsx-runtime");
let _carbon_icons_react = require("@carbon/icons-react");
//#region src/internal/useNormalizedInputProps.tsx
/**
* Copyright IBM Corp. 2021, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* 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 = require_usePrefix.usePrefix();
const normalizedProps = {
disabled: !readOnly && disabled,
invalid: !readOnly && !disabled && invalid,
invalidId: `${id}-error-msg`,
warn: !readOnly && !invalid && !disabled && warn,
warnId: `${id}-warn-msg`,
validation: null,
icon: null,
helperId: `${id}-helper-text`
};
if (normalizedProps.invalid) {
normalizedProps.icon = _carbon_icons_react.WarningFilled;
normalizedProps.validation = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Text.Text, {
as: "div",
className: `${prefix}--form-requirement`,
id: normalizedProps.invalidId,
children: invalidText
});
} else if (normalizedProps.warn) {
normalizedProps.icon = _carbon_icons_react.WarningAltFilled;
normalizedProps.validation = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Text.Text, {
as: "div",
className: `${prefix}--form-requirement`,
id: normalizedProps.warnId,
children: warnText
});
}
return normalizedProps;
};
//#endregion
exports.useNormalizedInputProps = useNormalizedInputProps;