Categories
BootstrapVue

BootstrapVue — Time Picker Internationalization and Images

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 time picker and images.

Time Picker Internationalization

We can change the label values of the time picker to make it work with different locales.

For example, we can write:

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

<script>
export default {
  name: "App",
  data() {
    return {
      value: "",
      de: {
        labelHours: "Stunden",
        labelMinutes: "Minuten",
        labelSeconds: "Sekunden",
        labelIncrement: "Erhöhen",
        labelDecrement: "Verringern",
        labelSelected: "Ausgewählte Zeit",
        labelNoTimeSelected: "Keine Zeit ausgewählt",
        labelCloseButton: "Schließen"
      }
    };
  }
};
</script>

We have the v-bind directive that binds the labels as props to the b-form-timerpicker component.

Now we’ll see Germany labels for the controls instead of the default English labels.

Hour Cycle

We can change how the hours are displayed by changing the hour cycle.

There are 4 ways to display the hours.

'h12' displays from 1 to 12.

'h23' displays from 0 to 23.

'h11' displays 0 to 11, and 'h24' displays 1 to 24.

These are all available as their own props.

To display hours in the 12-hour style, we set the hour12 prop to true.

To display 24 hours style, we set hour12 to false .

The formatted time string may be h12 or h23 depending on the value returned by Intl.DateTimeFormat .

For instance, we can write:

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

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

and display hours from 1 to 12, but it can be formatted as 0 to 11 or 1 to 12 depending on the locale.

Images

We can add the b-img component to display responsive images.

There’s also the b-img-lazy to load images only when they’re shown on the screen.

For instance, we can write:

<template>
  <div id="app">
    <b-img src="http://placekitten.com/200/200" fluid alt="cat"></b-img>
  </div>
</template>

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

to add an image to the component.

fluid means that the image won’t grow with its container.

To make it grow with the container, we can use the fluid-grow prop.

For instance, we can write:

<template>
  <div id="app">
    <b-img src="http://placekitten.com/200/200" fluid-grow alt="cat"></b-img>
  </div>
</template>
<script>
export default {
  name: "App"
};
</script>

Thumbnails

To make the image with the thumbnail, we can use the thumbnail prop.

For example, we can write:

<template>
  <div id="app">
    <b-img src="http://placekitten.com/200/200" thumbnail></b-img>
  </div>
</template>

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

This will make the image display as a thumbnail.

Rounded Corners

We can make the image corners rounded by using the rounded prop.

For instance, we can write:

<template>
  <div id="app">
    <b-img src="http://placekitten.com/200/200" rounded></b-img>
  </div>
</template>

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

Now we have rounded corners on our image.

It can also have a set value set for it, including 'top' , 'right' , 'bottom' , 'left' , 'circle' , or a 0.

0 turns off rounding.

So we can write:

<template>
  <div id="app">
    <b-img src="http://placekitten.com/200/200" rounded="circle"></b-img>
  </div>
</template>

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

Then we can see the images rounded.

Aligning Images

We can align the images by adding the left, center and right props.

For instance, we can add the image by using those props as follows:

<template>
  <div id="app">
    <b-img src="http://placekitten.com/200/200" right></b-img>
  </div>
</template>

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

Then we align the image to the right side of the page.

Solid Color Images

We can make the image a solid color.

For instance, we can write:

<template>
  <div id="app">
    <b-img blank blank-color="green" alt="green image" width='200' height='200'></b-img>
  </div>
</template>

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

We have the blank prop to make the image blank.

blank-color sets the color.

alt is the descriptive text.

width is the width and height is the height of the image.

Conclusion

We can customize the locale of the time picker.

Also, we can add images with the b-img component.

They can have an image or be blank.

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.