mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[ADD] web_widget_pattern
This commit is contained in:
15
web_widget_pattern/static/src/autocomplete.esm.js
Normal file
15
web_widget_pattern/static/src/autocomplete.esm.js
Normal file
@@ -0,0 +1,15 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import {AutoComplete} from "@web/core/autocomplete/autocomplete";
|
||||
|
||||
const _extractProps = AutoComplete.extractProps;
|
||||
AutoComplete.extractProps = ({attrs, field}) => {
|
||||
return Object.assign(_extractProps({attrs, field}), {
|
||||
pattern: attrs.pattern || field.pattern,
|
||||
});
|
||||
};
|
||||
|
||||
AutoComplete.props = {
|
||||
...AutoComplete.props,
|
||||
pattern: {type: String, optional: true},
|
||||
};
|
||||
10
web_widget_pattern/static/src/autocomplete.xml
Normal file
10
web_widget_pattern/static/src/autocomplete.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<templates xml:space="preserve">
|
||||
|
||||
<t t-inherit="web.AutoComplete" t-inherit-mode="extension" owl="1">
|
||||
<input position="attributes">
|
||||
<attribute name="t-att-pattern">props.pattern</attribute>
|
||||
</input>
|
||||
</t>
|
||||
|
||||
</templates>
|
||||
37
web_widget_pattern/static/src/char_field.esm.js
Normal file
37
web_widget_pattern/static/src/char_field.esm.js
Normal file
@@ -0,0 +1,37 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import {CharField} from "@web/views/fields/char/char_field";
|
||||
import {_lt} from "@web/core/l10n/translation";
|
||||
import {patch} from "@web/core/utils/patch";
|
||||
import {sprintf} from "@web/core/utils/strings";
|
||||
|
||||
patch(CharField.prototype, "web_widget_pattern", {
|
||||
parse(value) {
|
||||
const result = this._super(...arguments);
|
||||
const pattern = this.props.pattern;
|
||||
if (pattern) {
|
||||
const regex = new RegExp(pattern, "v");
|
||||
const match = regex.exec(result);
|
||||
if (!match || match[0] !== value) {
|
||||
throw new Error(
|
||||
_lt(
|
||||
sprintf("%s does not match required pattern %s", value, pattern)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
},
|
||||
});
|
||||
|
||||
const _extractProps = CharField.extractProps;
|
||||
CharField.extractProps = ({attrs, field}) => {
|
||||
return Object.assign(_extractProps({attrs, field}), {
|
||||
pattern: attrs.pattern || field.pattern,
|
||||
});
|
||||
};
|
||||
|
||||
CharField.props = {
|
||||
...CharField.props,
|
||||
pattern: {type: String, optional: true},
|
||||
};
|
||||
9
web_widget_pattern/static/src/char_field.xml
Normal file
9
web_widget_pattern/static/src/char_field.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<templates xml:space="preserve">
|
||||
|
||||
<t t-inherit="web.CharField" t-inherit-mode="extension" owl="1">
|
||||
<input position="attributes">
|
||||
<attribute name="t-att-pattern">props.pattern</attribute>
|
||||
</input>
|
||||
</t>
|
||||
</templates>
|
||||
Reference in New Issue
Block a user