Outlook_Addin_LLM/node_modules/@fluentui/react-tabster/lib/hooks/useMergeTabsterAttributes.js

48 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as React from 'react';
import { TABSTER_ATTRIBUTE_NAME } from 'tabster';
/**
* Merges a collection of tabster attributes.
*
* ⚠The attributes passed as arguments to this hook cannot change at runtime.
* @internal
* @param attributes - collection of tabster attributes from other react-tabster hooks
* @returns single merged tabster attribute
*/ export const useMergedTabsterAttributes_unstable = (...attributes)=>{
'use no memo';
const stringAttributes = attributes.map((attribute)=>attribute[TABSTER_ATTRIBUTE_NAME]).filter(Boolean);
return React.useMemo(()=>{
let attribute = stringAttributes[0];
attributes.shift();
for (const attr of stringAttributes){
attribute = mergeAttributes(attribute, attr);
}
return {
[TABSTER_ATTRIBUTE_NAME]: attribute
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, stringAttributes);
};
function mergeAttributes(a, b) {
if (!b) {
return a;
}
let aParsed = {};
let bParsed = {};
if (a) {
try {
aParsed = JSON.parse(a);
// eslint-disable-next-line no-empty
} catch {}
}
if (b) {
try {
bParsed = JSON.parse(b);
// eslint-disable-next-line no-empty
} catch {}
}
return JSON.stringify({
...aParsed,
...bParsed
});
}