(props, context.emit, 'value', 'change');
const status = props.status || 'default';
const renderType = ref(props.type);
- const { focused } = useFocus(inputRef, { initialValue: props.autofocus });
+ const focused = ref(false);
const inputClasses = computed(() => [
`${inputClass.value}__control`,
@@ -64,6 +62,14 @@ export default defineComponent({
[`${inputClass.value}--border`]: !props.borderless,
},
]);
+ const showClear = computed(() => {
+ if (isDisabled.value || props.readonly === true) return false;
+
+ if (props.clearable && innerValue.value && innerValue.value.length > 0) {
+ return props.clearTrigger === 'always' || (props.clearTrigger === 'focus' && focused.value);
+ }
+ return false;
+ });
const setInputValue = (v: InputValue = '') => {
const input = inputRef.value as HTMLInputElement;
@@ -102,24 +108,30 @@ export default defineComponent({
const focus = () => {
focused.value = true;
+ inputRef.value?.focus();
};
const blur = () => {
focused.value = false;
- // inputRef.value?.blur();
+ inputRef.value?.blur();
};
extendAPI({ focus, blur });
- const handleClear = (e: MouseEvent) => {
+ const handleClear = (e: TouchEvent) => {
+ e.preventDefault();
innerValue.value = '';
- focused.value = true;
+ focus();
props.onClear?.({ e });
};
+
const handleFocus = (e: FocusEvent) => {
+ focused.value = true;
props.onFocus?.(innerValue.value, { e });
};
+
const handleBlur = (e: FocusEvent) => {
+ focused.value = false;
props.onBlur?.(innerValue.value, { e });
};
@@ -128,16 +140,22 @@ export default defineComponent({
};
const handlePwdIconClick = () => {
+ if (isDisabled.value) return;
+
renderType.value = renderType.value === 'password' ? 'text' : 'password';
};
- watch(autofocus, (autofocus, prevAutofocus) => {
- if (autofocus === true) {
- nextTick(() => {
- focused.value = true;
- });
- }
- });
+ watch(
+ () => props.autofocus,
+ (v) => {
+ if (v === true) {
+ nextTick(() => {
+ focus();
+ });
+ }
+ },
+ { immediate: true },
+ );
watch(
() => props.type,
@@ -148,9 +166,9 @@ export default defineComponent({
);
return () => {
- const readerPrefix = () => {
- const prefixIcon = readerTNodeJSX('prefixIcon');
- const label = readerTNodeJSX('label');
+ const renderPrefix = () => {
+ const prefixIcon = renderTNodeJSX('prefixIcon');
+ const label = renderTNodeJSX('label');
return (
@@ -159,26 +177,27 @@ export default defineComponent({
);
};
- const readerClearable = () => {
- if (props.clearable && innerValue.value && innerValue.value.length > 0) {
+ const renderClearable = () => {
+ if (showClear.value) {
return (
-