Skip to content

Commit 5dbed33

Browse files
config and WIP with custom form item example
1 parent 16b0b46 commit 5dbed33

3 files changed

Lines changed: 88 additions & 45 deletions

File tree

src/mm-form/index.html

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@
8787
<mm-input
8888
id="width"
8989
placeholder="Width"
90-
validation="int"
90+
validation="int"
9191
on-changed="_widthChanged"></mm-input>
9292
<mm-input
9393
id="height"
9494
placeholder="Height"
95-
validation="int"
95+
validation="int"
9696
on-changed="_heightChanged"></mm-input>
9797
</mm-group>
9898
</template>
@@ -154,6 +154,7 @@
154154
label="Select a Dimension"
155155
error-message="You need to select dimensions"></special-snowflake>
156156
</mm-form>
157+
<!-- value='{"width":"300", "height":"250"}' -->
157158

158159
<script>
159160
// special form component to validate assumptions:
@@ -173,12 +174,6 @@
173174
type: Boolean
174175
}
175176
},
176-
_valueChanged: function(oldVal, newVal) {
177-
// must fire a changed event:
178-
if (newVal) {
179-
this.fire('changed', { value: newVal });
180-
}
181-
},
182177
_handleRadioSelected: function(e) {
183178
switch (e.detail.item.id) {
184179
case 'standardSize':
@@ -191,22 +186,50 @@
191186
return;
192187
}
193188
},
189+
_valueChanged: function(newVal, oldVal) {
190+
if (newVal) {
191+
this.fire('changed', { value: newVal });
192+
193+
// set the dropdown if needed
194+
if (!this.nonStandardSize) {
195+
if (!this.$.standardSizeDdl.value) {
196+
this.$.standardSizeDdl.value = String(
197+
this.value.width + 'x' + this.value.height
198+
);
199+
}
200+
} else {
201+
this.$.width.value = this.value.width;
202+
this.$.height.value = this.value.height;
203+
}
204+
}
205+
},
206+
194207
_standardSizeChanged: function(e) {
195-
// split the dimension and shove into the value Object
196-
var dimensions = e.detail.value.split('x');
197-
this.value = {
198-
width : parseInt(dimensions[0]),
199-
height : parseInt(dimensions[1])
200-
};
208+
var dimensions = e.detail.value.split('x'),
209+
width = parseInt(dimensions[0]),
210+
height = parseInt(dimensions[1]);
211+
this._dimensionsChanged(width, height);
201212
},
202213
_widthChanged: function(e) {
214+
e.preventDefault();
203215
if (e.detail.value) {
204-
this.value.width = parseInt(e.detail.value);
216+
var width = parseInt(e.detail.value),
217+
height = this.value ? this.value.height : null;
218+
this._dimensionsChanged(width, height);
205219
}
206220
},
207221
_heightChanged: function(e) {
222+
e.preventDefault();
208223
if (e.detail.value) {
209-
this.value.height = parseInt(e.detail.value);
224+
var width = this.value ? this.value.width : null,
225+
height = parseInt(e.detail.value);
226+
this._dimensionsChanged(width, height);
227+
}
228+
},
229+
_dimensionsChanged: function(width, height) {
230+
this.value = {
231+
width: width,
232+
height: height
210233
}
211234
}
212235
});
@@ -216,17 +239,16 @@
216239
var testForm = document.querySelector('#testForm');
217240
var dimensions = document.querySelector('#dimensions');
218241

219-
dimensions.value = {
220-
width: 300,
221-
height: 250
222-
};
242+
// dimensions.value = {
243+
// width: 300,
244+
// height: 250
245+
// };
223246

224247
// add custom validation rules for the dimension component
225248
// this would probably be more robust, but this is sufficient
226249
testForm.rules.dimensions = function(i) {
227-
var isValid = typeof(i.width === 'number') &&
228-
typeof(i.height === 'number');
229-
return isValid;
250+
return i ? typeof(parseInt(i.width) === 'number') &&
251+
typeof(parseInt(i.height) === 'number') : false;
230252
}
231253

232254
testForm.addEventListener('serialize-form', function(e){

src/mm-form/mm-form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
id="formActions"
2727
type="{{footerType}}"
2828
message="{{footerMessage}}"
29-
message-visible="{{_showFooterMessage}}"
29+
message-visible="{{_displayFooterMessage}}"
3030
unresolved fitparent semi-transparent>
3131

3232
<template is="dom-repeat" items="{{actions}}">

src/mm-form/mm-form.js

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@
5151
type: String,
5252
notify: true
5353
},
54-
footerMessage: {
55-
type: Boolean,
56-
value: true
57-
},
5854
actions: {
5955
type: Array,
6056
value: function() {
@@ -77,10 +73,30 @@
7773
];
7874
}
7975
},
76+
unsaved: {
77+
type: Boolean,
78+
value: false,
79+
notify: true
80+
},
81+
showFooterMessages: {
82+
type: Boolean,
83+
value: true,
84+
notify: true
85+
},
86+
showWarningMessages: {
87+
type: Boolean,
88+
value: true,
89+
notify: true
90+
},
8091
_showFooterMessage: {
8192
type: Boolean,
93+
value: false,
8294
notify: true
8395
},
96+
_displayFooterMessage: {
97+
type: Boolean,
98+
computed: '_displayMessage(_showFooterMessage, showFooterMessages)'
99+
}
84100
},
85101

86102
observers: [
@@ -148,6 +164,9 @@
148164
errorMsgEle = null,
149165
fieldHeaderEle = null,
150166
parentEle = Polymer.dom(item).parentNode;
167+
168+
// TODO: multiple property components - how to handle them
169+
// is it via a behavior / array, etc.
151170

152171
// create the label and error message if necessary
153172
if (errorMsg) {
@@ -194,33 +213,33 @@
194213
e.model.item.callback(e,this);
195214
},
196215

216+
// display footer messaging based on settings
217+
_displayMessage: function(_showFooterMessage, showFooterMessages) {
218+
return _showFooterMessage && showFooterMessages;
219+
},
220+
197221
// *******************************
198222
// handle changes within the form
199223
_handleChanged: function(e) {
200-
var field = e.target,
201-
value = e.detail.value;
224+
var name = e.target.getAttribute('name'),
225+
item = this.formItems[name];
202226

203-
// TODO: don't show change warnings flag
204-
// check here
205-
if (value) {
206-
this._dataUpdate(field, value);
227+
if (item && item.field.value) {
228+
this._updateData(item.field, item.value);
229+
this.unsaved = this._diffData();
207230

208-
var diff = this._diffData();
209-
210-
if (diff) {
231+
// trigger a warning message in the footer
232+
if (this.unsaved && this.showWarningMessages) {
211233
this.footerMessage = this.footerMessages.warning;
212234
this.footerType = 'warning';
213235
this._showFooterMessage = true;
214236
}
215237
}
216238
},
217239

218-
_dataUpdate: function(field, value) {
240+
_updateData: function(field, value) {
219241
var name = field.getAttribute('name');
220-
221-
if (name && this.formData[name]!== undefined) {
222-
this.formData[name] = value;
223-
}
242+
if (name) this.formData[name] = value;
224243
},
225244

226245
_diffData: function() {
@@ -231,7 +250,6 @@
231250
diff.push(key);
232251
}
233252
}
234-
235253
return diff.length > 0;
236254
},
237255

@@ -264,6 +282,9 @@
264282
validation = item.validation,
265283
isValid = false;
266284

285+
this._updateData(item.field, value);
286+
this.unsaved = this._diffData();
287+
267288
// validate UI:
268289
isValid = this._validateField(validation, value);
269290

@@ -282,14 +303,14 @@
282303

283304
if (invalid.length > 0) {
284305
// show messaging in the footer
285-
if (this.footerMessage) {
306+
if (this.unsaved) {
286307
this.footerType = 'error';
287308
this.footerMessage = this.footerMessages.error;
288309
this._showFooterMessage = true;
289310
}
290311
} else {
291312
// show messaging in the footer
292-
if (this.footerMessage) {
313+
if (this.unsaved) {
293314
this.footerType = 'success';
294315
this.footerMessage = this.footerMessages.success;
295316
this._showFooterMessage = true;

0 commit comments

Comments
 (0)