typescript
This commit is contained in:
@@ -110,6 +110,31 @@ interface QuickAddEntry {
|
||||
original: QuickAddOption | string;
|
||||
}
|
||||
|
||||
const resolveOptionName = (source: unknown): string => {
|
||||
if (!source) {
|
||||
return '';
|
||||
}
|
||||
if (typeof source === 'string') {
|
||||
return source.trim();
|
||||
}
|
||||
if (typeof source === 'object') {
|
||||
const candidate = source as { name?: string; label?: string; trim?: () => string };
|
||||
if (typeof candidate.name === 'string' && candidate.name.trim()) {
|
||||
return candidate.name.trim();
|
||||
}
|
||||
if (typeof candidate.label === 'string' && candidate.label.trim()) {
|
||||
return candidate.label.trim();
|
||||
}
|
||||
if (typeof candidate.trim === 'function') {
|
||||
const viaTrim = candidate.trim();
|
||||
if (typeof viaTrim === 'string' && viaTrim.trim()) {
|
||||
return viaTrim.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
const normalizeQuickAddOption = (option: QuickAddOption | string | null | undefined): QuickAddEntry | null => {
|
||||
if (option == null) {
|
||||
return null;
|
||||
@@ -348,16 +373,12 @@ export const CorrespondentSection: React.FC<CorrespondentSectionProps> = ({
|
||||
return;
|
||||
}
|
||||
const source = item.payload ?? item;
|
||||
const resolvedName =
|
||||
source?.name?.trim?.()
|
||||
|| source?.label?.trim?.()
|
||||
|| source?.trim?.()
|
||||
|| '';
|
||||
const resolvedName = resolveOptionName(source);
|
||||
if (!resolvedName) {
|
||||
return;
|
||||
}
|
||||
const payload = typeof source === 'object'
|
||||
? { ...source, name: resolvedName }
|
||||
const payload = (source && typeof source === 'object')
|
||||
? { ...(source as Record<string, unknown>), name: resolvedName }
|
||||
: { id: null, name: resolvedName };
|
||||
onAdd({ name: resolvedName, option: payload, input: null });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user