From 09e1eb5a0d4a99b31fd09c4e55acbf67120f7032 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 5 Mar 2021 11:05:27 +0800 Subject: [PATCH] chore: optional chaining (#8283) --- src/address-edit/index.tsx | 7 +++---- src/collapse-item/index.tsx | 8 ++++---- src/field/index.tsx | 15 +++------------ src/image-preview/ImagePreview.tsx | 16 +++++----------- src/search/index.tsx | 13 ++----------- src/stepper/index.tsx | 4 ++-- src/swipe/index.tsx | 8 +++++--- 7 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/address-edit/index.tsx b/src/address-edit/index.tsx index 62f2b3bb9..5d77f5526 100644 --- a/src/address-edit/index.tsx +++ b/src/address-edit/index.tsx @@ -145,10 +145,9 @@ export default createComponent({ }); // hide bottom field when use search && detail get focused - const hideBottomFields = computed(() => { - const { searchResult } = props; - return searchResult && searchResult.length && state.detailFocused; - }); + const hideBottomFields = computed( + () => props.searchResult?.length && state.detailFocused + ); const assignAreaValues = () => { if (areaRef.value) { diff --git a/src/collapse-item/index.tsx b/src/collapse-item/index.tsx index 3b35ade9b..44996496b 100644 --- a/src/collapse-item/index.tsx +++ b/src/collapse-item/index.tsx @@ -39,8 +39,8 @@ export default createComponent({ return; } - const currentName = computed(() => props.name ?? index.value); - const expanded = computed(() => parent.isExpanded(currentName.value)); + const name = computed(() => props.name ?? index.value); + const expanded = computed(() => parent.isExpanded(name.value)); const show = ref(expanded.value); const lazyRender = useLazyRender(show); @@ -86,8 +86,8 @@ export default createComponent({ }); }); - const toggle = (value = !expanded.value) => { - parent.toggle(currentName.value, value); + const toggle = (newValue = !expanded.value) => { + parent.toggle(name.value, newValue); }; const onClickTitle = () => { diff --git a/src/field/index.tsx b/src/field/index.tsx index 7d66cef70..b2f4bde45 100644 --- a/src/field/index.tsx +++ b/src/field/index.tsx @@ -283,7 +283,7 @@ export default createComponent({ value = props.formatter(value); } - if (inputRef.value && value !== inputRef.value.value) { + if (inputRef.value && inputRef.value.value !== value) { inputRef.value.value = value; } @@ -299,17 +299,8 @@ export default createComponent({ } }; - const focus = () => { - if (inputRef.value) { - inputRef.value.focus(); - } - }; - - const blur = () => { - if (inputRef.value) { - inputRef.value.blur(); - } - }; + const blur = () => inputRef.value?.blur(); + const focus = () => inputRef.value?.focus(); const onFocus = (event: Event) => { state.focused = true; diff --git a/src/image-preview/ImagePreview.tsx b/src/image-preview/ImagePreview.tsx index d0764dad8..22999f588 100644 --- a/src/image-preview/ImagePreview.tsx +++ b/src/image-preview/ImagePreview.tsx @@ -29,7 +29,7 @@ import ImagePreviewItem from './ImagePreviewItem'; const [createComponent, bem] = createNamespace('image-preview'); -export type ImagePreviewScaleEventParams = { +export type ScaleEventParams = { scale: number; index: number; }; @@ -110,8 +110,7 @@ export default createComponent({ } }; - const emitScale = (args: ImagePreviewScaleEventParams) => - emit('scale', args); + const emitScale = (args: ScaleEventParams) => emit('scale', args); const updateShow = (show: boolean) => emit('update:show', show); @@ -191,11 +190,8 @@ export default createComponent({ const onClosed = () => emit('closed'); - const swipeTo = (index: number, options?: SwipeToOptions) => { - if (swipeRef.value) { - swipeRef.value.swipeTo(index, options); - } - }; + const swipeTo = (index: number, options?: SwipeToOptions) => + swipeRef.value?.swipeTo(index, options); useExpose({ swipeTo }); @@ -205,9 +201,7 @@ export default createComponent({ watch( () => props.startPosition, - (value) => { - setActive(+value); - } + (value) => setActive(+value) ); watch( diff --git a/src/search/index.tsx b/src/search/index.tsx index 9ad0a1625..e1b79950e 100644 --- a/src/search/index.tsx +++ b/src/search/index.tsx @@ -92,17 +92,8 @@ export default createComponent({ } }; - const focus = () => { - if (filedRef.value) { - filedRef.value.focus(); - } - }; - - const blur = () => { - if (filedRef.value) { - filedRef.value.blur(); - } - }; + const blur = () => filedRef.value?.blur(); + const focus = () => filedRef.value?.focus(); const fieldPropNames = [ 'leftIcon', diff --git a/src/stepper/index.tsx b/src/stepper/index.tsx index 3653c42d1..97e9080fd 100644 --- a/src/stepper/index.tsx +++ b/src/stepper/index.tsx @@ -208,8 +208,8 @@ export default createComponent({ const onFocus = (event: Event) => { // readonly not work in lagacy mobile safari - if (props.disableInput && inputRef.value) { - inputRef.value.blur(); + if (props.disableInput) { + inputRef.value?.blur(); } else { emit('focus', event); } diff --git a/src/swipe/index.tsx b/src/swipe/index.tsx index f8a4f3786..d69fbc1e7 100644 --- a/src/swipe/index.tsx +++ b/src/swipe/index.tsx @@ -388,9 +388,11 @@ export default createComponent({ const renderDot = (_: number, index: number) => { const active = index === activeIndicator.value; - const style: CSSProperties = { - backgroundColor: active ? props.indicatorColor : undefined, - }; + const style = active + ? { + backgroundColor: props.indicatorColor, + } + : undefined; return ; };