48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
|
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
|
|||
|
});
|
|||
|
}
|