@crossed/ui
Version:
A universal & performant styling library for React Native, Next.js & React
145 lines (144 loc) • 6.33 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Input_exports = {};
__export(Input_exports, {
Input: () => Input
});
module.exports = __toCommonJS(Input_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react_native = require("react-native");
var import_react = require("react");
var import_form = require("../styles/form");
var import_gap = require("../styles/gap");
var import_styled = require("@crossed/styled");
var import_Form = require("./Form");
var import_CloseButton = require("../buttons/CloseButton");
var import_core = require("@crossed/core");
var import_XBox = require("../layout/XBox");
var import_Text = require("../typography/Text");
var import_YBox = require("../layout/YBox");
const styles = (0, import_styled.createStyles)(() => ({
close: { base: { padding: 0 } }
}));
const Input = (0, import_react.forwardRef)((allProps, ref) => {
const {
error,
label,
clearable,
defaultValue,
value: valueProps,
onChangeText,
disabled,
elementRight,
elementLeft,
description,
extra,
formFieldStyle,
...props
} = allProps;
const [value, setValue] = (0, import_core.useUncontrolled)({
value: valueProps,
defaultValue,
onChange: onChangeText
});
const [elementLeftWidth, setElementLeftWidth] = (0, import_react.useState)(0);
const [elementRightWidth, setElementRightWidth] = (0, import_react.useState)(0);
const { state, props: propsInteraction } = (0, import_styled.useInteraction)(allProps);
const { colors } = (0, import_styled.useTheme)();
const color = colors.text.secondary;
const onClear = (0, import_react.useCallback)(() => {
setValue("");
}, [setValue]);
const showClear = !!(clearable && value);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_Form.FormField,
{
disabled: disabled || !props.focusable && props.focusable !== void 0,
...(0, import_styled.composeStyles)(
(0, import_styled.inlineStyle)(() => ({ base: { flexGrow: 1 } })),
formFieldStyle
).rnw(),
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_YBox.YBox, { space: "xxs", children: [
!!(label || description || extra) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_XBox.XBox, { alignItems: "center", space: "xxs", children: [
!!label && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Form.FormLabel, { children: label }),
!!description && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Text.Text, { style: import_form.form.labelDescription, children: description }),
!!extra && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Text.Text, { style: import_form.form.labelExtra, textAlign: "right", children: extra })
] }),
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_XBox.XBox, { children: [
elementLeft && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_XBox.XBox,
{
style: import_form.form.elementLeft,
onLayout: ({ nativeEvent: { layout } }) => setElementLeftWidth(layout.width),
children: (0, import_react.isValidElement)(elementLeft) && typeof elementLeft.type !== "string" && elementLeft.type.displayName === "CrossedText" ? (0, import_react.cloneElement)(elementLeft, {
style: [elementLeft.style, { color }]
}) : elementLeft
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Form.FormControl, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_react_native.TextInput,
{
ref,
placeholderTextColor: color,
cursorColor: color,
editable: !disabled,
focusable: !disabled,
...props,
...propsInteraction,
...(0, import_styled.composeStyles)(
import_form.form.input,
error && import_form.form.inputError,
props.style
).rnw({
...props,
...state,
disabled,
style: [
elementLeftWidth && { paddingLeft: elementLeftWidth },
elementRightWidth && { paddingRight: elementRightWidth }
]
}),
value,
onChangeText: setValue
}
) }),
(!!elementRight || !!showClear) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
import_XBox.XBox,
{
style: (0, import_styled.composeStyles)(import_form.form.elementRight, import_gap.gapStyles.xs),
onLayout: ({ nativeEvent: { layout } }) => setElementRightWidth(layout.width),
children: [
(0, import_react.isValidElement)(elementRight) && typeof elementRight.type !== "string" && elementRight.type.displayName === "CrossedText" ? (0, import_react.cloneElement)(elementRight, {
style: [elementRight.style, { color }]
}) : elementRight,
!!showClear && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_CloseButton.CloseButton, { onPress: onClear, style: styles.close })
]
}
)
] }),
error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Text.Text, { color: "error", children: error.toString() })
] })
}
);
});
Input.displayName = "Input";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Input
});
//# sourceMappingURL=Input.js.map