public override void Process(TagHelperContext context, TagHelperOutput output) { InitFields(); bool isTextArea = output.TagName == "textarea"; if (!isTextArea) { // https://getbootstrap.com/docs/4.0/components/forms/ output.TagName = "input"; output.TagMode = TagMode.SelfClosing; } InputModelInfo modelInfo = new InputModelInfo(Generator, For, Format, InputTypeName, Value, Placeholder, LabelText, ViewContext); TagBuilder inputTbx = isTextArea ? modelInfo.GetTextarea(Rows, 0) : modelInfo.ProcessTagBuilder(context, output); modelInfo.SetExtraTextInputValues(inputTbx, isTextArea, Size, Disabled); output.MergeAttributes(inputTbx); TagBuilder validMsgTag = modelInfo.GetValidationMessageTag(ValidationMessage.NullIfEmptyTrimmed()); WriteStartGroupTag(output.PreElement, "textbox"); WriteLabel_PlusPrePost_IfAvailable(GetFinalLabelText(modelInfo), modelInfo, output.PreElement); output.PostElement.AppendHtml(validMsgTag); WriteEndGroupTag(output.PostElement); }
public string GetFinalLabelText(InputModelInfo modelInfo) => LabelText.FirstNotNulle(modelInfo.LabelText);
public (string optionLabel, InputColItem[]) GetInputCollectionItems( bool winnowNullSelectOption, string optionLabel = null, BoolNames boolNames = null, IEnumerable <InputColItem> items = null, IEnumerable <string> enumNames = null, object selectedValueForModelCollection = null) { InputModelInfo modelInfo = this; if (items != null) { InputColItem[] _items = items.ToArray(); if (_items.NotNulle()) { if (!_items.Any(kv => kv.IsSelected)) { throw new NotImplementedException(); // RUN to find first selected, if any... } return(optionLabel, _items); } } Type mainTyp = modelInfo.MainType; bool typeIsNullable = modelInfo.ModelTypeIsNullable; bool isBool = mainTyp.IsPrimitive && mainTyp == typeof(bool); bool isEnum = !isBool && mainTyp.IsEnum; if (isBool) { bool?modelValBool = (bool?)ModelValue; BoolNames bn = boolNames ?? DefaultBoolNames; if (boolNames != null) { bn.HtmlEncodeValues().Merge(DefaultBoolNames); } var nameValues = new InputColItem[] { new InputColItem(bn.TrueName, true, modelValBool == true), new InputColItem(bn.FalseName, false, modelValBool == false), modelInfo.ModelTypeIsNullable ? new InputColItem(bn.NotSetName, null, modelValBool == null) : null, }; var _items = _alterCollForSelectOption(nameValues, winnowNullSelectOption, ref optionLabel); return(optionLabel, _items); // see note below, can't call with ref setter in return line } else if (isEnum) { InputColItem[] _items = GetInputEnumValues(enumNames, values: null, countNullAsIsChecked: true); _items = _alterCollForSelectOption(_items, winnowNullSelectOption, ref optionLabel); // interesting! - the ref set won't set if called within the return line! so must put this line above return(optionLabel, _items); } else { object modelValue = modelInfo.ModelValue; if (modelValue != null) { if (modelValue is IEnumerable <object> arr) { List <InputColItem> kvlist = new List <InputColItem>(); bool _allowStringEquality = selectedValueForModelCollection != null && AllowStringBasedEqualityComparison(selectedValueForModelCollection.GetType()); foreach (object value in arr) { if (value != null) { string valueName = value.ToString(); if (valueName.NotNulle()) { bool isSel = selectedValueForModelCollection == null ? false : ObjectIsEqual(selectedValueForModelCollection, valueName, value, countNullAsIsCheckedMatch: true, allowStringEquality: _allowStringEquality); var _item = new InputColItem(valueName, value, isSel); kvlist.Add(_item); } } } return(optionLabel, kvlist.Where(kv => kv != null).ToArray()); } } } return(optionLabel, null); }
public override void Process(TagHelperContext context, TagHelperOutput output) { InitFields(); output.TagMode = TagMode.SelfClosing; output.TagName = "input"; InputTypeName = "checkbox"; InputModelInfo modelInfo = new InputModelInfo(Generator, For, Format, InputTypeName, Value, null, LabelText, ViewContext); TagBuilder cboxTb = modelInfo.ProcessTagBuilder(context, output); cboxTb.AddCssClass("custom-control-input"); cboxTb.AddDisabledAttribute(Disabled); WriteStartGroupTag(output.PreElement, "checkbox"); //, formGroupClassName: "form-check"); WriteLabel_PlusPrePost_IfAvailable(GroupLabel?.HtmlEncode(), modelInfo, output.PreElement); bool doNew = true; if (_isBS43 && doNew) { output.PreElement .AppendHtml("<div class=\"custom-control custom-checkbox\">") .AppendHtml("\r\n\t"); output.MergeAttributes(cboxTb); output.PostElement //.AppendHtml(cboxTb) .AppendHtml("\r\n\t") .AppendHtml("<label class=\"custom-control-label\" for=\"") .AppendHtml(modelInfo.FullNameId) .AppendHtml("\">") .AppendHtml(modelInfo.LabelText) .AppendHtml("</label>\r\n</div>\r\n") ; /* * <div class="custom-control custom-checkbox"> * <input type="checkbox" class="custom-control-input" id="customCheck1"> * <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label> * </div> */ } else { /* * <div class="form-check"> * <label class="custom-control custom-checkbox"> * <input type="checkbox" class="custom-control-input" data-val="true" data-val-required="The IsCool field is required." id="IsCool" name="IsCool" value="true"> * <span class="custom-control-indicator"></span> * <span class="custom-control-description">Hey, are you cool??!!</span> * </label> * </div>*/ output.PreElement.AppendHtml($@" <div class=""form-check""> <label class=""custom-control custom-checkbox""> " ); output.MergeAttributes(cboxTb); output.PostElement.AppendHtml($@" <span class=""custom-control-indicator""></span> <span class=""custom-control-description"">{modelInfo.LabelText}</span> </label> </div> "); } WriteEndGroupTag(output.PostElement); }