Use in form context
Import
Import the settings, the select and the fields scss files.
@import 'settings-tools/all-settings';
@import 'components/_c.select';
@import 'components/_c.fields';
Note that the import order is important to get the right rendering of the component.
Usage
To use the select in your form, you must use the following css classes:
mc-fieldon a containermc-field__labelon label tag- and
mc-field__inputin addition of themc-selectclass on the native select html tag:
<div class="mc-field"> <label class="mc-field__label" for="foo"> Label </label>
<select id="foo" class="mc-field__input mc-select"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select></div>
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="foo"> Label </label> <select id="foo" class="mc-field__input mc-select"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> </div></div>
Variations
Available sizes
You can use one of the 2 available sizes:
- Small: apply the modifier
mc-select--sin addition of themc-selectclass - Medium: this is the default style so you don't need to add a modifier class
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="defaultSelect"> Label </label> <select id="defaultSelect" class="mc-field__input mc-select"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> </div></div>
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="smallSelect"> Label </label> <select id="smallSelect" class="mc-field__input mc-select mc-select--s"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> </div></div>
Behaviors
Help text
You have the possibility to define a help text when using your select field.
For this you have to add a span with the mc-field__help class below the label.
<label class="mc-field__label"> Label</label><span id="helptext" class="mc-field__help"> Help text</span>
When using a help text you must comply with the following accessibility rules:
- the span
mc-field__helpmust always have anid - the
aria-describedbyattribute must be added on the select element and must refer to the id of the help text
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="foo"> Label </label> <span id="helptext" class="mc-field__help"> Help text </span> <select aria-describedby="helptext" id="foo" class="mc-field__input mc-select"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> </div></div>Requirement
When the select is mandatory you must specify it to the user.
For this you have to add a span with the class mc-field__requirement right after the label.
<label class="mc-field__label"> Label <span class="mc-field__requirement"> mandatory </span></label>
When the select is mandatory the required attribute must be added on the select element
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="foo"> Label <span class="mc-field__requirement"> mandatory </span> </label> <select required id="foo" class="mc-field__input mc-select"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> </div></div>States and Validation
Valid
To indicate that a field is valid, the is-valid class must be added to the select element.
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="foo"> Label </label> <select id="foo" class="mc-field__input mc-select is-valid"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> </div></div>
Invalid
To indicate that a field is not valid several steps are to be implemented:
- Add the
is-invalidclass and thearia-invalidattribute to the select element - You can add in addition a text detailing the error. This text must be added in a span with the class
mc-field__error-messageand positioned below the field
If you use an error message you must comply with the following accessibility rules:
- the span
mc-field__error-messagemust always have anid - the
aria-describedbyattribute must be added on the select element and must refer to the id of the error message
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="foo"> Label </label> <select aria-invalid="true" aria-describedby="err_1" id="foo" class="mc-field__input mc-select is-invalid"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select>
<span id="err_1" class="mc-field__error-message"> Error: A mistake has been made. Please try again. </span> </div></div>
Disabled
To indicate that the select is disabled, the disabled attribute must be added to the select element.
<div class="example"> <div class="mc-field"> <label class="mc-field__label" for="foo"> Label </label> <select disabled id="foo" class="mc-field__input mc-select"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> </div></div>
Use outside a form
In some cases you may need to use the select field outside of a form. To do this, the way to implement it is as follows:
Import
@import 'settings-tools/all-settings';
@import 'components/_c.select';
Usage
Apply the classes mc-textarea on a standard textarea html tag:
When you use a select outside of a form, you should not wrap it in the
mc-fieldcontainer
<select class="mc-select"> <option value="">-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option></select>
<div class="example"> <select id="foo" class="mc-select"> <option value="" selected disabled>-- Choose an option --</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select></div>
Variations
Default option (as a placeholder)
By default, the first option is displayed. You can use it as a placeholder, but remember to distinguish it from the rest of options and to add an empty value attribute. Always use '--' before and after the first option with spaces between the dashes and the text.
<select class="mc-select"> <option value="">-- Select one option --</option> <option value="option2">Option 1</option> <option value="option3">Option 2</option> <option value="option3">Option 3</option></select>
Accessibility and semantic
Use <option> for elements of your list <select>.
Each <option> element should have a value attribute with data.