-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch_delay_field.js
More file actions
58 lines (49 loc) · 1.81 KB
/
search_delay_field.js
File metadata and controls
58 lines (49 loc) · 1.81 KB
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
54
55
56
57
58
/** @odoo-module **/
import {Component, useEffect, useRef, onWillUnmount} from "@odoo/owl";
import {_t} from "@web/core/l10n/translation";
import {useDebounced} from "@web/core/utils/timing";
import {registry} from "@web/core/registry";
import {standardFieldProps} from "@web/views/fields/standard_field_props";
/**
* Char field that triggers onchange after a typing delay (500ms).
* Used for search fields where we want live results without waiting for blur.
*/
export class SearchDelayField extends Component {
static template = "spp_change_request_v2.SearchDelayField";
static props = {
...standardFieldProps,
placeholder: {type: String, optional: true},
};
setup() {
this.inputRef = useRef("input");
this.debouncedCommit = useDebounced((value) => {
this.props.record.update({[this.props.name]: value});
}, 500);
// Keep input in sync when record updates externally (e.g. onchange clears it)
useEffect(
() => {
const el = this.inputRef.el;
if (el) {
const recordValue = this.props.record.data[this.props.name] || "";
if (el !== document.activeElement || !recordValue) {
el.value = recordValue;
}
}
},
() => [this.props.record.data[this.props.name]]
);
}
get value() {
return this.props.record.data[this.props.name] || "";
}
onInput(ev) {
this.debouncedCommit(ev.target.value);
}
}
export const searchDelayField = {
component: SearchDelayField,
displayName: _t("Search with Delay"),
supportedTypes: ["char"],
extractProps: ({placeholder}) => ({placeholder}),
};
registry.category("fields").add("search_delay", searchDelayField);