Skip to content

Commit 96a0517

Browse files
authored
feat: enable radio button form support (#357)
1 parent d8bf3ea commit 96a0517

File tree

6 files changed

+72
-8
lines changed

6 files changed

+72
-8
lines changed

packages/main/src/CheckBox.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ class CheckBox extends UI5Element {
186186
onBeforeRendering() {
187187
this.syncLabel();
188188

189+
this._enableFormSupport();
190+
}
191+
192+
syncLabel() {
193+
this._label = Object.assign({}, this._label);
194+
this._label.text = this.text;
195+
this._label.wrap = this.wrap;
196+
this._label.textDirection = this.textDirection;
197+
}
198+
199+
_enableFormSupport() {
189200
if (CheckBox.FormSupport) {
190201
CheckBox.FormSupport.syncNativeHiddenInput(this, (element, nativeInput) => {
191202
nativeInput.disabled = element.disabled || !element.checked;
@@ -196,13 +207,6 @@ class CheckBox extends UI5Element {
196207
}
197208
}
198209

199-
syncLabel() {
200-
this._label = Object.assign({}, this._label);
201-
this._label.text = this.text;
202-
this._label.wrap = this.wrap;
203-
this._label.textDirection = this.textDirection;
204-
}
205-
206210
onclick() {
207211
this.toggle();
208212
}

packages/main/src/InputElementsFormSupport.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Input from "./Input.js";
22
import TextArea from "./TextArea.js";
33
import DatePicker from "./DatePicker.js";
44
import CheckBox from "./CheckBox.js";
5+
import RadioButton from "./RadioButton.js";
56
import Button from "./Button.js";
67

78
import FormSupport from "./util/FormSupport.js";
@@ -10,6 +11,7 @@ Input.FormSupport = FormSupport;
1011
TextArea.FormSupport = FormSupport;
1112
DatePicker.FormSupport = FormSupport;
1213
CheckBox.FormSupport = FormSupport;
14+
RadioButton.FormSupport = FormSupport;
1315
Button.FormSupport = FormSupport;
1416

1517
export default {};

packages/main/src/RadioButton.js

+36
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ const metadata = {
102102
* <br/><b>Note:</b>
103103
* Only one radio button can be selected per group.
104104
*
105+
* <b>Important:</b> For the <code>name</code> property to have effect when submitting forms, you must add the following import to your project:
106+
* <code>import InputElementsFormSupport from "@ui5/webcomponents/dist/InputElementsFormSupport";</code>
107+
*
108+
* <b>Note:</b> When set, a native <code>input</code> HTML element
109+
* will be created inside the <code>ui5-radiobutton</code> so that it can be submitted as
110+
* part of an HTML form.
111+
*
105112
* @type {string}
106113
* @public
107114
*/
@@ -110,6 +117,22 @@ const metadata = {
110117
type: String,
111118
},
112119

120+
/**
121+
* Defines the form value of the <code>ui5-radiobutton</code>.
122+
* When a form with a radio button group is submitted, the group's value
123+
* will be the value of the currently selected radio button.
124+
*
125+
* <b>Important:</b> For the <code>value</code> property to have effect, you must add the following import to your project:
126+
* <code>import InputElementsFormSupport from "@ui5/webcomponents/dist/InputElementsFormSupport";</code>
127+
*
128+
* @type {string}
129+
* @public
130+
*/
131+
value: {
132+
type: String,
133+
defaultValue: "",
134+
},
135+
113136
_label: {
114137
type: Object,
115138
},
@@ -170,6 +193,8 @@ class RadioButton extends UI5Element {
170193
onBeforeRendering() {
171194
this.syncLabel();
172195
this.syncGroup();
196+
197+
this._enableFormSupport();
173198
}
174199

175200
syncLabel() {
@@ -198,6 +223,17 @@ class RadioButton extends UI5Element {
198223
this._name = this.name;
199224
}
200225

226+
_enableFormSupport() {
227+
if (RadioButton.FormSupport) {
228+
RadioButton.FormSupport.syncNativeHiddenInput(this, (element, nativeInput) => {
229+
nativeInput.disabled = element.disabled || !element.selected;
230+
nativeInput.value = element.selected ? element.value : "";
231+
});
232+
} else if (this.value) {
233+
console.warn(`In order for the "value" property to have effect, you should also: import InputElementsFormSupport from "@ui5/webcomponents/dist/InputElementsFormSupport";`); // eslint-disable-line
234+
}
235+
}
236+
201237
onclick() {
202238
return this.toggle();
203239
}

packages/main/test/sap/ui/webcomponents/main/pages/FormSupport.html

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
<ui5-checkbox name="cb" checked></ui5-checkbox>
2626
<ui5-checkbox name="cb_disabled" disabled checked></ui5-checkbox>
2727

28+
<ui5-radiobutton name="radio" text="A" value="a"></ui5-radiobutton>
29+
<ui5-radiobutton name="radio" text="B" value="b" selected></ui5-radiobutton>
30+
<ui5-radiobutton name="radio" text="C" value="c"></ui5-radiobutton>
31+
2832
<ui5-button id="b1">Does not submit forms</ui5-button>
2933

3034
<ui5-button id="b2" submits>Submits forms</ui5-button>

packages/main/test/sap/ui/webcomponents/main/pages/form.html

+18
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ <h3> Input type 'URL'</h3>
7272
<br>
7373
<ui5-checkbox name="cb"></ui5-checkbox>
7474

75+
<br><br>
76+
77+
<ui5-radiobutton name="radio1" text="A" value="a" selected></ui5-radiobutton>
78+
<ui5-radiobutton name="radio1" text="B" value="b"></ui5-radiobutton>
79+
<ui5-radiobutton name="radio1" text="C" value="c"></ui5-radiobutton>
80+
81+
<br><br>
82+
83+
<ui5-radiobutton name="radio2" text="D" value="d" selected></ui5-radiobutton>
84+
<ui5-radiobutton name="radio2" text="E" value="e"></ui5-radiobutton>
85+
<ui5-radiobutton name="radio2" text="F" value="f"></ui5-radiobutton>
86+
87+
<br><br>
88+
89+
<input type="radio" name="radio_native" value="o1">Option 1<br>
90+
<input type="radio" name="radio_native" value="o2" selected>Option 2<br>
91+
<input type="radio" name="radio_native" value="o3">Option 3
92+
7593
<br>
7694
<ui5-button>Does not submit forms</ui5-button>
7795

packages/main/test/specs/FormSupport.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("Form support", () => {
2121
submitButton.click();
2222

2323
const formWasSubmitted = browser.execute(() => {
24-
const expectedFormData = "FormSupport.html?input=ok&ta=ok&dp=Apr+10%2C+2019&cb=on";
24+
const expectedFormData = "FormSupport.html?input=ok&ta=ok&dp=Apr+10%2C+2019&cb=on&radio=b";
2525
return location.href.endsWith(expectedFormData);
2626
});
2727
assert.ok(formWasSubmitted, "For was submitted and URL changed");

0 commit comments

Comments
 (0)