Browse Source

refactor(input): remove unnecessary assertions and use?? instead of the ternary operator (#7571)

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

* refactor: use?? instead of the ternary operator
pull/7632/head
Kylin 6 months ago committed by GitHub
parent
commit
4318147fc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      components/input-number/index.tsx
  2. 2
      components/input/Password.tsx
  3. 18
      components/input/TextArea.tsx
  4. 7
      components/input/calculateNodeHeight.tsx

2
components/input-number/index.tsx

@ -72,7 +72,7 @@ const InputNumber = defineComponent({
const mergedSize = computed(() => compactSize.value || size.value); 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); const focused = shallowRef(false);
watch( watch(
() => props.value, () => props.value,

2
components/input/Password.tsx

@ -59,7 +59,7 @@ export default defineComponent({
}); });
const getIcon = (prefixCls: string) => { const getIcon = (prefixCls: string) => {
const { action, iconRender = slots.iconRender || defaultIconRender } = props; const { action, iconRender = slots.iconRender || defaultIconRender } = props;
const iconTrigger = ActionMap[action!] || ''; const iconTrigger = ActionMap[action] || '';
const icon = iconRender(visible.value); const icon = iconRender(visible.value);
const iconProps = { const iconProps = {
[iconTrigger]: onVisibleChange, [iconTrigger]: onVisibleChange,

18
components/input/TextArea.tsx

@ -38,10 +38,10 @@ function setTriggerValue(
let newTriggerValue = triggerValue; let newTriggerValue = triggerValue;
if (isCursorInEnd) { if (isCursorInEnd) {
// //
newTriggerValue = fixEmojiLength(triggerValue, maxLength!); newTriggerValue = fixEmojiLength(triggerValue, maxLength);
} else if ( } else if (
[...(preValue || '')].length < triggerValue.length && [...(preValue || '')].length < triggerValue.length &&
[...(triggerValue || '')].length > maxLength! [...(triggerValue || '')].length > maxLength
) { ) {
// //
newTriggerValue = preValue; newTriggerValue = preValue;
@ -58,7 +58,7 @@ export default defineComponent({
const formItemContext = useInjectFormItemContext(); const formItemContext = useInjectFormItemContext();
const formItemInputContext = FormItemInputContext.useInject(); const formItemInputContext = FormItemInputContext.useInject();
const mergedStatus = computed(() => getMergedStatus(formItemInputContext.status, props.status)); 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 resizableTextArea = shallowRef();
const mergedValue = shallowRef(''); const mergedValue = shallowRef('');
const { prefixCls, size, direction } = useConfigInject('input', props); const { prefixCls, size, direction } = useConfigInject('input', props);
@ -79,7 +79,7 @@ export default defineComponent({
const onInternalCompositionStart = (e: CompositionEvent) => { const onInternalCompositionStart = (e: CompositionEvent) => {
compositing.value = true; compositing.value = true;
// //
oldCompositionValueRef.value = mergedValue.value as string; oldCompositionValueRef.value = mergedValue.value;
// //
oldSelectionStartRef.value = (e.currentTarget as any).selectionStart; oldSelectionStartRef.value = (e.currentTarget as any).selectionStart;
emit('compositionstart', e); emit('compositionstart', e);
@ -94,7 +94,7 @@ export default defineComponent({
oldSelectionStartRef.value === oldCompositionValueRef.value?.length; oldSelectionStartRef.value === oldCompositionValueRef.value?.length;
triggerValue = setTriggerValue( triggerValue = setTriggerValue(
isCursorInEnd, isCursorInEnd,
oldCompositionValueRef.value as string, oldCompositionValueRef.value,
triggerValue, triggerValue,
props.maxlength, props.maxlength,
); );
@ -177,14 +177,14 @@ export default defineComponent({
// 1. maxlength 2.maxlength // 1. maxlength 2.maxlength
const target = e.target as any; const target = e.target as any;
const isCursorInEnd = const isCursorInEnd =
target.selectionStart >= props.maxlength! + 1 || target.selectionStart >= props.maxlength + 1 ||
target.selectionStart === triggerValue.length || target.selectionStart === triggerValue.length ||
!target.selectionStart; !target.selectionStart;
triggerValue = setTriggerValue( triggerValue = setTriggerValue(
isCursorInEnd, isCursorInEnd,
mergedValue.value as string, mergedValue.value,
triggerValue, triggerValue,
props.maxlength!, props.maxlength,
); );
} }
resolveOnChange(e.currentTarget as any, e, triggerChange, triggerValue); resolveOnChange(e.currentTarget as any, e, triggerChange, triggerValue);
@ -237,7 +237,7 @@ export default defineComponent({
}); });
watchEffect(() => { watchEffect(() => {
let val = fixControlledValue(stateValue.value) as string; let val = fixControlledValue(stateValue.value);
if ( if (
!compositing.value && !compositing.value &&
hasMaxLength.value && hasMaxLength.value &&

7
components/input/calculateNodeHeight.tsx

@ -48,9 +48,8 @@ const computedStyleCache: Record<string, NodeType> = {};
let hiddenTextarea: HTMLTextAreaElement; let hiddenTextarea: HTMLTextAreaElement;
export function calculateNodeStyling(node: HTMLElement, useCache = false) { export function calculateNodeStyling(node: HTMLElement, useCache = false) {
const nodeRef = (node.getAttribute('id') || const nodeRef =
node.getAttribute('data-reactid') || node.getAttribute('id') || node.getAttribute('data-reactid') || node.getAttribute('name');
node.getAttribute('name')) as string;
if (useCache && computedStyleCache[nodeRef]) { if (useCache && computedStyleCache[nodeRef]) {
return computedStyleCache[nodeRef]; return computedStyleCache[nodeRef];
@ -103,7 +102,7 @@ export default function calculateAutoSizeStyle(
// Fix wrap="off" issue // Fix wrap="off" issue
// https://github.com/ant-design/ant-design/issues/6577 // https://github.com/ant-design/ant-design/issues/6577
if (uiTextNode.getAttribute('wrap')) { if (uiTextNode.getAttribute('wrap')) {
hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap') as string); hiddenTextarea.setAttribute('wrap', uiTextNode.getAttribute('wrap'));
} else { } else {
hiddenTextarea.removeAttribute('wrap'); hiddenTextarea.removeAttribute('wrap');
} }

Loading…
Cancel
Save