Categories
BootstrapVue

BootstrapVue — Datepicker

To make good looking Vue apps, we need to style our components. To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to add a date picker to our Vue app.

Datepicker

BootstrapVue comes with its own date picker component. We can use it instead of using another library to add one. We can add the b-form-datepicker to add the date picker.

For instance, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value"></b-form-datepicker>
    <p>Value: '{{ value }}'</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: "2020-05-13"
    };
  }
};
</script>

We have the b-form-datepicker with the v-model directive to bind to the value field. value is updated with the date string when we select a date.

It can take a few props to customize it. We can disable it, which means the navigation and date selection are all disabled. The disabled prop disables the date picker.

Also, we can make the date picker read-only, which means that we can navigate through the dates, but can’t select any date. The readonly prop makes the date picker read-only.

For instance, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" :disabled="true"></b-form-datepicker>
    <p>Value: '{{ value }}'</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: "2020-05-13"
    };
  }
};
</script>

to disable the date picker.

And we write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" :readonly='true'></b-form-datepicker>
    <p>Value: '{{ value }}'</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: "2020-05-13"
    };
  }
};
</script>

to make the date picker read-only.

Date Constraints

We can restrict the date range that’s selectable with the min and max props.

For example, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" :min="min" :max="max"></b-form-datepicker>
    <p>Value: '{{ value }}'</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: "2020-05-13",
      min: new Date(2020, 4, 1),
      max: new Date(2020, 4, 31)
    };
  }
};
</script>

Then we can only select dates within May 2020.

Disabling Dates

We don’t have to restrict dates from a range. We can disable dates based on some other selection criteria. The date-disabled-fn prop takes a function for dates to disable.

For instance, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" :date-disabled-fn="dateDisabled"></b-form-datepicker>
  </div>
</template>

<script>
export default {
  name: "App",
  methods: {
    dateDisabled(ymd, date) {
      const weekday = date.getDay();
      return weekday >= 1 && weekday <= 5;
    }
  }
};
</script>

Then we can only select weekend dates. date is a Date instance so we can call methods available for it like getDay .

Validation States

b-form-datepicker can have the validation state displayed. We just have to pass a value to the state prop to display it.

For instance, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" :state="isValid"></b-form-datepicker>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: undefined
    };
  },
  computed: {
    isValid() {
      return new Date(this.value).getDay() === 1;
    }
  }
};
</script>

We have a computed property isValid that returns whether the day of the week is 1. 1 indicates that it’s Tuesday. So when we pick a Tuesday, then the date picker is green. Otherwise, it’s red.

The validation status is only displayed when a date is picked.

Styling

We can change the size of the date picker with the size prop.

For example, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" size="sm"></b-form-datepicker>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: undefined
    };
  }
};
</script>

We set the size to 'sm' , which is small. Alternatively, we can set it to 'lg' , which is big. The placeholder can be set with the placeholder prop.

We can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" placeholder="Choose a date"></b-form-datepicker>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: undefined
    };
  }
};
</script>

to display the ‘Choose a date’ placeholder.

Conclusion

The b-form-datepicker lets us pick dates and show it.

It has many customization options.

We can set the date range users can select.

Also, we can change the size and placeholder.

Categories
BootstrapVue

BootstrapVue — Customizing Checkboxes

To make good looking Vue apps, we need to style our components. To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to customize checkboxes with BootstrapVue.

HTML Checkbox Text

We can have HTML in our checkbox text. Instead of the text property, we use the html to set the text.

However, we should be careful of cross-site scripting attacks since the HTML won’t be sanitized before being displayed.

For example, we can write:

<template>
  <div id="app">
    <b-form-group label="fruits">
      <b-form-checkbox-group v-model="selected" :options="options" name="fruits"></b-form-checkbox-group>
    </b-form-group>
    <div>{{ selected }}</div>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      options: [
        { text: "orange", value: "orange", disabled: false },
        { html: "<b>apple</b>", value: "apple", disabled: false },
      ],
      selected: []
    };
  }
};
</script>

We have html: “<b>apple</b>” to make ‘apple’ bold.

Inline and Stacked Checkboxes

We can add the stacked prop on b-form-checkbox-groip to stack the options.

For instance, we can write:

<template>
  <div id="app">
    <b-form-group label="fruits">
      <b-form-checkbox-group stacked v-model="selected" :options="options" name="fruits"></b-form-checkbox-group>
    </b-form-group>
    <div>{{ selected }}</div>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      options: [
        { text: "orange", value: "orange", disabled: false },
        { text: "apple", value: "apple", disabled: false },
      ],
      selected: []
    };
  }
};
</script>

Then the checkboxes will be stacked instead of being side by side.

Sizing

We can change the sizing of the checkbox. To do that, we can use the size prop. The available options are sm or lg

For instance, we can write:

<template>
  <div id="app">
    <b-form-checkbox size="sm">Small</b-form-checkbox>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

to get an extra small checkbox.

Button Style Checkbox

We can change the checkbox to look like a button. We can do that with the button prop.

For instance, we can write:

<template>
  <div id="app">
    <b-form-checkbox v-model="checked" name="checked" button>
      <b>{{ checked }}</b>
    </b-form-checkbox>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      checked: false
    };
  }
};
</script>

We add the button prop to the b-form-checkbox and then we put our content inside the tags. Also, we can add the button-variant to change the style of the button.

For instance, we can write:

<template>
  <div id="app">
    <b-form-checkbox v-model="checked" button-variant="info" name="checked" button>
      <b>{{ checked }}</b>
    </b-form-checkbox>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      checked: false
    };
  }
};
</script>

The button-variant takes a variant string like other BootstrapVue components. info would make the button green.

Grouped Button Style Checkboxes

Like buttons, we can group button style checkboxes. We can use the b-form-checkbox-group to create a group of button style checkboxes.

For example, we can write:

<template>
  <div id="app">
    <b-form-checkbox-group v-model="selected" :options="options" buttons></b-form-checkbox-group>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      selected: [],
      options: ["apple", "orange", "banana"]
    };
  }
};
</script>

We have the buttons prop to turn the checkbox group into a group of button style checkboxes. Also, we can customize them like buttons.

We can use the size and button-variant props on them.

For example, we can write:

<template>
  <div id="app">
    <b-form-checkbox-group
      button-variant="primary"
      size="lg"
      v-model="selected"
      :options="options"
      buttons
    ></b-form-checkbox-group>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      selected: [],
      options: ["apple", "orange", "banana"]
    };
  }
};
</script>

We made our button style checkboxes big with size="lg" and button-variant='primary' make them blue.

Switch Style Checkboxes

We can make a checkbox look like a switch toggle.

For instance, we can write:

<template>
  <div id="app">
    <b-form-checkbox v-model="checked" name="check-button" switch>{{ checked }}</b-form-checkbox>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      checked: false
    };
  }
};
</script>

We add the switch prop to make a checkbox look like a toggle switch.

Conclusion

We can do many things with checkboxes.

The text can be customized.

We can also make each checkbox look like buttons.

Their sizes can also change.

We can also make checkboxes look like a toggle switch.

Categories
BootstrapVue

BootstrapVue — Button Groups and Toolbars, and Calendar

To make good looking Vue apps, we need to style our components.

To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to add button groups and toolbars to group buttons.

We also look at how to use the calendar to let users select dates.

Button Group

Button groups let us group a series of buttons together.

To add a button group, we can use the b-button-group component.

For instance, we can write:

<template>
  <div id="app">
    <b-button-group>
      <b-button>Button 1</b-button>
      <b-button>Button 2</b-button>
    </b-button-group>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We have the b-button-group component with b-button components inside.

Vertical Groups

We can make the group vertical by adding the vertical prop:

<template>
  <div id="app">
    <b-button-group vertical>
      <b-button>Button 1</b-button>
      <b-button>Button 2</b-button>
    </b-button-group>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Then we make the buttons arranged vertically.

Dropdown Menu

We can add dropdown menus inside our button group.

For instance, we can write:

<template>
  <div id="app">
    <b-button-group>
      <b-button>Button 1</b-button>
      <b-dropdown right text="Menu">
        <b-dropdown-item>foo</b-dropdown-item>
        <b-dropdown-divider></b-dropdown-divider>
        <b-dropdown-item>bar</b-dropdown-item>
      </b-dropdown>
    </b-button-group>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We have a dropdown menu on the right of the button with the b-dropdown component.

We have b-dropdown-itemn and b-dropdown-divider components inside.

Button Toolbar

A button toolbar is similar to a button group.

It groups multiple buttons together.

Also, we can use it to group multiple button groups together.

We use the b-button-toolbar component to create a button toolbar.

For instance, we can write:

<template>
  <div id="app">
    <b-button-toolbar>
      <b-button-group class="mx-1">
        <b-button>apple</b-button>
        <b-button>orange</b-button>
      </b-button-group>
      <b-button-group class="mx-1">
        <b-button>banana</b-button>
        <b-button>grape</b-button>
      </b-button-group>
    </b-button-toolbar>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

The mx-1 class make the groups spaced apart.

We apply to the button groups to add a margin between them.

We can also add a dropdown menu inside a button toolbar as we do with button groups.

For instance, we can write:

<template>
  <div id="app">
    <b-button-toolbar>
      <b-button-group class="mx-1">
        <b-button>apple</b-button>
        <b-button>orange</b-button>
      </b-button-group>

      <b-dropdown class="mx-1" right text="menu">
        <b-dropdown-item>foo</b-dropdown-item>
        <b-dropdown-item>bar</b-dropdown-item>
      </b-dropdown>

      <b-button-group class="mx-1">
        <b-button>banana</b-button>
        <b-button>grape</b-button>
      </b-button-group>
    </b-button-toolbar>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We have the same classes to keep them apart.

The text prop sets the text for the dropdown button.

b-dropdown-item have the dropdown items.

Calendar

BootstrapVue has the b-calendar component to create a calendar.

For instance, we can write:

<template>
  <div id="app">
    <b-calendar v-model="date" @context="onContext" locale="en-US"></b-calendar>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      date: new Date()
    };
  },
  methods: {
    onContext(e) {
      console.log(e);
    }
  }
};
</script>

We have the onContext method to get the selected date value when the calendar is clicked.

date is the model, which we bind to the calendar with v-model .

Disabled and Readonly State

We can disable the calendar with the disabled prop.

Likewise, we can add the readonly prop to make it read-only.

The difference between them is that disabled removes all interactivity, but readonly will disable selecting a date, but keep the component interactive.

So if we have:

<b-calendar disabled locale="en-US"></b-calendar>

then we disable the whole calendar.

If we have

<b-calendar readonly locale="en-US"></b-calendar>

Then we can navigate through the calendar but can’t select anything.

Date Constraints

We can limit the date range that can be selected with the min and max props.

For instance, we can write:

<template>
  <div id="app">
    <b-calendar v-model="date" :min="min" :max="max" locale="en-US"></b-calendar>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      date: new Date(),
      min: new Date(2020, 0, 1),
      max: new Date(2020, 11, 31)
    };
  }
};
</script>

Then we can only select dates that are in the year 2020.

Conclusion

We can use the calendar component to let users select dates.

The selectable date range can be limited.

Also, we can group buttons with button groups and button toolbars.

Categories
BootstrapVue

BootstrapVue — Checkboxes and Data Lists

To make good looking Vue apps, we need to style our components.

To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to create more checkboxes and data lists with BootstrapVue.

Datalists

We can add a datalist element which lets us create a native autocomplete input.

To add it, we can use the b-form-datalist component.

For example, we can write:

<template>
  <div id="app">
    <label for="fruit">fruit</label>
    <b-form-input list="fruit-list" id="fruit"></b-form-input>
    <b-form-datalist id="fruit-list" :options="options"></b-form-datalist>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
     options: ['apple', 'orange', 'grape']
    };
  },
};
</script>

We have a b-form-input which provides us with an input text box.

Then we have the b-form-datalist to add a autocomplete list where we can choose the items listed in the options array.

We add the options prop to b-form-datalist to set the options available.

The id value of b-form-datalist has to be the same as the list attribute value in b-form-input so that the b-form-datalist will be used for populating the options.

Checkbox

To add a checkbox to a form, we can use the b-form-checkbox component.

For instance, we can write:

<template>
  <div id="app">
    <b-form-checkbox v-model="status" name="checkbox" value="yes" unchecked-value="no">accept?</b-form-checkbox>

    <div>
      State:
      {{ status }}
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      status: ""
    };
  }
};
</script>

We have the b-form-checkbox component.

It has the v-model directive to bind the value to the state we want.

Also, we set the value prop to set the value of the status model if the checkbox is checked.

Likewise, we have the unchecked-value to set the value of the status model if the checkbox is unchecked.

Then when we toggle the checkbox between checked and unchecked, we see the text below toggle between ‘yes’ or ‘no’.

Multiple Choice Checkboxes

We can create mulitiple checkboxes with the b-form-checkbox-group component.

For instance, we can write the following code to create one:

<template>
  <div id="app">
    <b-form-group label="fruits">
      <b-form-checkbox-group v-model="selected" :options="options" name="fruits"></b-form-checkbox-group>
    </b-form-group>
    <div>{{ selected }}</div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      options: ["apple", "orange", "grape"],
      selected: []
    };
  }
};
</script>

The label prop has the label content.

We have the selected state for holding the selected values. It must be an array.

The options array for rendering the checkboxes with the given options.

Now when we click the checkboxes, we see the selected items.

Equivalently, we can use the b-form-checkbox-group with the b-form-checkbox for more flexibility.

For instance, we can write:

<template>
  <div id="app">
    <b-form-group label="fruit:">
      <b-form-checkbox-group id="friots" v-model="selected" name="fruits">
        <b-form-checkbox value="orange">orange</b-form-checkbox>
        <b-form-checkbox value="apple">apple</b-form-checkbox>
        <b-form-checkbox value="grape">grape</b-form-checkbox>
      </b-form-checkbox-group>
    </b-form-group>
    <div>{{ selected }}</div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selected: []
    };
  }
};
</script>

We added b-form-checkbox components instead of using the options prop to render checkboxes.

The value prop has the value that’ll be set if we check a checkbox.

Therefore, we get the same result as the previous example.

Options Array

Each entry of the options array can have up to 3 properties.

It can have the text , value , and disabled properties.

For instance, we can write:

<template>
  <div id="app">
    <b-form-group label="fruits">
      <b-form-checkbox-group v-model="selected" :options="options" name="fruits"></b-form-checkbox-group>
    </b-form-group>
    <div>{{ selected }}</div>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      options: [
        { text: "orange", value: "orange", disabled: false },
        { text: "apple", value: "apple", disabled: false },
        { text: "grape", value: { grape: "grape" }, disabled: true }
      ],
      selected: []
    };
  }
};
</script>

Instead of an array of strings, we have an array of objects with the text , value , and disabled properties.

If disabled is true , then the checkbox would be disabled.

The value property can have anything. It’s the value that’ll be in the selected array if we check the checkbox.

Conclusion

We can create checkboxes with the b-form-checkbox and associated components.

It’s very flexible, we can change the value and text to whatever we want.

Also, we can choose to enable or disabled checkboxes

Datalists let us create a simple autocomplete input.

Categories
BootstrapVue

BootstrapVue — Text Areas

To make good looking Vue apps, we need to style our components.

To make our lives easier, we can use components with styles built-in.

In this article, we’ll look at how to add a text area.

Text Area

We can add a text area to a form to let us add multiline text inputs.

It can support auto height sizing, minimum and maximum number of rows, and displaying validation states.

To add one, we use the b-form-textarea component.

For instance, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" rows="3" max-rows="6"></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  }
};
</script>

We added the b-form-textarea input box.

v-model binds text to the inputted value.

placeholder has the placeholder.

rows is set to 3, but we can enter up to 6 rows of text with the max-rows .

The entered value is displayed.

Sizing

We can change the size of the input.

To change the size, we can use the size prop.

For example, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" size="sm"></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  }
};
</script>

to shrink the text area.

The possible values are 'sm' or 'lg' . 'sm' is for small. 'lg' is for large.

Displayed Rows

We can change the rows with the rows prop.

For example, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" rows="8"></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  }
};
</script>

then we have 8 rows in our text area.

Disable Resize

The no-resize prop disables the resizing of the text area.

For instance, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" rows="3" no-resize></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  }
};
</script>

The no-resize prop disables the resize handle.

Auto Height

By default, the b-form-textarea will adjust its height to fit the content.

We can use the no-auto-shrink prop to disable shrinking if the inputted rows are reduced.

sticky will make it never shrink.

For instance, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" rows="3" max-rows="8" no-auto-shrink></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  }
};
</script>

Validation States

We can set the state prop to display the validation state of the input.

For instance, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" :state="text.length >= 5"></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  }
};
</script>

We have the state prop that checks if text ‘s length is bigger than or equal to 5 for it to be considered valid.

The box is displayed in red if it’s invalid.

Otherwise, it’s displayed in green.

Text Formatter

We can format the inputted text the way we want.

To do that, we can set the formatter prop.

For instance, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" :formatter="formatter"></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  },
  methods: {
    formatter(value) {
      return value.toUpperCase();
    }
  }
};
</script>

We have the formatter method to return the text turned upper case.

Make the Text Area Display as Plain Text

The plaintext prop makes the text area display as plain text.

For example, we can write:

<template>
  <div id="app">
    <b-form-textarea v-model="text" placeholder="Enter text" plaintext></b-form-textarea>
    <p>{{text}}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      text: ""
    };
  }
};
</script>

Now we don’t have the border displayed.

Make it Read Only

The readonly prop makes it read-only.

Conclusion

We can use the b-form-textarea to add a text area to our app.

It can be customized in various, like changing the size or display it as plain text.