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.

Categories
BootstrapVue

BootstrapVue — Badges, Breadcrumbs, and Buttons

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 badges, breadcrumbs, and buttons with BootstrapVue.

Badges

Badges are small tags for adding context to any content.

We can use it by using the b-badge component:

<template>
  <div id="app">
    <h2>Feature
      <b-badge>New</b-badge>
    </h2>
  </div>
</template>

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

We just add it straight into the template without any props.

There are variations like primary , secondary , etc.

We can add the variant prop to set the variant.

For instance, we can write:

<template>
  <div id="app">
    <b-badge variant="primary">New</b-badge>
  </div>
</template>

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

Pill badges are badges that are more rounded than usual.

We just add the pill prop:

<template>
  <div id="app">
    <b-badge pill variant="primary">Primary</b-badge>
  </div>
</template>

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

Breadcrumbs

Breadcrumbs are a navigation widget that shows the hierarchy that leads to the current page.

BootstrapVue has the b-breadcrumb component to let us add breadcrumbs.

For instance, we can write:

<template>
  <div id="app">
    <b-breadcrumb :items="items"></b-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      items: [
        {
          text: "Home",
          href: "#"
        },
        {
          text: "Settings",
          href: "#"
        },
        {
          text: "Library",
          active: true
        }
      ]
    };
  }
};
</script>

We have an items with the text and the URL to go to.

We set that as the value of the items prop and they’ll be displayed in the same order.

We can use the b-breadcrumb-item component to place items manually.

For instance, we can write:

<template>
  <div id="app">
    <b-breadcrumb>
      <b-breadcrumb-item href="#foo">Foo</b-breadcrumb-item>
    </b-breadcrumb>
  </div>
</template>

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

Then we put the breadcrumbs item inside the breadcrumb manually.

href is the URL for the link.

Buttons

BootstrapVue comes with many varieties of buttons.

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

We can write:

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

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

Then we see a button displayed in a gray color.

We can use the variant prop to change the styles.

For instance, we can write:

<template>
  <div id="app">
    <b-button variant="success">Button</b-button>
  </div>
</template>

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

Then we get a green button displayed.

Size

We can change the size of a button with the size prop.

For example, we can write:

<template>
  <div id="app">
    <b-button size="sm">Button</b-button>
  </div>
</template>

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

sm means small.

Outline

We can also make buttons with a white fill and a colored outline.

To do that, we prefix the variant value with outline- .

For instance, we can write:

<template>
  <div id="app">
    <b-button variant="outline-primary">Primary</b-button>
  </div>
</template>

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

Then we get a button with the blue outline with blue text.

Rounded Button

We can add the pill prop to make buttons more rounded.

For instance, we can write:

<template>
  <div id="app">
    <b-button pill>Button</b-button>
  </div>
</template>

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

We’ll get a button that’s more round than the default style.

Disabled Button

Also, we can set a button to be disabled with the disabled prop.

For instance, we can write:

<template>
  <div id="app">
    <b-button disabled>Button</b-button>
  </div>
</template>

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

Then we get a button that we can’t click on.

Pressed State

We can set the pressed state of the button to make them look pressed or not.

To make them look pressed, we can write:

<template>
  <div id="app">
    <b-button :pressed="true">Button</b-button>
  </div>
</template>

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

We used the pressed prop and set it to true to make it look pressed.

Likewise, we can set it to false to make them not pressed.

Conclusion

With BootstrapVue, we can add badges and add tag text.

Also, we can add breadcrumbs with a breadcrumb and breadcrumb item components.

BootstrapVue also comes with many varieties of buttons.

Categories
BootstrapVue

Getting Started with BootstrapVue

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 get started with BootstrapVue.

Getting Started

To get started we need Vue 2.6 or later. Bootstrap 4.3.1 is required. Popper.js is required for dropdowns, tooltips, and popovers. jQuery isn’t required.

Installation

To install it, we install the required dependencies by running:

npm install vue bootstrap-vue bootstrap

with NPM or:

yarn add vue bootstrap-vue bootstrap

with Yarn.

Then we can register the plugin by writing:

import Vue from "vue";
import App from "./App.vue";
import { BootstrapVue } from "bootstrap-vue";

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

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

in main.js.

We called Vue.use on BootstrapVue to register the plugin globally.

Next, we add the CSS that’s required by BootstrapVue:

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Then we get:

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

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

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

Picking Individual Components

If we only want to use individual components, then we can import them and add them to a component ourselves.

For instance, we can write:

<template>
  <div id="app">

</div>
</template>

<script>
import { BModal } from 'bootstrap-vue'

export default {
  name: "App",
  components: {
    'b-modal': BModal
  }
};
</script>

Then we just imported the BModal component into our component.

We can also register a component globally.

For instance, we can write:

import Vue from "vue";
import App from "./App.vue";
import { BModal } from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";

Vue.component("b-modal", BModal);
Vue.config.productionTip = false;

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

Then we only added the BModal component globally.

Alerts

Now, we can use the components in our app.

We can add an alert component by using the b-alert component.

We can write:

<template>
  <div id="app">
    <b-alert show>Alert</b-alert>
  </div>
</template>

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

Then we get alert box in our component.

It also comes with various variants.

We can write:

<b-alert variant="success" show>Success</b-alert>

to create a success alert, which is green.

To add a dismissible alert, we can write:

<template>
  <div id="app">
    <b-alert v-model="showAlert" variant="danger" dismissible>Dismissible Alert!</b-alert>
  </div>
</template>

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

We added the v-model directive to use the showAlert state to toggle the modal off.

We set showAlert to true initially so that we can see it.

The dismissible directive makes it dismissible. This means we see an ‘x’ that we can click to dismiss it.

We can create an alert that can be dismissed after some time by writing:

<template>
  <div id="app">
    <b-alert
      :show="dismissCountDown"
      dismissible
      variant="warning"
      @dismissed="dismissCountDown = 0"
      @dismiss-count-down="countDownChanged"
    >
      <p>alert will dismiss {{ dismissCountDown }} seconds...</p>
      <b-progress variant="warning" :max="dismissSecs" :value="dismissCountDown" height="4px"></b-progress>
    </b-alert>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      dismissCountDown: 15,
      dismissSecs: 15
    };
  },
  methods: {
    countDownChanged(e){
      this.dismissCountDown = e;
    }
  }
};
</script>

We added the dismissCountDown state which tracks the time before the alert is dismissed.

Variant 'warning' shows us a yellow alert.

The dismissed handler takes a condition of when the alert will be dismissed.

In this case, when dismissCountDown is 0.

We have the countDownChanged which takes an event parameter with the number of seconds left to set it to dismissCountDown to update the progress bar.

Avatar

We can add an avatar by using the b-avatar component.

We can use it by writing:

<template>
  <div id="app">
    <b-avatar></b-avatar>
  </div>
</template>

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

Then we get a generic avatar.

To add initials to the avatar, we can write:

<template>
  <div id="app">
    <b-avatar variant="primary" text="AB"></b-avatar>
  </div>
</template>

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

We set the variant to primary to show the blue color.

text has the initials that we want to display.

Also, we can set an image by writing:

<template>
  <div id="app">
    <b-avatar variant="info" src="https://placekitten.com/g/100/100"></b-avatar>
  </div>
</template>

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

to add an image to the avatar with the src prop.

Conclusion

We can add alerts and avatars easily with BootstrapVue.

It’s also easy to install.

Categories
BootstrapVue

BootstrapVue — Form Basics

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 basic forms with BootstrapVue.

Form

BootstrapVue has plenty of components for creating forms our way.

To create a form we can use the b-form , b-form-group , and b-form-input components.

For instance, we can create a simple form by writing:

<template>
  <div id="app">
    <b-form @submit="onSubmit" @reset="onReset">
      <b-form-group id="input-group-1" label="name" label-for="input-1" description="your name">
        <b-form-input id="input-1" v-model="form.name" type="text" required placeholder="name"></b-form-input>
      </b-form-group>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App",
  data() {
    return {
      form: {}
    };
  },
  methods: {
    onSubmit() {},
    onReset() {}
  }
};
</script>

We created a simple form with an input box by using those components.

The b-form-input is a text input box that renders the input element.

v-model binds the inputted value to the model.

It also takes the @submit and @reset handlers to allow us to handle submit and reset events triggered on a form.

Inline Form

We can make a form for input inline by adding the inline prop to b-form .

For instance, we can write:

<template>
  <div id="app">
    <b-form inline>
      <label class="sr-only" for="name">Name</label>
      <b-input id="name" placeholder="name"></b-input>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App"
};
</script>

We just add the inline prop to b-form to make our form inline instead of a block element.

We can add multiple form input elements side by side by writing:

`<template>
  <div id="app">
    <b-form inline>
      <label class="sr-only" for="name">name</label>
      <b-input class="mb-2 mr-sm-2 mb-sm-0" id="name" placeholder="name"></b-input>
`
<label class="sr-only" for="email">email</label>
      <b-input-group prepend="@" class="mb-2 mr-sm-2 mb-sm-0">
        <b-input id="email" placeholder="email"></b-input>
      </b-input-group>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App"
};
</script>

Now we have 2 input boxes side by side.

They’re responsive, so their size will adjust according to the viewport width.

Form Text Helper

We can add a b-form-text element to add an explanation for our input field.

For example, we can write:

<template>
  <div id="app">
    <b-form inline>
      <label for="name">name</label>
      <b-input type="text" id="name"></b-input>
      <b-form-text id="name">
        10 or more characters
      </b-form-text>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App"
};
</script>

We have the b-form-text to add a description below the input box.

Feedback Helpers

BootstrapVue provides us with the b-form-invalid-feedback component to display form validation errors.

It also has the b-form-valid-feedback to display a message if the form input value is valid.

For instance, we can display form validation errors by writing:

<template>
  <div id="app">
    <b-form inline>
      <label for="feedback-user">name</label>
      <b-input v-model="name" :state="validation" id="name"></b-input>
      <b-form-invalid-feedback :state="validation">please enter a name</b-form-invalid-feedback>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App",
  data() {
    return {
      name: ""
    };
  },
  computed: {
    validation() {
      return this.name.length > 0;
    }
  }
};
</script>

We have a b-input component with the b-form-invalid-feedback below it.

The state prop takes an expression that indicates the form value validation state.

The b-form-invalid-feedback also takes the same state prop.

Now if we type something, we see a green border and a checkmark.

If we typed nothing, we see a red border and an exclamation mark with the form validation message.

We just have the validation computed property to return the input validation state.

To add a message to show something when we input something valid, we can write:

<template>
  <div id="app">
    <b-form inline>
      <label for="feedback-user">name</label>
      <b-input v-model="name" :state="validation" id="name"></b-input>
      <b-form-invalid-feedback :state="validation">please enter a name</b-form-invalid-feedback>
      <b-form-valid-feedback :state="validation">good job</b-form-valid-feedback>
    </b-form>
  </div>
</template>
`
<script>
export default {
  name: "App",
  data() {
    return {
      name: ""
    };
  },
  computed: {
    validation() {
      return this.name.length > 0;
    }
  }
};
</script>

We added:

<b-form-valid-feedback :state="validation">good job</b-form-valid-feedback>

to display a message when we entered something to the input box.

Conclusion

BootstrapVue provides us with lots of form components to add input boxes and validation indicators.

It can bind input values to model fields automatically.