Debian Codegraph @

addons/point_of_sale/static/src/app/components/inputs/input/input.js

raw
1 import { useRef, useState, onPatched } from "@odoo/owl";
2 import { useAutofocus } from "@web/core/utils/hooks";
3 import { debounce } from "@web/core/utils/timing";
4 import { TModelInput } from "@point_of_sale/app/components/inputs/t_model_input";
5 /**
6 * This component is meant to provide a "batteries included" api for working
7 * with inputs. It is well suited to work as a search bar or as a monetary input.
8 * Optional props allow handling debouncing, toggling between mobile and desktop views,
9 * autofocus, validation, and more.
10 */
11 export class Input extends TModelInput {
12 static template = "point_of_sale.input";
13 static props = {
14 ...super.props,
15 isSmall: { type: Boolean, optional: true },
16 debounceMillis: { type: Number, optional: true },
17 icon: {
18 type: Object,
19 optional: true,
20 shape: { type: String, value: String },
21 },
22 getRef: { type: Function, optional: true },
23 autofocus: { type: Boolean, optional: true },
24 autofocusMobile: { type: Boolean, optional: true },
25 iconOnLeftSide: { type: Boolean, optional: true },
26 isValid: { type: Function, optional: true },
27 placeholder: { type: String, optional: true },
28 class: { type: String, optional: true },
29 callback: { type: Function, optional: true },
30 isOpenCallback: { type: Function, optional: true },
31 readonly: { type: Boolean, optional: true },
32 };
33 static defaultProps = {
34 class: "",
35 isSmall: false,
36 debounceMillis: 0,
37 icon: {},
38 placeholder: "",
39 autofocus: false,
40 autofocusMobile: false,
41 iconOnLeftSide: true,
42 isValid: () => true,
43 readonly: false,
44 };
45 setup() {
46 this.state = useState({ isOpen: false });
47 // Bind setValue to ensure that 'this' remains the component instance.
48 this.setValue = debounce(this.setValue.bind(this), this.props.debounceMillis);
49 const ref =
50 (this.props.autofocus &&
51 useAutofocus({ refName: "input", mobile: this.props.autofocusMobile })) ||
52 useRef("input");
53 this.props.getRef?.(ref);
54 onPatched(() => {
55 this.setValue.cancel(true);
56 });
57 }
58 setValue(newValue, tModel = this.props.tModel) {
59 super.setValue(newValue, tModel);
60 this.props.callback?.(newValue);
61 }
62 }