Skip to content

Commit c266590

Browse files
authored
refactor: use a feature registry to enable form support (#528)
1 parent a07880d commit c266590

File tree

8 files changed

+40
-26
lines changed

8 files changed

+40
-26
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const features = new Map();
2+
3+
const registerFeature = (name, feature) => {
4+
features.set(name, feature);
5+
};
6+
7+
const getFeature = name => {
8+
return features.get(name);
9+
};
10+
11+
export { registerFeature, getFeature };

packages/main/src/Button.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap.js";
33
import { isSpace, isEnter } from "@ui5/webcomponents-base/src/events/PseudoEvents.js";
44
import { getCompactSize } from "@ui5/webcomponents-base/src/Configuration.js";
55
import getEffectiveRTL from "@ui5/webcomponents-base/src/util/getEffectiveRTL.js";
6+
import { getFeature } from "@ui5/webcomponents-base/src/FeaturesRegistry.js";
67
import ButtonType from "./types/ButtonType.js";
78
import ButtonRenderer from "./build/compiled/ButtonRenderer.lit.js";
89
import Icon from "./Icon.js";
@@ -193,7 +194,8 @@ class Button extends UI5Element {
193194
}
194195

195196
onBeforeRendering() {
196-
if (this.submits && !Button.FormSupport) {
197+
const FormSupport = getFeature("FormSupport");
198+
if (this.submits && !FormSupport) {
197199
console.warn(`In order for the "submits" property to have effect, you should also: import InputElementsFormSupport from "@ui5/webcomponents/dist/InputElementsFormSupport";`); // eslint-disable-line
198200
}
199201
}
@@ -210,8 +212,9 @@ class Button extends UI5Element {
210212
event.isMarked = "button";
211213
if (!this.disabled) {
212214
this.fireEvent("press", {});
213-
if (Button.FormSupport) {
214-
Button.FormSupport.triggerFormSubmit(this);
215+
const FormSupport = getFeature("FormSupport");
216+
if (FormSupport) {
217+
FormSupport.triggerFormSubmit(this);
215218
}
216219
}
217220
}

packages/main/src/CheckBox.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import UI5Element from "@ui5/webcomponents-base/src/UI5Element.js";
33
import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap.js";
44
import ValueState from "@ui5/webcomponents-base/src/types/ValueState.js";
55
import { getCompactSize } from "@ui5/webcomponents-base/src/Configuration.js";
6+
import { getFeature } from "@ui5/webcomponents-base/src/FeaturesRegistry.js";
67
import getEffectiveRTL from "@ui5/webcomponents-base/src/util/getEffectiveRTL.js";
78
import { isSpace, isEnter } from "@ui5/webcomponents-base/src/events/PseudoEvents.js";
89
import CheckBoxRenderer from "./build/compiled/CheckBoxRenderer.lit.js";
@@ -204,8 +205,9 @@ class CheckBox extends UI5Element {
204205
}
205206

206207
_enableFormSupport() {
207-
if (CheckBox.FormSupport) {
208-
CheckBox.FormSupport.syncNativeHiddenInput(this, (element, nativeInput) => {
208+
const FormSupport = getFeature("FormSupport");
209+
if (FormSupport) {
210+
FormSupport.syncNativeHiddenInput(this, (element, nativeInput) => {
209211
nativeInput.disabled = element.disabled || !element.checked;
210212
nativeInput.value = element.checked ? "on" : "";
211213
});

packages/main/src/DatePicker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap.js";
66
import { getCalendarType } from "@ui5/webcomponents-base/src/Configuration.js";
77
import { getLocale } from "@ui5/webcomponents-base/src/LocaleProvider.js";
88
import { getIconURI } from "@ui5/webcomponents-base/src/IconPool.js";
9+
import { getFeature } from "@ui5/webcomponents-base/src/FeaturesRegistry.js";
910
import LocaleData from "@ui5/webcomponents-core/dist/sap/ui/core/LocaleData.js";
1011
import DateFormat from "@ui5/webcomponents-core/dist/sap/ui/core/format/DateFormat.js";
1112
import CalendarType from "@ui5/webcomponents-base/src/dates/CalendarType.js";
@@ -308,8 +309,9 @@ class DatePicker extends UI5Element {
308309
this._calendar.selectedDates = [];
309310
}
310311

311-
if (DatePicker.FormSupport) {
312-
DatePicker.FormSupport.syncNativeHiddenInput(this);
312+
const FormSupport = getFeature("FormSupport");
313+
if (FormSupport) {
314+
FormSupport.syncNativeHiddenInput(this);
313315
} else if (this.name) {
314316
console.warn(`In order for the "name" property to have effect, you should also: import InputElementsFormSupport from "@ui5/webcomponents/dist/InputElementsFormSupport";`); // eslint-disable-line
315317
}

packages/main/src/Input.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap.js";
33
import { isIE } from "@ui5/webcomponents-core/dist/sap/ui/Device.js";
44
import ValueState from "@ui5/webcomponents-base/src/types/ValueState.js";
55
import { getCompactSize } from "@ui5/webcomponents-base/src/Configuration.js";
6+
import { getFeature } from "@ui5/webcomponents-base/src/FeaturesRegistry.js";
67
import {
78
isUp,
89
isDown,
@@ -329,8 +330,9 @@ class Input extends UI5Element {
329330
this.enableSuggestions();
330331
}
331332

332-
if (Input.FormSupport) {
333-
Input.FormSupport.syncNativeHiddenInput(this);
333+
const FormSupport = getFeature("FormSupport");
334+
if (FormSupport) {
335+
FormSupport.syncNativeHiddenInput(this);
334336
} else if (this.name) {
335337
console.warn(`In order for the "name" property to have effect, you should also: import InputElementsFormSupport from "@ui5/webcomponents/dist/InputElementsFormSupport";`); // eslint-disable-line
336338
}
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
import Input from "./Input.js";
2-
import TextArea from "./TextArea.js";
3-
import DatePicker from "./DatePicker.js";
4-
import CheckBox from "./CheckBox.js";
5-
import RadioButton from "./RadioButton.js";
6-
import Button from "./Button.js";
7-
1+
import { registerFeature } from "@ui5/webcomponents-base/src/FeaturesRegistry.js";
82
import FormSupport from "./util/FormSupport.js";
93

10-
Input.FormSupport = FormSupport;
11-
TextArea.FormSupport = FormSupport;
12-
DatePicker.FormSupport = FormSupport;
13-
CheckBox.FormSupport = FormSupport;
14-
RadioButton.FormSupport = FormSupport;
15-
Button.FormSupport = FormSupport;
4+
// Add form support to the global features registry so that Web Components can find and use it
5+
registerFeature("FormSupport", FormSupport);
166

177
export default {};

packages/main/src/RadioButton.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isDesktop } from "@ui5/webcomponents-core/dist/sap/ui/Device.js";
22
import { getCompactSize } from "@ui5/webcomponents-base/src/Configuration.js";
33
import getEffectiveRTL from "@ui5/webcomponents-base/src/util/getEffectiveRTL.js";
4+
import { getFeature } from "@ui5/webcomponents-base/src/FeaturesRegistry.js";
45
import UI5Element from "@ui5/webcomponents-base/src/UI5Element.js";
56
import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap.js";
67
import ValueState from "@ui5/webcomponents-base/src/types/ValueState.js";
@@ -253,8 +254,9 @@ class RadioButton extends UI5Element {
253254
}
254255

255256
_enableFormSupport() {
256-
if (RadioButton.FormSupport) {
257-
RadioButton.FormSupport.syncNativeHiddenInput(this, (element, nativeInput) => {
257+
const FormSupport = getFeature("FormSupport");
258+
if (FormSupport) {
259+
FormSupport.syncNativeHiddenInput(this, (element, nativeInput) => {
258260
nativeInput.disabled = element.disabled || !element.selected;
259261
nativeInput.value = element.selected ? element.value : "";
260262
});

packages/main/src/TextArea.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import UI5Element from "@ui5/webcomponents-base/src/UI5Element.js";
33
import CSSSize from "@ui5/webcomponents-base/src/types/CSSSize.js";
44
import Integer from "@ui5/webcomponents-base/src/types/Integer.js";
55
import { fetchResourceBundle, getResourceBundle } from "@ui5/webcomponents-base/src/ResourceBundle.js";
6+
import { getFeature } from "@ui5/webcomponents-base/src/FeaturesRegistry.js";
67
import TextAreaRenderer from "./build/compiled/TextAreaRenderer.lit.js";
78

89
import { TEXTAREA_CHARACTERS_LEFT, TEXTAREA_CHARACTERS_EXCEEDED } from "./i18n/defaults.js";
@@ -249,8 +250,9 @@ class TextArea extends UI5Element {
249250
this._maxHeight = `${this.growingMaxLines * 1.4 * 14 + 9}px`;
250251
}
251252

252-
if (TextArea.FormSupport) {
253-
TextArea.FormSupport.syncNativeHiddenInput(this);
253+
const FormSupport = getFeature("FormSupport");
254+
if (FormSupport) {
255+
FormSupport.syncNativeHiddenInput(this);
254256
} else if (this.name) {
255257
console.warn(`In order for the "name" property to have effect, you should also: import InputElementsFormSupport from "@ui5/webcomponents/dist/InputElementsFormSupport";`); // eslint-disable-line
256258
}

0 commit comments

Comments
 (0)