Skip to content

Commit

Permalink
refactor(input): remove unnecessary assertions and use?? instead of t…
Browse files Browse the repository at this point in the history
…he ternary operator (vueComponent#7571)

* refactor: remove unnecessary assertions and use?? instead of the ternary operator

* refactor: use?? instead of the ternary operator
  • Loading branch information
lxKylin authored Jun 4, 2024
1 parent 312bcc5 commit 4318147
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion components/input-number/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const InputNumber = defineComponent({

const mergedSize = computed(() => compactSize.value || size.value);

const mergedValue = shallowRef(props.value === undefined ? props.defaultValue : props.value);
const mergedValue = shallowRef(props.value ?? props.defaultValue);
const focused = shallowRef(false);
watch(
() => props.value,
Expand Down
2 changes: 1 addition & 1 deletion components/input/Password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default defineComponent({
});
const getIcon = (prefixCls: string) => {
const { action, iconRender = slots.iconRender || defaultIconRender } = props;
const iconTrigger = ActionMap[action!] || '';
const iconTrigger = ActionMap[action] || '';
const icon = iconRender(visible.value);
const iconProps = {
[iconTrigger]: onVisibleChange,
Expand Down
18 changes: 9 additions & 9 deletions components/input/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ function setTriggerValue(
let newTriggerValue = triggerValue;
if (isCursorInEnd) {
// 光标在尾部,直接截断
newTriggerValue = fixEmojiLength(triggerValue, maxLength!);
newTriggerValue = fixEmojiLength(triggerValue, maxLength);
} else if (
[...(preValue || '')].length < triggerValue.length &&
[...(triggerValue || '')].length > maxLength!
[...(triggerValue || '')].length > maxLength
) {
// 光标在中间,如果最后的值超过最大值,则采用原先的值
newTriggerValue = preValue;
Expand All @@ -58,7 +58,7 @@ export default defineComponent({
const formItemContext = useInjectFormItemContext();
const formItemInputContext = FormItemInputContext.useInject();
const mergedStatus = computed(() => getMergedStatus(formItemInputContext.status, props.status));
const stateValue = shallowRef(props.value === undefined ? props.defaultValue : props.value);
const stateValue = shallowRef(props.value ?? props.defaultValue);
const resizableTextArea = shallowRef();
const mergedValue = shallowRef('');
const { prefixCls, size, direction } = useConfigInject('input', props);
Expand All @@ -79,7 +79,7 @@ export default defineComponent({
const onInternalCompositionStart = (e: CompositionEvent) => {
compositing.value = true;
// 拼音输入前保存一份旧值
oldCompositionValueRef.value = mergedValue.value as string;
oldCompositionValueRef.value = mergedValue.value;
// 保存旧的光标位置
oldSelectionStartRef.value = (e.currentTarget as any).selectionStart;
emit('compositionstart', e);
Expand All @@ -94,7 +94,7 @@ export default defineComponent({
oldSelectionStartRef.value === oldCompositionValueRef.value?.length;
triggerValue = setTriggerValue(
isCursorInEnd,
oldCompositionValueRef.value as string,
oldCompositionValueRef.value,
triggerValue,
props.maxlength,
);
Expand Down Expand Up @@ -177,14 +177,14 @@ export default defineComponent({
// 1. 复制粘贴超过maxlength的情况 2.未超过maxlength的情况
const target = e.target as any;
const isCursorInEnd =
target.selectionStart >= props.maxlength! + 1 ||
target.selectionStart >= props.maxlength + 1 ||
target.selectionStart === triggerValue.length ||
!target.selectionStart;
triggerValue = setTriggerValue(
isCursorInEnd,
mergedValue.value as string,
mergedValue.value,
triggerValue,
props.maxlength!,
props.maxlength,
);
}
resolveOnChange(e.currentTarget as any, e, triggerChange, triggerValue);
Expand Down Expand Up @@ -237,7 +237,7 @@ export default defineComponent({
});

watchEffect(() => {
let val = fixControlledValue(stateValue.value) as string;
let val = fixControlledValue(stateValue.value);
if (
!compositing.value &&
hasMaxLength.value &&
Expand Down
7 changes: 3 additions & 4 deletions components/input/calculateNodeHeight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ const computedStyleCache: Record<string, NodeType> = {};
let hiddenTextarea: HTMLTextAreaElement;

export function calculateNodeStyling(node: HTMLElement, useCache = false) {
const nodeRef = (node.getAttribute('id') ||
node.getAttribute('data-reactid') ||
node.getAttribute('name')) as string;
const nodeRef =
node.getAttribute('id') || node.getAttribute('data-reactid') || node.getAttribute('name');

if (useCache && computedStyleCache[nodeRef]) {
return computedStyleCache[nodeRef];
Expand Down Expand Up @@ -103,7 +102,7 @@ export default function calculateAutoSizeStyle(
// Fix wrap="off" issue
// https://github.com/ant-design/ant-design/issues/6577
if (uiTextNode.getAttribute('wrap')) {
hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap') as string);
hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap'));
} else {
hiddenTextarea.removeAttribute('wrap');
}
Expand Down

0 comments on commit 4318147

Please sign in to comment.