Categories
BootstrapVue

BootstrapVue — File Input

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 file input with BootstrapVue.

File Upload

We can add a file upload widget by using the b-form-file component.

For example, we can write:

<template>
  <div id="app">
    <b-form-file
      v-model="file"
      :state="Boolean(file)"
      placeholder="choose file..."
      drop-placeholder="drop file..."
    ></b-form-file>
    <p>{{ file ? file.name : '' }}</p>
  </div>
</template>

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

The b-form-file component lets us add a file input.

v-model gets the selected file object and binds it to the file property.

state is for setting the validation state.

placeholder is a placeholder displayed for the regular file input.

drop-placeholder is the placeholder displayed when we’re dropping files.

Then we display the file name below it.

We can use the multiple prop to let users add multiple files.

Drop mode is enabled by default.

We can add the no-drop prop to disable dropping files.

Limiting File Types

We can limit the file types that can be uploaded.

To do that, we just have to set the accept prop.

For instance, we can write:

<b-form-file accept="image/jpeg, image/png, image/gif"></b-form-file>

or:

<b-form-file accept=".jpg, .png, .gif"></b-form-file>

We can set the mime type or the file extension of the file.

Sizing

The sizing of the file upload box can also be changed.

The size prop lets us change the size. 'sm' is for small and 'lg' is for large.

Otherwise, we get the default size.

For instance, we can write:

<template>
  <div id="app">
    <b-form-file v-model="file" :state="Boolean(file)" size="sm"></b-form-file>
    <p>{{ file ? file.name : '' }}</p>
  </div>
</template>

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

File Name Formatter

We can format the file name for the selected file the way we like.

To do that, we can create a function.

For instance, we can write:

<template>
  <div id="app">
    <b-form-file multiple :file-name-formatter="formatNames"></b-form-file>
    <p>{{ file ? file.name : '' }}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  methods: {
    formatNames(files) {
      if (files.length === 1) {
        return files[0].name;
      } else {
        return `${files.length} files chosen`;
      }
    }
  }
};
</script>

The files parameter is an array-like object with the chosen files.

Therefore, we can check the length property and return the string we want to display.

Formatting File Names with Scoped Slots

To display something other than text when files are chosen, we can add items to slots.

To do that, we write:

<template>
  <div id="app">
    <b-form-file multiple>
      <template slot="file-name" slot-scope="{ names }">
        <b-badge>chosen file: {{ names[0] }}</b-badge>
        <b-badge v-if="names.length > 1" variant="dark" class="ml-1">{{ names.length }} files chosen</b-badge>
      </template>
    </b-form-file>
  </div>
</template>

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

We populate the file-name slot with our own content.

The names array lets us access the file names.

Autofocus

We can add the autofocus prop to make the file input focus when it’s loaded or when it’s displayed inside a keep-alive component.

Clearing File Selection

We can clear file selection by setting the file object to null or undefined .

For instance, we can write:

<template>
  <div id="app">
    <b-form-file v-model="file"></b-form-file>
    <b-button @click="file = undefined">Reset file input</b-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      file: undefined
    };
  }
};
</script>

We just set file to undefined to clear the input.

Also, we can clear it by setting a ref for the file input and then call reset on it.

We can write:

<template>
  <div id="app">
    <b-form-file v-model="file" ref="file-input" class="mb-2"></b-form-file>
    <b-button @click="clearFiles">Reset file input</b-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      file: undefined
    };
  },
  methods: {
    clearFiles() {
      this.$refs["file-input"].reset();
    }
  }
};
</script>

This does the same thing as resetting by clearing the model.

Conclusion

BootstrapVue comes with a file input component.

We can do all the things we can think of with it, like dropping files, selecting files, and clearing file selection.

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.