Categories
BootstrapVue

BootstrapVue — Text Area Customization and Time Picker

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 a text area and add a time picker.

v-model Modifiers

The lazy , trim , number modifiers for v-model aren’t supported with the b-form-textarea component.

However, there are trim , number , and lazy props can be used to replace those modifiers.

Debounce

We can add the debounce prop to delay the input value binding to the state.

For instance, we can write:

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

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

Then we delay the input value binding by 500 ms.

Autofocus

The autofocus prop can be added to the b-form-textarea to make it focus when the component loads or reactive in a keep-alive component.

Timepicker

We can use the b-form-timepicker component to add a time picker.

For instance, we can write:

<template>
  <div id="app">
<b-form-timepicker v-model="value" locale="en"></b-form-timepicker>
    <div>Value: {{ value }}</div>
  </div>
</template>

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

Then we can pick the time from the displayed input box.

The locale prop lets us change the locale.

value has the selected value, which is bound to the inputted value with v-model .

Disabled or Read Only States

We can add the disabled prop to remove all interactivity on the b-form-timepicker component.

readonly disables selecting a time, but will keep the component interactive.

We can write:

<b-form-timepicker v-model="value" disabled></b-form-timepicker>

or:

<b-form-timepicker v-model="value" readonly></b-form-timepicker>

to change both.

Validation States

Like other input comments, we can display the validation state with it.

For instance, we can write:

<template>
  <div id="app">
    <b-form-timepicker v-model="value" :state="!!value"></b-form-timepicker>
    <div>Value: {{ value }}</div>
  </div>
</template>

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

We set the state prop to the value of value converted to a boolean.

Then we’ll see a red box if the time isn’t selected.

Otherwise, we see a green box.

Setting Seconds

We can set the seconds by adding the show-seconds prop.

For instance, we can write:

<template>
  <div id="app">
    <b-form-timepicker v-model="value" show-seconds></b-form-timepicker>
    <div>Value: {{ value }}</div>
  </div>
</template>

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

Now we see the seconds box and we can set the seconds.

Sizing

We can add the size prop to change the size of the time picker sizing.

For example, we can write:

<template>
  <div id="app">
    <b-form-timepicker v-model="value" size="sm"></b-form-timepicker>
    <div>Value: {{ value }}</div>
  </div>
</template>

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

to shrink the time picker’s size.

We can also change the value to 'lg' to make it larger than the default.

Optional Controls

We can add optional controls to the time picker with a few props.

For instance, we can add the now-button to let users pick the current time.

reset-button displays a reset button to let users reset the time.

For example, we can write:

<template>
  <div id="app">
    <b-form-timepicker v-model="value" now-button reset-button></b-form-timepicker>
    <div>Value: {{ value }}</div>
  </div>
</template>

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

Then we can see the buttons on the time picker.

Button Only Mode

The button-only prop lets us change the time picker to button-only mode.

For example, we can write:

<template>
  <div id="app">
    <b-input-group class="mb-3">
      <b-form-input v-model="value" type="text" placeholder="Select time"></b-form-input>
      <b-input-group-append>
        <b-form-timepicker v-model="value" right button-only></b-form-timepicker>
      </b-input-group-append>
    </b-input-group>
    <div>Value: {{ value }}</div>
  </div>
</template>

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

Then we have an input box with the time displayed.

But we can’t click it to show the time picker.

The button on the right of the input box lets us select the time.

We need the right prop so that the right edge of the date picker will be aligned to the right side of the button.

Conclusion

We can add denounce to a text area.

Also, we can add a time picker to let us add a time picker control and customize it.

Categories
BootstrapVue

BootstrapVue — Switch Style 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 create switch style checkboxes in BootstrapVue.

Grouped Switch Style Checkboxes

To make a group os switch style checkboxes, we can add the switches prop to b-form-checkbox-group .

For example, we can write:

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

Because of the switches prop on b-form-checkbox-group , we have a group of checkboxes that look like toggle switches.

options sets the checkbox options.

Then when we toggle the switches on and off, we see the selected choices displayed below it.

Switch Sizing

The size prop lets us adjust the size.

So we can write:

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

We can see an extra small switch because we have size='sm' .

Alternatively, we can write size='lg' to make an extra-large switch.

Plain Check Inputs

We can disable the Bootstrap styling for the checkboxes with the plain prop.

For instance, we can write:

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

We have the plain prop on b-form-checkbox-group , so we have the browser native checkbox displayed.

Contextual States

We can add a state prop to display the validation state to the user.

The prop should be added to b-form-checkbox-group , b-form-invalid-feedback , and b-form-valid-feedaback components.

For example, we can write:

<template>
  <div id="app">
    <b-form-checkbox-group
      v-model="selected"
      :options="options"
      :state="state"
      name="checkbox-validation"
    >
      <b-form-invalid-feedback :state="state">please select 1 or more</b-form-invalid-feedback>
      <b-form-valid-feedback :state="state">looks good</b-form-valid-feedback>
    </b-form-checkbox-group>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      selected: [],
      options: ["apple", "orange", "grape"]
    };
  },
  computed: {
    state() {
      return this.selected.length > 0;
    }
  }
};
</script>

We added the state prop to all 3 components.

The state value is computed by checking this.state.length .

If we didn’t select anything, we see that everything is displayed in red and we see ‘please select 1 or more’.

Otherwise, we see ‘looks good’ and everything displayed in green.

Tri-State Support

We can add an indeterminate state to our checkbox.

For instance, we can write:

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

We have the indeterminate prop which is set to the indeterminate field.

If we click ‘toggle indeterminate’, we would see that a dash is displayed in the checkbox.

That indicates that the checkbox is in an indeterminate state.

Conclusion

We can make a tristate checkbox with the indeterminate prop.

Also, we can make a group of switch style checkboxes.

Categories
BootstrapVue

BootstrapVue — Star Rating and Drop Downs

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 star rating inputs and adding dropdowns.

Clear Button

The show-clear prop lets us add a clear button to reset the selected ratings.

For instance, we can write:

<template>
  <div id="app">
    <b-form-rating id="rating-inline"  show-clear show-value  value="4"></b-form-rating>
  </div>
</template>

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

Then there’s an ‘x’ button on the left that we can click to clear the ratings.

Icons

The icons in the rating can be changed.

Therefore, we can display something other than a star.

We’ve to register the IconsPlugin to change the icons.

So we’ve to write:

import Vue from "vue";
import App from "./App.vue";
import { BootstrapVue, IconsPlugin } from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";

Vue.use(BootstrapVue);
Vue.use(IconsPlugin);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

in main.js .

Then in our component, we write:

<template>
  <div id="app">
    <b-form-rating
      icon-empty="heart"
      icon-half="heart-half"
      icon-full="heart-fill"
      icon-clear="x-square"
      precision="2"
      show-clear
      show-value
      v-model='value'
    ></b-form-rating>
  </div>
</template>

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

We set the icon-empty , icon-half , icon-full , and icon-clear props. to change the icons.

icon-empty is for icons that aren’t filled.

icon-half is for the half-filled icons.

icon-full is for the icons that are fully filled.

icon-clear is the icon for the clear button.

Internationalization

The locale of the buttons can be changed.

We can set the locale prop to set the locale.

For instance, we can write:

<template>
  <div id="app">
    <b-form-rating :locale="locale" v-model="value"></b-form-rating>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      locale: "en-US"
    };
  }
};
</script>

to set the locale string.

Select Dropdown

BootstrapVue provides us with a b-form-select component to let us add dropdown menus to our forms.

For instance, we can write:

<template>
  <div id="app">
    <b-form-select v-model="selected" :options="options"></b-form-select>
    <p>{{ selected }}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selected: null,
      options: [
        { value: null, text: "Select a Fruit" },
        { value: "apple", text: "Apple" },
        { value: "orange", text: "Orange" },
        { value: "grape", text: "Grape", disabled: true }
      ]
    };
  }
};
</script>

to create a dropdown menu.

We have an options array, which has the value and text properties.

The text property is what’s displayed.

value is the value that’s set as the value of the state.

disabled will prevent the option from being selected if it’s true .

We set the array as the value of the options prop.

The selected value is displayed in the p element.

The size can be set with the size prop like other components.

Options

We can add option elements individually with the b-form-select-option component.

For example, we can write:

<template>
  <div id="app">
    <b-form-select v-model="selected" :options="options">
      <b-form-select-option :value="null">Select a Fruit</b-form-select-option>
      <b-form-select-option value="apple">Apple</b-form-select-option>
      <b-form-select-option value="orange">Orange</b-form-select-option>
      <b-form-select-option value="grape" disabled>Grape</b-form-select-option>
    </b-form-select>
    <p>{{ selected }}</p>
  </div>
</template>

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

We have the b-form-select-option components.

The value prop is the value that’s bound to the selected state.

disabled stops the option from being selected.

Option Groups

The b-form-select-option-group lets us add groups of options.

For instance, we can write:

<template>
  <div id="app">
    <b-form-select v-model="selected" :options="options">
      <b-form-select-option :value="null">Select a Fruit</b-form-select-option>
      <b-form-select-option value="apple">apple</b-form-select-option>
      <b-form-select-option-group label="orange">
        <b-form-select-option :value="{orange: 'juice'}">orange juice</b-form-select-option>
        <b-form-select-option :value="{ orange: 'ice cream' }">orange ice cream</b-form-select-option>
      </b-form-select-option-group>
    </b-form-select>
    <p>{{ selected }}</p>
  </div>
</template>

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

to add option groups.

We have the b-form-select-option-group and b-form-select-option components.

The label prop is displayed as the heading of the group.

Conclusion

We can customize star rating components by adding icons and changing sizes.

Categories
BootstrapVue

BootstrapVue — Datepicker Customization

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 the BootstrapVue date picker.

Dropdown Placement

We can change the drop-down placement with the right , dropup , dropright , dropleft , no-flip and offset props. They change the date picker placement.

no-flip doesn’t move the date picker even if it’s cut off.

offet lets us shift the date picker position.

The rest let us change the direction.

Dark Mode

We can set the dark prop to true to enable dark mode.

For instance, we can write:

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

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

to make the date picker’s background black.

Button Only Mode

By default, the date picker is shown when we click on the input box. And the input box is shown by default.

To only show the date picker when we click the calendar icon, we can use the button-only prop. This will also remove the input box.

So, we can write:

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

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

Then only the calendar icon is shown.

And we can click on it to see the date picker.

Date String Format

We can change the date string format of the date picker.

For instance, we can write:

<template>
  <div id="app">
    <b-form-datepicker
      v-model="value"
      :date-format-options="{ year: 'numeric', month: 'short', day: '2-digit', weekday: 'short' }"
    ></b-form-datepicker>
  </div>
</template>

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

We added the date-format-options prop.

The object we pass in has the year , month , day , and weekday props to set the selected date format.

'numeric' means it’s a number.

'short' means we show the abbreviation.

'2-digit' means we show 2 digits.

Therefore, we should see that the selected date has the full year, abbreviated month and weekday, and the day number.

Full-Width Calendar Dropdown

We can adjust the calendar dropdown width with the calendar-width and the menu-class prop.

For instance, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" menu-class="w-100" calendar-width="100%"></b-form-datepicker>
  </div>
</template>

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

menu-class is 'w-100' , which makes the date picker fills the width of the screen.

calendar-width is '100%' so that the content fills the box.

Internationalization

We can change the locale of the date picker.

For instance, we can write:

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

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

We have the locale field set to 'de' to set the date picker to the Germany locale.

Now we’ll see the weekdays and months are displayed in German.

We can also add the labels to change the rest of the text.

For instance, we can write:

<template>
  <div id="app">
    <b-form-datepicker v-model="value" v-bind="labels.de" :locale="locale"></b-form-datepicker>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: undefined,
      locale: "de",
      labels: {
        de: {
          labelPrevDecade: "Vorheriges Jahrzehnt",
          labelPrevYear: "Vorheriges Jahr",
          labelPrevMonth: "Vorheriger Monat",
          labelCurrentMonth: "Aktueller Monat",
          labelNextMonth: "Nächster Monat",
          labelNextYear: "Nächstes Jahr",
          labelNextDecade: "Nächstes Jahrzehnt",
          labelToday: "Heute",
          labelSelected: "Ausgewähltes Datum",
          labelNoDateSelected: "Kein Datum gewählt",
          labelCalendar: "Kalender",
          labelNav: "Kalendernavigation",
          labelHelp: "Mit den Pfeiltasten durch den Kalender navigieren"
        }
      }
    };
  }
};
</script>

Now the rest of the labels are in German.

This includes all the navigation labels and instructions.

Likewise, we can add the weekday field to change the starting week daty.

weekdays to add labels for weekdays.

showDecadeNav toggles on or off the decade navigation.

hideHeader hide header hides the header if it’s true .

We can set them as follows:

<template>
  <div id="app">
    <b-form-datepicker
      :start-weekday="weekday"
      :show-decade-nav="showDecadeNav"
      :hide-header="hideHeader"
      v-model="value"
      v-bind="labels.de"
      :locale="locale"
    ></b-form-datepicker>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: undefined,
      locale: "de",
      labels: {
        de: {
          labelPrevDecade: "Vorheriges Jahrzehnt",
          labelPrevYear: "Vorheriges Jahr",
          labelPrevMonth: "Vorheriger Monat",
          labelCurrentMonth: "Aktueller Monat",
          labelNextMonth: "Nächster Monat",
          labelNextYear: "Nächstes Jahr",
          labelNextDecade: "Nächstes Jahrzehnt",
          labelToday: "Heute",
          labelSelected: "Ausgewähltes Datum",
          labelNoDateSelected: "Kein Datum gewählt",
          labelCalendar: "Kalender",
          labelNav: "Kalendernavigation",
          labelHelp: "Mit den Pfeiltasten durch den Kalender navigieren"
        }
      },
      showDecadeNav: false,
      hideHeader: false,
      weekday: 0,
      weekdays: [
        { value: 0, text: "Sunday" },
        { value: 1, text: "Monday" },
        { value: 6, text: "Saturday" }
      ]
    };
  }
};
</script>

We added a few props to set them.

Conclusion

There are many customization options with the date picker.

We can change the locales and labels as we wish.

Categories
BootstrapVue

BootstrapVue — Star Rating Customization

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 star rating inputs.

Number of Stars

By default, the b-form-rating displays 5 stars.

We can change the number if starts to display with the stars prop.

For instance, we can write:

<template>
  <div id="app">
    <b-form-rating v-model="value" stars="8"></b-form-rating>
  </div>
</template>

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

We set stars to 8, so we see 8 stars.

Show Stars Value

The show-value prop lets us show the value of the stars.

For example, we can write:

<template>
  <div id="app">
    <b-form-rating v-model="value" show-value></b-form-rating>
  </div>
</template>

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

Then when we click the stars, we see how many stars we set.

Precision

The stars don’ have to always increment by 1.

We can change the precision by using the show-value-precision prop.

For instance, we can write:

<template>
  <div id="app">
    <b-form-rating v-model="value" readonly show-value precision="2"></b-form-rating>
  </div>
</template>

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

Then we can display star value to 2 decimal places.

However, we can’t select a part of a star.

Show Maximum Value

We can use the show-value-max prop to show the maximum value of the stars.

For instance, we can write:

<template>
  <div id="app">
    <b-form-rating v-model="value" readonly show-value show-value-max precision="2"></b-form-rating>
  </div>
</template>

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

We have the show-value and show-value-max props to display the selected and maximum star values respectively.

Sizing

Like other components, we can change the sizing of the star rating component with the size prop.

The value can be 'sm' or ’lg' ,

For example, we can write:

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

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

and we get extra small stars.

'lg' will make the stars extra large.

Make Stars Inline

The stars can be made inline with the inline prop.

For instance, we can write:

<template>
  <div id="app">
    <b-form-rating inline value="4"></b-form-rating>
  </div>
</template>

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

Then the rating component will be displayed inline.

Borderless

The star rating component has a border by default.

We can remove it with the no-border prop.

For example, we can write:

<template>
  <div id="app">
    <b-form-rating no-border value="4"></b-form-rating>
  </div>
</template>

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

Now there’s no border displayed.

Disable Input

We can stop users from selecting ratings by using the disabled prop.

For instance, we write:

<template>
  <div id="app">
    <b-form-rating disabled value="4"></b-form-rating>
  </div>
</template>

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

Make Stars Read Only

We can make the stars component read-only, which means it’s focusable but we can’t choose anything.

For example, we can write:

<template>
  <div id="app">
    <b-form-rating readonly value="4"></b-form-rating>
  </div>
</template>

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

Conclusion

There are lots of customization options for the star rating component.

The icons can be changed.

We add half-filled icons. Also, we can add a clear button to clear the ratings.