How to insert label element in Semantic UI + React Dropdown component? - html

I would like to recreate the following Dropdown from Semantic UI, in which a <label> is inserted for the dropdown menu using UI React:
(https://semantic-ui.com/collections/form.html#dropdown)
This is the markdown I'd like my React app to create:
<div class="ui form">
<div class="field">
<label>Gender</label>
<div class="ui selection dropdown">
<input type="hidden" name="gender">
<i class="dropdown icon"></i>
<div class="default text">Gender</div>
<div class="menu">
<div class="item" data-value="1">Male</div>
<div class="item" data-value="0">Female</div>
</div>
</div>
</div>
</div>
I'm struggling to accomplish this with the React UI implementation of Semantic UI.
My current attempt is this:
<Dropdown placeholder='What grade is your child in?' fluid selection
options={ grades }
labeled={ true }
name={ `survey_response[grade_${this.state.id}]` } />
It's very important that this is labeled clearly. I've found in user research that the placeholder is confusing as a question prompt alone.
There is documentation for adding a label, however, it requires nesting child components under Dropdown component, which interferes with my use of the "options" prop. Error is as follows:
Warning: Failed prop type: Prop children in Dropdown conflicts
with props: options, selection. They cannot be defined together,
choose one or the other
Thank you, Stackoverflow!

If you are using it inside a Form you can do:
<Form>
<Form.Dropdown
label='Gender'
options={//your array of options}
selection
/>
</Form>

Related

How to separate behavior of one component from the other [Angular]

I'm learning Angular. I've created a datepicker from the scratch (*why from the scratch is a different story). As you can see I've two date-picker custom component in the same widget. The code is same for both. I just copied and pasted in the same HTML file.
Here is my monthpicker.component.html
Note: dls is our own library of basic html components like textfields etc. Please Ignore that.
....
<div class="dropdown">
<span>
<dls-textbox id="upper">
<input dlsInput [ngModel]="text" placeholder="...">
</dls-textbox>
</span>
<div class="my-table-div dropdown-content">
<div class="year-div">
<input type="number" [(ngModel)]="year" value="2019" min="2018" max="2024">
</div>
<br>
<div *ngFor="..." class="my-table">
<span *ngFor="...">
<span class="month-name" (click)="onClick(x);">
{{ x.monthName }}
</span>
</span>
</div>
</div>
<!-- A TOGGLE SWITCH GOES HERE-->
<!-- THEN SAME DATE PICKER CODE I COPIED BELOW-->
<div class="dropdown">
...
</div>
But the problem is, dates selected in one calendar is reflected in the later one also.
I tried separating them by changing id and class also. But still both of them are responding together. I want them to act independently.
Please correct me.
It happens because you are binding two inputs to the same variable with the [ngModel], due to which a change in one gets reflected in the other.
Try changing the ngModel value.
<div class="year-div">
<input type="number" [(ngModel)]="year" value="2019" min="2018" max="2024">
</div>
If you enter the same value for both ngModels it will reflect the other one as well. You need to use different values for each ng-model.
Make sure you should clear the dates which you selected with another one. Make "ID" should be different in each compoenent.

bind to ngModel through clickable options instead of select-option (dropdown)

I am trying to build a basic colorpicker with predefined colors.
for that I have an object "colors" with color-values as it's properties:
public colors = [
{ value: '#ffffff' },
{ value: '#919191' },
{ value: '#555555' },
// and some more
];
Following some examples on the web, I set up a select-option structure in my html:
<select name="role" [(ngModel)]="item.color">
<option *ngFor="let color of colors" [value]="color.value">
<div class="color-box-modal" [style.background]="color.value"></div>
</option>
</select>
This does create a dropdown menu for the options, though the colors inside don't show up. The class color-box-modal has height and width values as I did not intend to have a dropdown, but several colored boxes to click on in order to select.
Is there an alternative to the select-option structure which allows me to not have a dropdown, but just several colored boxes? Radio-buttons/checkboxes are not the desirerable way either, as I want to have a clickable field on it's own that reacts to being clicked.
If there is no alternative, is it possible to do the ngModel binding on a button-click?
edit:
After testing option 2 on Osman Ceas answer, I now have this:
<ng-template #content let-c="close" let-d="dismiss">
<i class="close icon" (click)="d('Close click x')"></i>
<div class="header">
Choose a new color
</div>
<div class="content">
<label for="col1" class="color-box-modal" style="background-color: #ffffff">
<input (click)="c('#ffffff')" id="col1" type="radio" class="hidden" [(ngModel)]="item.color" [value]="'#ffffff'">
</label>
<label for="col2" class="color-box-modal" style="background-color: #ffff00">
<input (click)="c('#ffff00')" id="col2" type="radio" class="hidden" [(ngModel)]="item.color" [value]="'#ffff00'">
</label>
<label for="col3" class="color-box-modal" style="background-color: #00ffff">
<input (click)="c('#00ffff')" id="col3" type="radio" class="hidden" [(ngModel)]="item.color" [value]="'#00ffff'">
</label>
</div>
<div class="actions">
<div class="ui button" (click)="c('Close click cancel')">Cancel</div>
</div>
</ng-template>
Though the ngModel binding does not seem to work.
The whole thing opens in a modal (the template), which in itself works, just binding to the ngModel, as I said, does not.
A native HTML select will only render text inside and any other tag will be ignored, so that's why your boxes are not showing.
If your wrap your radio button or checkbox in a <label> with the attribute for equals to an ID given to the <input>, you can basically click anywhere on the label, lets say some adjacent text, and the click will propagate to the input so the binding will still work.
You can create your own custom form controls, check out this article. So you could create a custom color picker form element that will work in any form using template forms or reactive forms.
Have a nice day
Now, this might not help everyone, but it was my solution in the end.
I started with a loop, item of items where the template in my question was meant for one single item.
I solved, or rather worked around the binding situation by just moving each item to it's own component, somewhat like this:
<div *ngFor="let item of items>
<app-sub-item [item]="item"></app-sub-item>
</div>
inside I have severel of these:
<label for="col1" class="color-box-modal" style="background-color: #ffffff">
<input (click)="setColor('#ffffff')" id="col1" type="radio" class="hidden">
</label>
With the following being in the ts-file:
setColor(color: string) {
this.item.color = color;
}
This actually works just fine at the moment.
Hope whoever reads this issue can find some use in my solution.

How do I setup placeholders in a string that can be replaced by interpolated values after selecting them from a dropdown?

so I'm trying to use binding in Angular2 so that when someone selects a name from a dropdown menu at the top of my webapp then that name replaces specific placeholders throughout the form so it displays the person's name rather than "this person" or "your teammate".
Not only do I want the name value of the object to be substituted into those placeholders, but I also want the email value to be pulled as well so that I can call on that from a mailing service when I press submit to send an email.
I've managed to get the binding working, so at the moment when I select the name it inserts it into the text, but I'm having trouble figuring out how to make placeholders that can be replaced with the selected name.
Also, how can I pull 2 values (name & email) from one ng-model?
an excerpt of my code:
<div id="toggle" class="row">>
<div class="dropdown">
<div class='col-md-6 col-md-offset-5'>
<select [(ngModel)]="selectedTeammate.name">
<option *ngFor="let teammate of teammates" value= {{teammate.name}}>{{teammate.name}}</option>
</select>
</div>
</div>
</div> s
<form>
<div id="questions">
<div id="question1" class="row">
<div id="questionbox" class='col-md-10 col-md-offset-1'>
<p>{{question1a}} {{ selectedTeammate.name }} {{question1b}} {{ selectedTeammate.name }} {{question1c}}</p>
</div>
<div id="textbox" class='col-md-6 col-md-offset-3'>
<textarea id="styled" placeholder="Why or why not?"></textarea>
</div>
</div>
<div id="question2" class="row">
<div id="questionbox" class='col-md-10 col-md-offset-1'>
<p>{{question2a}} {{selectedTeammate.name }} {{question2b}}</p>
</div>
<div id="textbox" class='col-md-6 col-md-offset-3'>
<textarea id="styled" onfocus="this.value=''; setbg('#e5fff3');" placeholder="Please provide a brief explanation"></textarea>
</div>
</div>
What it looks like:
The selected name is inserted into the string, but I need a placeholder to fill the gap in the sentence until a name is selected
If anyone can help me with my dropdown menu as well, then that would be absolutely fantastic! I've tried bootstrapping my dropdown to make it prettier, and even copied code from the bootstrap website to test, but the dropdown doesn't display any of the options when clicked. Here is the code I'm using for it.
<div class="dropdown row">
<div id="dropdown column" col-md-3 col-md-offset-3 style="margin: 0 auto; text-align: center;">
<button class="btn btn-success dropdown-toggle" type="button" data-toggle="dropdown" >Select a teammate youve worked with!
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
</div>
</div>
The dropdown displays correctly, but when clicked nothing happens and I don't see any of the list objects. I need this to work with ngFor just like the above dropdown does.
I'm also having issues trying to setup a favicon for the site.
I've included this in my of my index file:
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
and here is the file path of my favicon image:
C:\Users\user1\Desktop\Projects\sds-app\src\app\assets\Pictures\favicon.ico
I know this is a lot and maybe not formatted in the best manner, but let me know if there is any additional information you'd like. If you can help me with any of these questions I'd very much appreciate it!
Thanks!
In bootstrap select option you can show default show option as Select so you have to add one more line as option.
<select [(ngModel)]="selectedTeammate.name">
<option value="">Select</option>
<option *ngFor="let teammate of teammates" value= {{teammate.name}}>{{teammate.name}}</option>
</select>
So code will look like this.
Using ngValue you can pull the whole Object from your [(ngModel). To set a "placeholder" for your Object, so that you have some value there in your view until replaced with your chosen teammate, you can initialize the selectedTeammate with some values, e.g:
selectedTeammate: Object = {id: null, name: 'your teammate', e-mail: ''};
And as mentioned, use ngValue to bind the whole object, and change the ngModel to be e.g selectedTeammate like I have used in these examples.
<select [(ngModel)]="selectedTeammate">
<option *ngFor="let teammate of teammates" [ngValue]="teammate">{{teammate.name}}</option>
</select>
Now when you have chosen a teammate, selectedTeammate consits of the whole object, not only the name. From there you can pull the e-mail related to that teammate.
Hope this helps! :)
Demo plunker

Custom style following HTML form select field into rails code

I want to style below dropdown select option using rails form select or collection_select
<div class="field">
<label>Gender</label>
<div class="ui selection dropdown">
<input name="gender" type="hidden" value="Male"/>
<div class="default text">Select gender</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="male" data-text="Male">
<i class="male icon"></i>
Male
</div>
<div class="item" data-value="female" data-text="Female">
<i class="female icon"></i>
Female
</div>
</div>
</div>
How can I go about it? Because I'm finding it difficult to custom style rails select helper.
Well, it is pretty difficult to parse your html into Rails code without actually working with it, but you may find a good start here.
As you can see in that link, you can pass custom blocks for options:
# A block can be passed to select to customize how the options tags will be rendered. This is useful when the options tag has complex attributes.
select(report, "campaign_ids") do
available_campaigns.each do |c|
content_tag(:option, c.name, value: c.id, data: { tags: c.tags.to_json })
end
end
I think it is your way to go.

parsley.js hidden input file not validating

So I have a google map where the user selects their location, and I am trying to validate that they have selected a location, by populating a hidden input field. I was able to get it to work with a different validation engine, but now need it to work with parsley.
If the user has not selected a location, I need it to display the error message, as with all other validations. All my other validations are working, so I suspect it is something with my syntax. I am a bit confused by the parsley documentation as far as hidden input fields.
Here is my html:
<div class="col-md-6">
<!-- WIDGET GOOGLE MAP -->
<div class="widget">
<div class="widget-header" style="height: 115px;">
<h3><i class="fa fa-globe"></i> Part II: Click & drag to select your location <small>(Your actual location will never be stored or shared. It will always be randomized within a five (5) mile radius when shown to other users)</small></h3>
<em>- custom styled google map</em>
</div>
<div class="widget-content no-padding" style="height: 300px;" >
<div class="google-map">
<div id="register-map-canvas" style="height: 300px;"></div>
</div>
</div>
<div class="widget-content">
<div class="form-group">
<input type="hidden" name="homelat" id="latFld" required data-parsley-errors-container="#error-map"/> <!--the homelat input field is below the map only because it looks silly with two adjacent error messages-->
</div>
<div class="form-group">
<input type="hidden" name="homelng" id="lngFld" />
</div>
<p id="error-map"></p>
<button class="btn btn-primary btn-block" type="submit">Register</button>
<button type="submit" class="btn btn-primary">Validate</button>
</div>
</div>
<!-- END WIDGET GOOGLE MAP -->
</div>
So as you can see, I am trying to get the error message to pop up beneath my map within a widget box.
I populate the latFld and lngFld inputs but only validate one of them, so as to only generate one error message. But unfortunately I cannot get that one to validate. I know the fields are populating.
In the js parsley file (~line 195) you must on excluded: line put in comment or delete input[type=hidden]'
var ParsleyDefaults = {
// ### General
// Default data-namespace for DOM API
namespace: 'prsly-',
// Supported inputs by default
inputs: 'input, textarea, select',
// Excluded inputs by default
excluded: 'input[type=button], input[type=submit], input[type=reset]',
//input[type=hidden]',