feature: implament clear button

This commit is contained in:
Sagi 2022-09-17 12:46:22 +08:00
parent 10ef216d4e
commit db564e36ff
5 changed files with 37 additions and 26 deletions

View File

@ -1,4 +1,4 @@
import { defineComponent, computed, toRefs } from 'vue'; import { defineComponent, computed, toRefs, ref } from 'vue';
import type { SetupContext } from 'vue'; import type { SetupContext } from 'vue';
import { buttonEditProps, ButtonEditProps } from './button-edit.props'; import { buttonEditProps, ButtonEditProps } from './button-edit.props';
import { useButton } from './composition/use-button'; import { useButton } from './composition/use-button';
@ -12,12 +12,13 @@ export default defineComponent({
'updateExtendInfo', 'updateExtendInfo',
'clear', 'clear',
'change', 'change',
'clickHandle', 'click',
'blurHandle', 'clickButton',
'focusHandle', 'blur',
'enterHandle', 'focus',
'iconMouseEnter', 'mouseEnter',
'iconMouseLeave', 'mouseEnterIcon',
'mouseLeaveIcon',
'keyup', 'keyup',
'keydown', 'keydown',
'inputClick', 'inputClick',
@ -25,11 +26,11 @@ export default defineComponent({
'update:modelValue', 'update:modelValue',
], ],
setup(props: ButtonEditProps, context: SetupContext) { setup(props: ButtonEditProps, context: SetupContext) {
const { modelValue } = toRefs(props); const modelValue = ref(props.modelValue);
const { buttonClass, onClickButton, onMouseEnterButton, onMouseLeaveButton } = useButton(props, context); const { buttonClass, onClickButton, onMouseEnterButton, onMouseLeaveButton } = useButton(props, context);
const { showClearButton, onClearValue, onMouseEnterTextBox, onMouseLeaveTextBox } = useClear(props, context); const { showClearButton, onClearValue, onMouseEnterTextBox, onMouseLeaveTextBox } = useClear(props, context, modelValue);
const { const {
hasFocusedTextBox, hasFocusedTextBox,
@ -45,7 +46,7 @@ export default defineComponent({
onKeyUpTextBox, onKeyUpTextBox,
onMouseDownTextBox, onMouseDownTextBox,
onTextBoxValueChange, onTextBoxValueChange,
} = useTextBox(props, context); } = useTextBox(props, context, modelValue);
const inputGroupClass = computed(() => ({ const inputGroupClass = computed(() => ({
'input-group': true, 'input-group': true,
@ -73,6 +74,7 @@ export default defineComponent({
type={props.inputType} type={props.inputType}
value={modelValue.value} value={modelValue.value}
onBlur={onBlurTextBox} onBlur={onBlurTextBox}
onChange={onTextBoxValueChange}
onClick={onClickTextBox} onClick={onClickTextBox}
onFocus={onFocusTextBox} onFocus={onFocusTextBox}
onInput={onInput} onInput={onInput}

View File

@ -60,6 +60,10 @@ export interface UseTextBox {
* *
*/ */
textBoxTitle: ComputedRef<string>; textBoxTitle: ComputedRef<string>;
/**
* change事件
*/
changeTextBoxValue: (newValue: string, showEmitChangeEmit: boolean) => void;
/** /**
* *
*/ */
@ -91,5 +95,5 @@ export interface UseTextBox {
/** /**
* *
*/ */
onTextBoxValueChange: (value: string, emit: boolean) => void; onTextBoxValueChange: ($event: Event) => void;
} }

View File

@ -12,7 +12,7 @@ export function useButton(props: ButtonEditProps, context: SetupContext): UseBut
function onClickButton($event: Event) { function onClickButton($event: Event) {
if (canClickAppendButton.value) { if (canClickAppendButton.value) {
context.emit('clickButton', { origin: $event, value: this.value }); context.emit('clickButton', { origin: $event, value: props.modelValue });
} }
$event.stopPropagation(); $event.stopPropagation();
} }

View File

@ -1,13 +1,13 @@
import { computed, ref, SetupContext } from 'vue'; import { computed, Ref, ref, SetupContext } from 'vue';
import { ButtonEditProps } from '../button-edit.props'; import { ButtonEditProps } from '../button-edit.props';
import { UseClear } from './types'; import { UseClear } from './types';
import { useTextBox } from './use-text-box'; import { useTextBox } from './use-text-box';
export function useClear(props: ButtonEditProps, context: SetupContext): UseClear { export function useClear(props: ButtonEditProps, context: SetupContext, modelValue: Ref<string>): UseClear {
const hasShownClearButton = ref(false); const hasShownClearButton = ref(false);
const showClearButton = computed(() => props.enableClear && !props.readonly && !props.disable); const showClearButton = computed(() => props.enableClear && !props.readonly && !props.disable);
const { onTextBoxValueChange } = useTextBox(props, context); const { changeTextBoxValue } = useTextBox(props, context, modelValue);
function toggleClearIcon($event: any, isShow = false) { function toggleClearIcon($event: any, isShow = false) {
const display = isShow ? '' : 'none'; const display = isShow ? '' : 'none';
@ -22,7 +22,7 @@ export function useClear(props: ButtonEditProps, context: SetupContext): UseClea
const flag2 = !props.editable; const flag2 = !props.editable;
$event.stopPropagation(); $event.stopPropagation();
if (flag1 || flag2) { if (flag1 || flag2) {
onTextBoxValueChange('', false); changeTextBoxValue('', false);
hasShownClearButton.value = false; hasShownClearButton.value = false;
context.emit('clear'); context.emit('clear');
} }
@ -32,7 +32,7 @@ export function useClear(props: ButtonEditProps, context: SetupContext): UseClea
if (!showClearButton.value) { if (!showClearButton.value) {
return; return;
} }
if (this.value) { if (modelValue.value) {
if (!props.editable) { if (!props.editable) {
if (!props.disable) { if (!props.disable) {
hasShownClearButton.value = true; hasShownClearButton.value = true;
@ -42,7 +42,7 @@ export function useClear(props: ButtonEditProps, context: SetupContext): UseClea
} }
} }
if (hasShownClearButton.value) { if (hasShownClearButton.value) {
this.toggleClearIcon($event, true); toggleClearIcon($event, true);
} }
} }

View File

@ -1,10 +1,9 @@
import { isContext } from 'vm'; import { computed, Ref, SetupContext, watch } from 'vue';
import { computed, ref, SetupContext, watch } from 'vue';
import { ButtonEditProps } from '../button-edit.props'; import { ButtonEditProps } from '../button-edit.props';
import { UseTextBox } from './types'; import { UseTextBox } from './types';
export function useTextBox(props: ButtonEditProps, context: SetupContext): UseTextBox { export function useTextBox(props: ButtonEditProps, context: SetupContext, modelValue: Ref<string>): UseTextBox {
const textBoxTitle = computed(() => (props.enableTitle ? props.modelValue : '')); const textBoxTitle = computed(() => (props.enableTitle ? modelValue.value : ''));
const textBoxPlaceholder = computed(() => ((props.disable || props.readonly) && !props.forcePlaceholder ? '' : props.placeholder)); const textBoxPlaceholder = computed(() => ((props.disable || props.readonly) && !props.forcePlaceholder ? '' : props.placeholder));
@ -68,21 +67,27 @@ export function useTextBox(props: ButtonEditProps, context: SetupContext): UseTe
context.emit('keyup', $event); context.emit('keyup', $event);
} }
function onTextBoxValueChange(newValue: string, emit = true) { function changeTextBoxValue(newValue: string, showEmitChangeEmit = true) {
if (props.modelValue !== newValue) { if (modelValue.value !== newValue) {
props.modelValue = newValue; modelValue.value = newValue;
if (emit) { if (showEmitChangeEmit) {
context.emit('change', newValue); context.emit('change', newValue);
} }
} }
} }
function onTextBoxValueChange($event: Event) {
const newValue = ($event.target as HTMLInputElement).value;
changeTextBoxValue(newValue);
}
return { return {
hasFocusedTextBox, hasFocusedTextBox,
isTextBoxReadonly, isTextBoxReadonly,
textBoxClass, textBoxClass,
textBoxPlaceholder, textBoxPlaceholder,
textBoxTitle, textBoxTitle,
changeTextBoxValue,
onBlurTextBox, onBlurTextBox,
onClickTextBox, onClickTextBox,
onFocusTextBox, onFocusTextBox,