-
Notifications
You must be signed in to change notification settings - Fork 18
/
renderer.ts
53 lines (44 loc) · 1.26 KB
/
renderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { uuid } from '@refinitiv-ui/utils/uuid.js';
import { updateElementContent } from './utils.js';
import { AutosuggestQuery, AutosuggestRenderer, Suggestion, AutosuggestItem } from './types.js';
/**
* Build item element from data object
* @param suggestion Suggestion data
* @param query A query data (usually string, but could be any entity )
* @returns item
*/
export const renderer: AutosuggestRenderer = (suggestion: AutosuggestItem, query: AutosuggestQuery | null): HTMLElement => {
if (typeof suggestion !== 'object') {
const value = suggestion as string || '';
suggestion = {
label: value,
value
};
}
const {
type,
label,
title,
icon,
disabled,
value,
id
} = (suggestion as Suggestion);
const el = document.createElement('ef-item');
const isTextType = !type || type === 'text';
const elId = id || (isTextType ? uuid() : '');
if (elId) {
el.id = elId;
}
el.tabIndex = -1;
el.setAttribute('role', isTextType ? 'option' : 'presentation');
el.type = type || null;
el.disabled = !!disabled;
el.icon = icon || null;
el.value = value || label || '';
if (title) {
el.title = title;
}
updateElementContent(el, query as string || '', label || '', el.value);
return el;
};