Link to the hosted Bigger Form page.

Much of the basics has been described in the Simple Form. All we have done is added more fields.

In this chapter, we'll show you how to use the grid to make the form responsive. There are 4 responsive sizes, Extra Small (xs), Small (sm), Medium (md), Large (lg).

The screen shot above shows the Large layout. It basically has 3 columns, most of the right hand column is blank.

Here you can see the right most column has been removed and the 3 visible columns are smaller.

Here you can see most of the form now fits into one column. Where there was more than one column, there now wrap to the next line and show most items vertically.

Here we show only a subset of the full functionality of the grid, mainly how to configure it for responsive. You will already have seen in the previous example Simple Form the basics of constructing the grid.

Grids help ensure good component layout by ensuring that changes in screen size are responded to gracefully, so your UI design always looks great and performs well. Try resizing the browser window on the demo page - see how the form elements reorganise themselves? That's the grid at work.

  • A grid typically has container element encapsulating 3 grid items. Here we see a grid with up to three columns available. This when resized can drop to 2 columns and even to one column depending on the numbers.
<uwc-grid container >
<uwc-grid xs="12" sm="6" md="4" lg="4"><!-- Component goes here --></uwc-grid>
<uwc-grid xs="12" sm="6" md="4" lg="4"><!-- Component goes here --></uwc-grid>
<uwc-grid xs="12" sm="0" md="4" lg="4"><!-- Component goes here --></uwc-grid>
</uwc-grid>
  • The magic number is 12, all columns need to add up to 12. If the number is greater than 12, that field will wrap to the next line. If the number is smaller than 12, then other grid attributes take hold to position the cells, e.g left justify, centre, etc.
  • To have 3 columns, the lg and md responsive layouts need to use up 4 grid cells for each column.
  • To have 2 columns, the md responsive layout uses 6 grid cells to hold each column. The 3 column is set to zero, to hide it.
  • To have 1 column, the field has to occupy all 12 grid cells.
<uwc-grid container spacing="3">
<uwc-grid item xs="12" sm="12" md="8" lg="8"><uwc-text-field required label="Address" value=""></uwc-text-field></uwc-grid>
<uwc-grid item xs="0" sm="0" md="4" lg="4"><!-- Blank Cell --></uwc-grid>
<uwc-grid item xs="12" sm="4" md="4" lg="4"><uwc-text-field required label="City" value=""></uwc-text-field></uwc-grid>
<uwc-grid item xs="12" sm="4" md="4" lg="4"><uwc-select required label="State">
<uwc-list-item>Aberdeen</uwc-list-item>
<uwc-list-item>Manchester</uwc-list-item>
<uwc-list-item>Brighton</uwc-list-item>
<uwc-list-item>Hull</uwc-list-item>
</uwc-select></uwc-grid>
<uwc-grid item xs="12" sm="4" md="4" lg="4"><uwc-text-field required label="Postcode" value=""></uwc-text-field></uwc-grid>
</uwc-grid>
  • The "Address" field has 2 responsive layouts. One for xs and sm, and one for md and lg.

    1. When in xs or sm, it will occupy the full width of the screen, hence it needs a value of 12 to signify that it want to take up all 12 grid cells. The blank column is set to zero to hide it.
    2. when in md or lg, it will occupy 2/3 of the screen, leaving the right most column blank. So we set the field to occupy 8 grid cells and the blank column to occupy 4 grid cells, a total of 12.
  • The "city", "state" and "postcode" fields would occupy 3 columns in all but xs where it occupies one column.

    1. xs is set to 12 to occupy the whole width, this forces the next field to take the next line. Since that too is set to 12, it occupies the whole width, similary for the 3 field. Hence we have 3 fields all stacked on top of each other.
    2. for the remainder layouts (sm, md, lg), we decided to leave them occupying 3 columns. Hence they are given 4 grid cells each. Basically 1/3 of the screen.

Quite often, there is a need to change the page dynamically and/or to interact withthe user. We will show some javascript to make some modifications to the screen.

  1. Add some data to the "Address" field.
  2. Make the "Address" field read-only.
  3. Show some error messages on the "city" and "State" field

First we add some id's to each element

<uwc-grid container spacing="3">
<uwc-grid item xs="12" sm="12" md="8" lg="8"><uwc-text-field required label="Address" id="address"></uwc-text-field></uwc-grid>
<uwc-grid item xs="0" sm="0" md="4" lg="4"><!-- Blank Cell --></uwc-grid>
<uwc-grid item xs="12" sm="4" md="4" lg="4"><uwc-text-field required label="City" id="city"></uwc-text-field></uwc-grid>
<uwc-grid item xs="12" sm="4" md="4" lg="4"><uwc-select required label="State" id="state">
<uwc-list-item>Aberdeen</uwc-list-item>
<uwc-list-item>Manchester</uwc-list-item>
<uwc-list-item>Brighton</uwc-list-item>
<uwc-list-item>Hull</uwc-list-item>
</uwc-select></uwc-grid>
<uwc-grid item xs="12" sm="4" md="4" lg="4"><uwc-text-field required label="Postcode" id="postcode"></uwc-text-field></uwc-grid>
</uwc-grid>

Then add the javascript

<script type="text/javascript">
function buttonClickHandler(event) {
// Show error message
document.getElementById("city").setCustomValidity('API Error Message');
document.getElementById("city").reportValidity();
document.getElementById("state").setCustomValidity("My Error Message");
document.getElementById("state").reportValidity();
// Set some values using javascript
document.getElementById("address").value="2, People Building, Hemel Hempstead.";
document.getElementById("address").setAttribute("readOnly", "true");
};
</script>

Finally we assign this buttonClickHandler to a button

<uwc-grid xs="2" sm="2" md="2" lg="2">
<uwc-button trailingIcon contained onclick="buttonClickHandler()" size="large" type="submit" icon="send" label="Submit"></uwc-button>
</uwc-grid>

When you click the submit button...

See the fields with red error messages and the address field now has some data.

Grid Tutorial

  • Add event handlers
  • Form submission