@trail-ui/react
Version:
188 lines (182 loc) • 7.55 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/input/input.tsx
var input_exports = {};
__export(input_exports, {
Input: () => _Input
});
module.exports = __toCommonJS(input_exports);
var import_icons = require("@trail-ui/icons");
var import_shared_utils2 = require("@trail-ui/shared-utils");
var import_theme = require("@trail-ui/theme");
var import_react3 = require("react");
var import_react_aria_components = require("react-aria-components");
// src/input/use-input.ts
var import_shared_utils = require("@trail-ui/shared-utils");
var import_react2 = require("react");
var import_react_aria2 = require("react-aria");
// src/_utils/utils.tsx
var import_utils = require("@react-aria/utils");
var import_react = __toESM(require("react"));
var import_react_aria = require("react-aria");
var import_react_dom = __toESM(require("react-dom"));
var import_jsx_runtime = require("react/jsx-runtime");
if (typeof HTMLTemplateElement !== "undefined") {
const getFirstChild = Object.getOwnPropertyDescriptor(Node.prototype, "firstChild").get;
Object.defineProperty(HTMLTemplateElement.prototype, "firstChild", {
configurable: true,
enumerable: true,
get: function() {
if (this.dataset.reactAriaHidden) {
return this.content.firstChild;
} else {
return getFirstChild.call(this);
}
}
});
}
var HiddenContext = (0, import_react.createContext)(false);
var hiddenFragment = typeof DocumentFragment !== "undefined" ? new DocumentFragment() : null;
function useDOMRef(ref) {
const domRef = (0, import_react.useRef)(null);
(0, import_react.useImperativeHandle)(ref, () => domRef.current);
return domRef;
}
// src/input/use-input.ts
function useInput(props) {
const { ref, onClear, ...otherProps } = props;
const domRef = useDOMRef(ref);
const handleClear = (0, import_react2.useCallback)(() => {
if (domRef == null ? void 0 : domRef.current) {
domRef.current.value = "";
domRef.current.focus();
}
onClear == null ? void 0 : onClear();
}, [domRef, onClear]);
const { hoverProps, isHovered } = (0, import_react_aria2.useHover)({});
const { isFocused, isFocusVisible, focusProps } = (0, import_react_aria2.useFocusRing)({
isTextInput: true,
autoFocus: props.autoFocus
});
const { focusProps: clearFocusProps, isFocusVisible: isClearButtonFocusVisible } = (0, import_react_aria2.useFocusRing)();
const { pressProps: clearPressProps } = (0, import_react_aria2.usePress)({
isDisabled: !!(props == null ? void 0 : props.disabled),
onPress: handleClear
});
const inputValue = props["data-value"];
const isFilled = !!inputValue;
const isInvalid = !!props["aria-invalid"] && props["aria-invalid"] !== "false";
const getInputWrapperProps = (0, import_react2.useCallback)(
(inputWrapperProps = {}) => {
return {
"data-filled": (0, import_shared_utils.dataAttr)(isFilled),
"data-focused": (0, import_shared_utils.dataAttr)(isFocused),
"data-focus-visible": (0, import_shared_utils.dataAttr)(isFocusVisible),
"data-hovered": (0, import_shared_utils.dataAttr)(isHovered),
"data-disabled": (0, import_shared_utils.dataAttr)(props.disabled),
"data-invalid": (0, import_shared_utils.dataAttr)(isInvalid),
...inputWrapperProps
};
},
[isFilled, isFocusVisible, isFocused, isHovered, isInvalid, props.disabled]
);
const getInputProps = (0, import_react2.useCallback)(
(inputProps = {}) => {
return {
"data-filled": (0, import_shared_utils.dataAttr)(isFilled),
...(0, import_react_aria2.mergeProps)(otherProps, focusProps, hoverProps, inputProps),
ref: domRef
};
},
[domRef, focusProps, hoverProps, isFilled, otherProps]
);
const getClearButtonProps = (0, import_react2.useCallback)(
(clearButtonProps = {}) => {
return {
role: "button",
tabIndex: 0,
"data-focus-visible": (0, import_shared_utils.dataAttr)(isClearButtonFocusVisible),
...(0, import_react_aria2.mergeProps)(clearFocusProps, clearPressProps, clearButtonProps)
};
},
[clearFocusProps, clearPressProps, isClearButtonFocusVisible]
);
return {
domRef,
getInputWrapperProps,
getInputProps,
getClearButtonProps
};
}
// src/input/input.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
function Input(props, ref) {
[props, ref] = (0, import_react_aria_components.useContextProps)(props, ref, import_react_aria_components.InputContext);
const { classNames, className, variant, fullWidth, startContent, endContent, ...otherProps } = props;
const { getInputWrapperProps, getInputProps, getClearButtonProps, domRef } = useInput({
...otherProps,
ref
});
const isClearable = !!props.onClear;
const slots = (0, import_react3.useMemo)(
() => (0, import_theme.input)({ variant, fullWidth, isClearable }),
[fullWidth, variant, isClearable]
);
const end = (0, import_react3.useMemo)(() => {
if (isClearable) {
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
"span",
{
...getClearButtonProps(),
className: slots.clearButton({ class: classNames == null ? void 0 : classNames.clearButton }),
children: endContent || /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_icons.XCircleFilledIcon, {})
}
);
}
return endContent;
}, [classNames == null ? void 0 : classNames.clearButton, endContent, getClearButtonProps, isClearable, slots]);
const baseStyles = (0, import_shared_utils2.clsx)(classNames == null ? void 0 : classNames.base, className);
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: slots.base({ class: baseStyles }), ...getInputWrapperProps(), children: [
startContent,
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
"input",
{
...getInputProps(),
className: slots.input({ class: classNames == null ? void 0 : classNames.input }),
ref: domRef
}
),
end
] });
}
var _Input = (0, import_react3.forwardRef)(Input);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Input
});