@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
100 lines (99 loc) • 3.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ensureTextNode = ensureTextNode;
exports.listAllSections = listAllSections;
exports.pickSectionDomProps = pickSectionDomProps;
exports.setSectionDomApi = setSectionDomApi;
function setSectionDomApi({
element,
inputId,
displayValue,
caretPositionsRef,
sectionSelectionModeRef,
selectSection,
setSectionCaret
}) {
Object.defineProperties(element, {
value: {
configurable: true,
get() {
return displayValue;
}
},
inputMode: {
configurable: true,
get() {
var _element$getAttribute;
return (_element$getAttribute = element.getAttribute('inputmode')) !== null && _element$getAttribute !== void 0 ? _element$getAttribute : '';
}
},
selectionStart: {
configurable: true,
get() {
var _caretPositionsRef$cu;
return sectionSelectionModeRef.current[inputId] === 'all' ? 0 : (_caretPositionsRef$cu = caretPositionsRef.current[inputId]) !== null && _caretPositionsRef$cu !== void 0 ? _caretPositionsRef$cu : displayValue.length;
}
},
selectionEnd: {
configurable: true,
get() {
var _caretPositionsRef$cu2;
return sectionSelectionModeRef.current[inputId] === 'all' ? displayValue.length : (_caretPositionsRef$cu2 = caretPositionsRef.current[inputId]) !== null && _caretPositionsRef$cu2 !== void 0 ? _caretPositionsRef$cu2 : displayValue.length;
}
},
setSelectionRange: {
configurable: true,
value(start, end) {
element.focus();
if (start === 0 && end >= displayValue.length) {
selectSection(inputId);
return;
}
setSectionCaret(inputId, end);
}
},
select: {
configurable: true,
value() {
element.focus();
selectSection(inputId);
}
}
});
if (sectionSelectionModeRef.current[inputId] !== 'all') {
var _caretPositionsRef$cu3;
caretPositionsRef.current[inputId] = Math.min((_caretPositionsRef$cu3 = caretPositionsRef.current[inputId]) !== null && _caretPositionsRef$cu3 !== void 0 ? _caretPositionsRef$cu3 : displayValue.length, displayValue.length);
}
}
function pickSectionDomProps(props) {
if (!props) {
return {};
}
return Object.entries(props).reduce((acc, [key, value]) => {
if (key === 'autoComplete' || key === 'inputMode' || key === 'lang' || key === 'dir' || key === 'title' || key === 'tabIndex' || key === 'disabled' || key === 'required' || key === 'readOnly' || key.startsWith('aria-') || key.startsWith('data-')) {
acc[key] = value;
}
return acc;
}, {});
}
function ensureTextNode(element) {
const firstChild = element.firstChild;
if ((firstChild === null || firstChild === void 0 ? void 0 : firstChild.nodeType) === Node.TEXT_NODE) {
return firstChild;
}
const textNode = document.createTextNode(element.textContent || '');
element.textContent = '';
element.appendChild(textNode);
return textNode;
}
function listAllSections(scope) {
try {
const root = scope || document;
return Array.from(root.querySelectorAll('.dnb-segmented-field__section'));
} catch {
return [];
}
}
//# sourceMappingURL=dom.js.map