OOUI/Layouts/Fields and Fieldsets

From MediaWiki.org
< OOUI‎ | Layouts
Jump to: navigation, search

One of the most commonly used layouts is the FieldsetLayout, which is composed of one or more FieldLayouts or ActionFieldLayouts that contain a field widget, such as a form input. In addition to a field widget, ActionFieldLayouts contain a button.

FieldsetLayouts, FieldLayouts, ActionFieldLayouts and HorizontalLayouts can be configured with a label. Both field and action field layouts can also be configured with help text, which is accessed by the user via a help icon in the upper-right corner of the field.

Note that each field and action field layout requires a widget object (e.g., a button or input field), which should be specified by reference before any optional configuration settings. A FieldsetLayout can only contain field and action field layouts, and an error will be thrown if another type of object is included directly.
An example of a Fieldset layout

<!-- This is an example of a FieldsetLayout that contains four FieldLayouts 
(one for each of the labeled form elements). Each field has been configured 
to illustrate a way in which labels can be aligned: `left`, `right`, `top`, or 
`inline`. -->

<style>
body {
  font-family: sans-serif;
  font-size: 0.875em;
}

.container {
  width: 600px;
  height: 300px;
  border-style: dotted;
  border-width: 1px;
  padding: 20px;
}
</style>

<script>
 // Create form  elements (text input, checkbox, submit button, etc.)
var input1 = new OO.ui.TextInputWidget( { 
  placeholder: 'A form text field'
} );

var input2 = new OO.ui.TextInputWidget( { 
  placeholder: 'A form text field'
} );

var input3 = new OO.ui.TextInputWidget( { 
  placeholder: 'A form text field with help'
} );

var input4 = new OO.ui.CheckboxInputWidget( {
  selected: true
} );
      
// Create a Fieldset layout.
var fieldset = new OO.ui.FieldsetLayout( { 
  label: 'FieldsetLayout: Examples of label alignment and help text',
  classes: ["container"]
} );

// Add field layouts that contain the form elements to the fieldset. Items can 
// also be specified with the FieldsetLayout's `items` config option: 

fieldset.addItems( [ 
  new OO.ui.FieldLayout( input1, {
    label: 'Left-aligned label, the default', 
    align: 'left' 
  } ),

  new OO.ui.FieldLayout( input2, { 
    label: 'Right-aligned label',
    align: 'right' 
  } ),

  new OO.ui.FieldLayout( input3, { 
    label: 'Top-aligned label', 
    align: 'top', 
    help: 'A bit of help'
  } ),

  new OO.ui.FieldLayout( input4, { 
    label: 'Inline label', 
    align: 'inline' 
  } )
] );

$( 'body' ).append( fieldset.$element );
</script>

Field labels are aligned in one of four ways:

  • left: The label is placed before the form field (or other object) and aligned with the left margin. A left-alignment is used for forms with many fields.
  • right: The label is placed before the form field and aligned to the right margin. A right-alignment is used for long but familiar forms which users tab through, verifying the current field with a quick glance at the label.
  • top: The label is placed above the form field. A top-alignment is used for brief forms that users fill out from top to bottom.
  • inline: The label is placed after the form field and aligned to the left. An inline-alignment is best used with checkboxes or radio buttons.

The following is an example of a fieldset layout with an action field layout:

An example of an action field layout

// An example of a fieldset with an action field layout
      
var fieldset = new OO.ui.FieldsetLayout( { 
  label: 'FieldsetLayout with an action field layout'
} );

// Add an action field layout: 
fieldset.addItems( [ 
  new OO.ui.ActionFieldLayout(
    new OO.ui.TextInputWidget( {
        placeholder: 'Field widget'
    } ),
    new OO.ui.ButtonWidget( {
        label: 'Button'
    } ),
    {
        label: 'This label is aligned right.',
        align: 'right'
    }
  )
] );

$( 'body' ).append( fieldset.$element );

HorizontalLayout[edit]

For specific forms, for example when the checkboxes are direct modifiers of an input element, it can be considered to use a horizontal layout for the side-by-side alignment. Please be aware about the implications for user orientation flow, when the reading direction is changed from vertical to horizontal, therefore it should only be used in specific, option-rich forms.

// An example of a fieldset with horizontal layout

var fieldset = new OO.ui.FieldsetLayout( {
	label: 'HorizontalLayout'
} );

// Add an horizontal field layout
fieldset.addItems( [ 
	new OO.ui.FieldLayout(
		new OO.ui.Widget( {
			content: [ new OO.ui.HorizontalLayout( {
				items: [
					new OO.ui.ButtonWidget( { label: 'Button' } ),
					new OO.ui.ButtonGroupWidget( { items: [
						new OO.ui.ToggleButtonWidget( { label: 'A' } ),
						new OO.ui.ToggleButtonWidget( { label: 'B' } )
					] } ),
					new OO.ui.ButtonInputWidget( { label: 'ButtonInput' } ),
					new OO.ui.TextInputWidget( { value: 'TextInput' } ),
					new OO.ui.CheckboxInputWidget( { selected: true } ),
					new OO.ui.RadioInputWidget( { selected: true } ),
					new OO.ui.LabelWidget( { label: 'Label' } )
				]
			} ) ]
		} ),
		{
			label: 'Multiple widgets shown as a single line, ' +
				'as used in compact forms or in parts of a bigger widget.',
			align: 'top'
		}
	)
] );

$( 'body' ).append( fieldset.$element );

For a full list of supported methods and configuration options, please see the code-level documentation for FieldsetLayouts, FieldLayouts, and ActionFieldLayouts