@opensig/opendesign
Version:
158 lines (157 loc) • 5.17 kB
JavaScript
import { defineComponent, inject, ref, computed, onMounted, createBlock, openBlock, resolveDynamicComponent, h, unref } from "vue";
import { textareaProps } from "./types.mjs";
import { formItemInjectKey } from "../form/provide.mjs";
import _sfc_main$1 from "../_components/in-box/InBox.vue.mjs";
import _sfc_main$2 from "../_components/in-textarea/InTextarea.vue.mjs";
import { uniqueId, pick, formateToString } from "../_utils/helper.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "OTextarea",
props: textareaProps,
emits: ["update:modelValue", "change", "input", "blur", "focus", "clear"],
setup(__props, { expose: __expose, emit: __emit }) {
const props = __props;
const emits = __emit;
const formItemInjection = inject(formItemInjectKey, null);
const inTextareaRef = ref();
const color = computed(() => {
var _a;
if (formItemInjection == null ? void 0 : formItemInjection.fieldResult.value) {
return ((_a = formItemInjection == null ? void 0 : formItemInjection.fieldResult.value) == null ? void 0 : _a.type) || "normal";
} else {
return props.color;
}
});
const onInput = (e) => {
var _a, _b;
emits("input", e);
(_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onInput) == null ? void 0 : _b.call(_a);
};
const isFocus = ref(false);
const onFocus = (e) => {
var _a, _b;
if (isFocus.value) {
return;
}
isFocus.value = true;
emits("focus", e);
(_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onFocus) == null ? void 0 : _b.call(_a);
};
const onBlur = (e) => {
var _a, _b;
isFocus.value = false;
emits("blur", e);
(_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onBlur) == null ? void 0 : _b.call(_a);
};
const onClear = (e) => {
emits("clear", e);
};
const onUpdatedModelValue = (value) => {
emits("update:modelValue", value);
};
const onChange = (value) => {
var _a, _b;
emits("change", value);
(_b = formItemInjection == null ? void 0 : (_a = formItemInjection.fieldHandlers).onChange) == null ? void 0 : _b.call(_a);
};
const textareaId = ref(props.textareaId);
onMounted(() => {
if (!textareaId.value) {
textareaId.value = uniqueId();
}
});
const round = computed(() => {
return props.round === "pill" ? "var(--o-radius_control-l)" : props.round;
});
__expose({
/**
* @zh-CN 聚焦文本域
* @en-US Focus the textarea
*/
focus: () => {
var _a;
return (_a = inTextareaRef.value) == null ? void 0 : _a.focus();
},
/**
* @zh-CN 取消文本域聚焦
* @en-US Blur the textarea
*/
blur: () => {
var _a;
return (_a = inTextareaRef.value) == null ? void 0 : _a.blur();
},
/**
* @zh-CN 清空文本域内容
* @en-US Clear the textarea value
*/
clear: () => {
var _a;
return (_a = inTextareaRef.value) == null ? void 0 : _a.clear();
},
/**
* @zh-CN 获取原生 textarea 元素
* @en-US Get the native textarea element
*/
inputEl: () => {
var _a;
return (_a = inTextareaRef.value) == null ? void 0 : _a.inputEl;
}
});
return (_ctx, _cache) => {
return openBlock(), createBlock(resolveDynamicComponent(
h(
unref(_sfc_main$1),
{
class: "o-textarea",
size: props.size,
variant: props.variant,
color: color.value,
disabled: props.disabled,
readonly: props.readonly,
round: round.value,
focused: isFocus.value
},
{
default: () => h(
unref(_sfc_main$2),
{
ref: "inTextareaRef",
class: "o-textarea-textarea",
modelValue: unref(formateToString)(props.modelValue),
defaultValue: unref(formateToString)(props.defaultValue),
textareaId: textareaId.value,
...unref(pick)(props, [
"scrollbar",
"placeholder",
"disabled",
"readonly",
"clearable",
"format",
"validate",
"valueOnInvalidChange",
"autoSize",
"resize",
"rows",
"cols",
"getLength",
"maxLength",
"inputOnOutlimit",
"showLength"
]),
onChange,
onInput,
onFocus,
onBlur,
onClear,
"onUpdate:modelValue": onUpdatedModelValue
},
unref(pick)(_ctx.$slots, ["prefix", "suffix"])
)
}
)
));
};
}
});
export {
_sfc_main as default
};