Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — Dropdown and Rich Text Editor

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Dropdown

We can add a dropdown into our Vue 3 app with PrimeVue’s Dropdown component.

For example, we can write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Dropdown from 'primevue/dropdown';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Dropdown", Dropdown);
app.mount("#app");

App.vue

<template>
  <div>
    <Dropdown
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
      placeholder="Select a City"
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: "",
      cities: [
        { name: "New York", code: "NY" },
        { name: "Miami", code: "MI" },
        { name: "London", code: "LDN" },
      ],
    };
  },
  methods: {},
};
</script>

name is displayed to the user as set by the optionLabel prop.

And code is the value of the value attribute.

placeholder is displayed as the dropdown placeholder.

We bind the value to the selectedCity reactive property with v-model .

The filter prop will render an input box that lets us filter the choices:

<template>
  <div>
    <Dropdown
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
      placeholder="Select a City"
      filter
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: "",
      cities: [
        { name: "New York", code: "NY" },
        { name: "Miami", code: "MI" },
        { name: "London", code: "LDN" },
      ],
    };
  },
  methods: {},
};
</script>

We can customize how each option is displayed with the option slot.

And we can customize how the selected value is displayed with the value slot:

<template>
  <div>
    <Dropdown
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
      placeholder="Select a City"
      showClear
    >
      <template #value="slotProps">
        <div class="p-dropdown-code">
          <span v-if="slotProps.value">{{ slotProps.value.name }}</span>
          <span v-else>
            {{ slotProps.placeholder }}
          </span>
        </div>
      </template>
      <template #option="slotProps">
        <div class="p-dropdown-name">
          <span v-if="slotProps.option">{{ slotProps.option.name }}</span>
          <span v-else>
            {{ slotProps.placeholder }}
          </span>
        </div>
      </template>
    </Dropdown>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: "",
      cities: [
        { name: "New York", code: "NY" },
        { name: "Miami", code: "MI" },
        { name: "London", code: "LDN" },
      ],
    };
  },
  methods: {},
};
</script>

Rich Text Editor

We can add a rich text editor into our Vue 3 app with PrimeVue’s Editor component.

It’s based on the Quill rich text editor.

To add it, we run npm i quill to install the Quill editor package, then we write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Editor from 'primevue/editor';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Editor", Editor);
app.mount("#app");

App.vue

<template>
  <div>
    <Editor v-model="value" editorStyle="height: 320px" />
  </div>
</template>

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

We set the styles for the text editor with the editorStyle prop.

The value we enter into the editor is bound to the value reactive property with v-model .

Conclusion

We can add a dropdown and rich text editor easily into our Vue 3 app with PrimeVue.

Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — Checkbox, Chips Input, and Color Picker

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Checkbox

We can add a checkbox by writing:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Checkbox from 'primevue/checkbox';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Checkbox", Checkbox);
app.mount("#app");

App.vue

<template>
  <div class="card">
    <div class="p-field-checkbox">
      <Checkbox name="city1" value="Miami" v-model="cities" />
      <label> Miami</label>
    </div>
    <div class="p-field-checkbox">
      <Checkbox name="city" value="Los Angeles" v-model="cities" />
      <label>Los Angeles</label>
    </div>
    <div class="p-field-checkbox">
      <Checkbox name="city" value="New York" v-model="cities" />
      <label>New York</label>
    </div>
    <div class="p-field-checkbox">
      <Checkbox name="city" value="San Francisco" v-model="cities" />
      <label>San Francisco</label>
    </div>
  </div>
</template>

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

We add the primevue.min.css to add the core CSS.

theme.css has the theme CSS.

We also need the primeicons.css to add the checkbox icon.

Then we register the Checkbox component to let us use the checkbox.

In App.vue , we add the Checkbox component.

We bind each one to the same reactive property to let us add multiple checked values.

Chips Input

We can add a chips input into our Vue 3 app with PrimeVue’s Chips component.

For example, we can write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Chips from 'primevue/chips';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Chips", Chips);
app.mount("#app");

App.vue

<template>
  <div>
    <Chips v-model="value">
      <template #chip="slotProps">
        <div>
          <span>{{ slotProps.value }} </span>
          <i class="pi pi-user-plus" style="font-size: 14px"></i>
        </div>
      </template>
    </Chips>
  </div>
</template>

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

We add the Chips component and bind it to the value reactive property.

We optionally populate the chip slot to get the value that we entered with the slotProps.value property and display the content we want in the chip.

Color Picker

The ColorPicker component lets us add a color picker component.

To use it, we write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import ColorPicker from 'primevue/colorpicker';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("ColorPicker", ColorPicker);
app.mount("#app");

App.vue

<template>
  <div>
    <ColorPicker v-model="color" />
  </div>
</template>

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

We register the ColorPicker component and use it in App.vue .

We bind the picker color’s value with v-model to the color reactive property.

The inline property lets us display the color picker preview inline:

<template>
  <div>
    <ColorPicker v-model="color" inline  />
  </div>
</template>

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

The color picker will display on the page instead of in a popup.

Conclusion

We can add a checkbox, chips input, and color picker into our Vue 3 app with the PrimeVue framework.

Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — Month Picker and Cascade Select

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Month Picker

We can add a month picker by writing:

<template>
  <div>
    <Calendar
      v-model="value"
      view="month"
      dateFormat="mm/yy"
      yearNavigator
      yearRange="2000:2025"
    />

<p>{{ value }}</p>
  </div>
</template>

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

<style scoped>
.special-day {
  font-style: italic;
}
</style>

We add the yearNavigator prop to let us add a dropdown to let us select the year.

yearRange sets the years we can select.

dateFormat formats the date to be displayed in the input.

view sets the view mode for the date picker.

The touchUI prop lets us enable the date picker to function with touch:

<template>
  <div>
    <Calendar v-model="value" touchUI />
    <p>{{ value }}</p>
  </div>
</template>

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

<style scoped>
.special-day {
  font-style: italic;
}
</style>

Cascade Select

We can use the CascadeSelect component to display a nested structure of options.

To add it, we write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import CascadeSelect from 'primevue/cascadeselect';
import 'primevue/resources/themes/bootstrap4-light-blue/theme.css'
import 'primeicons/primeicons.css'

const app = createApp(App);
app.use(PrimeVue);
app.component("CascadeSelect", CascadeSelect);
app.mount("#app");

App.vue

<template>
  <div>
    <CascadeSelect
      v-model="selectedCity"
      :options="countries"
      optionLabel="cname"
      optionGroupLabel="name"
      :optionGroupChildren="['states', 'cities']"
      style="minwidth: 14rem"
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      countries: [
        {
          name: "Canada",
          code: "CA",
          states: [
            {
              name: "Quebec",
              cities: [
                { cname: "Montreal", code: "C-MO" },
                { cname: "Quebec City", code: "C-QU" },
              ],
            },
            {
              name: "Ontario",
              cities: [
                { cname: "Ottawa", code: "C-OT" },
                { cname: "Toronto", code: "C-TO" },
              ],
            },
          ],
        },
        {
          name: "United States",
          code: "US",
          states: [
            {
              name: "California",
              cities: [
                { cname: "Los Angeles", code: "US-LA" },
                { cname: "San Francisco", code: "US-SF" },
              ],
            },
            {
              name: "Florida",
              cities: [
                { cname: "Jacksonville", code: "US-JA" },
                { cname: "Miami", code: "US-MI" },
              ],
            },
            {
              name: "Texas",
              cities: [
                { cname: "Austin", code: "US-AU" },
                { cname: "Dallas", code: "US-DA" },
              ],
            },
          ],
        },
      ],
    };
  },
  methods: {},
};
</script>

We set the options prop to countries , which is an array of objects.

optionLabel has the property name for the option labels.

optionGroupLabel has the property name for the dropdown labels.

We can customize how the options are displayed with the option slot:

<template>
  <div>
    <CascadeSelect
      v-model="selectedCity"
      :options="countries"
      optionLabel="cname"
      optionGroupLabel="name"
      :optionGroupChildren="['states', 'cities']"
      style="minwidth: 14rem"
    >
      <template #option="slotProps">
        <div>
          <i class="pi pi-compass p-mr-3" v-if="slotProps.option.cities"></i>
          <i class="pi pi-map-marker p-mr-3" v-if="slotProps.option.cname"></i>
          <span>{{ slotProps.option.cname || slotProps.option.name }}</span>
        </div>
      </template>
    </CascadeSelect>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      countries: [
        {
          name: "Canada",
          code: "CA",
          states: [
            {
              name: "Quebec",
              cities: [
                { cname: "Montreal", code: "C-MO" },
                { cname: "Quebec City", code: "C-QU" },
              ],
            },
            {
              name: "Ontario",
              cities: [
                { cname: "Ottawa", code: "C-OT" },
                { cname: "Toronto", code: "C-TO" },
              ],
            },
          ],
        },
        {
          name: "United States",
          code: "US",
          states: [
            {
              name: "California",
              cities: [
                { cname: "Los Angeles", code: "US-LA" },
                { cname: "San Francisco", code: "US-SF" },
              ],
            },
            {
              name: "Florida",
              cities: [
                { cname: "Jacksonville", code: "US-JA" },
                { cname: "Miami", code: "US-MI" },
              ],
            },
            {
              name: "Texas",
              cities: [
                { cname: "Austin", code: "US-AU" },
                { cname: "Dallas", code: "US-DA" },
              ],
            },
          ],
        },
      ],
    };
  },
  methods: {},
};
</script>

slotProps.option has the option object.

Conclusion

We can add dropdowns that work with nested entries with the cascade select component into our Vue 3 app.

Also, we can add a month picker into our Vue 3 app with PrimeVue.

Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — Date and Time Picker

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Date and Time Picker

We can add a date and time picker with the showTime prop:

<template>
  <div>
    <Calendar v-model="value" showTime />
    <p>{{ value }}</p>
  </div>
</template>

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

showTime adds a time picker with the date picker.

We can change the time format with the hourFormat prop:

<template>
  <div>
    <Calendar v-model="value" showTime hourFormat="12" />
    <p>{{ value }}</p>
  </div>
</template>

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

We can change it to 12 or 24 hour format.

We can restrict the dates that can be selected with the minDate and maxDate props:

<template>
  <div>
    <Calendar v-model="value" :minDate="minDateValue" :maxDate="maxDateValue" />
    <p>{{ value }}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: null,
      minDateValue: new Date(2020, 11, 1),
      maxDateValue: new Date(2020, 11, 10),
    };
  },
  methods: {},
};
</script>

We can also disable dates from selection with the disableDates prop:

<template>
  <div>
    <Calendar v-model="value" :disabledDates="invalidDates" />
    <p>{{ value }}</p>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: null,
      invalidDates: [new Date(2020, 11, 1), new Date(2020, 11, 10)],
    };
  },
  methods: {},
};
</script>

We can also disable dates by the date of the week:

<template>
  <div>
    <Calendar v-model="value" :disabledDays="[0, 6]" />
    <p>{{ value }}</p>
  </div>
</template>

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

0 is Sunday and 6 is Saturday.

Also, we can add a button to the bottom of the date picker to let users select dates with the showButtonBar prop:

<template>
  <div>
    <Calendar v-model="value" showButtonBar />
    <p>{{ value }}</p>
  </div>
</template>

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

We can also show multiple months in the date picker with the numberOfMonths prop:

<template>
  <div>
    <Calendar v-model="value" :numberOfMonths="3" />
    <p>{{ value }}</p>
  </div>
</template>

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

We can our own header and footer content by writing:

<template>
  <div>
    <Calendar v-model="value">
      <template #header>Header</template>
      <template #footer>Footer</template>
    </Calendar>
    <p>{{ value }}</p>
  </div>
</template>

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

We can customize how dates are displayed with the default slot:

<template>
  <div>
    <Calendar v-model="value">
      <template #date="slotProps">
        <strong
          v-if="slotProps.date.day > 10 && slotProps.date.day < 15"
          class="special-day"
          >{{ slotProps.date.day }}</strong
        >
        <template v-else>{{ slotProps.date.day }}</template>
      </template>
    </Calendar>
    <p>{{ value }}</p>
  </div>
</template>

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

<style scoped>
.special-day {
  font-style: italic;
}
</style>

slotProps.date.day gets the day value.

Conclusion

We can add a date picker with various customizations into our Vue 3 app with the PrimeVue framework.

Categories
PrimeVue

Getting Started with Vue 3 Development with the PrimeVue Framework

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Getting Started

We install the required packages by running:

npm install primevue@^3.1.1 --save
npm install primeicons --save

This will install the library with all the components and the icons.

Next, we add the PrimeVue plugin into our app:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import InputText from "primevue/inputtext";
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/bootstrap4-light-blue/theme.css'
import 'primeicons/primeicons.css'

const app = createApp(App);
app.use(PrimeVue);
app.component("InputText", InputText);
app.mount("#app");

App.vue

<template>
  <div>
    <InputText type="text" v-model="text" />
    <p>{{text}}</p>
  </div>
</template>

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

In main.js m we call app.use to add the plugin.

Then we call app.component to register the InputText component.

import ‘primevue/resources/primevue.min.css’ imports the core CSS.

import ‘primevue/resources/themes/bootstrap4-light-blue/theme.css’ is the CSS for the theme.

import ‘primeicons/primeicons.css’ add the icons.

We can also include the script tag for the individual components:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>calendar demo</title>
    <link
      href="https://unpkg.com/primevue/resources/themes/saga-blue/theme.css "
      rel="stylesheet"
    />
    <link
      href="https://unpkg.com/primevue/resources/primevue.min.css "
      rel="stylesheet"
    />
    <link
      href="https://unpkg.com/primeicons/primeicons.css "
      rel="stylesheet"
    />
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://unpkg.com/primevue@3.1.1/components/inputtext/inputtext.umd.min.js"></script>
  </head>
  <body>
    <div id="app">
      <p-inputtext v-model="text"></p-inputtext>
      <p>{{text}}</p>
    </div>

    <script>
      Vue.createApp({
        data() {
          return {
            text: ""
          };
        },
        components: {
          "p-inputtext": inputtext
        }
      }).mount("#app");
    </script>
  </body>
</html>

We add the script tags for Vue 3 and PrimeVue’s inputtext component.

Then we can use it in our code.

PrimeFlex

We can install the primeflex package to add CSS to let us create layouts easily without writing everything from scratch.

To install it, we run:

npm install primeflex --save

Then we cal add the CSS by writing:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import InputText from "primevue/inputtext";
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("InputText", InputText);
app.mount("#app");

Now we can use the classes by writing:

App.vue

<template>
  <div class="card">
    <InputText type="text" v-model="text" class="p-ml-2 p-d-inline" />
    <p>{{ text }}</p>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      text: "",
    };
  },
};
</script>

p-ml-2 adds some margin on the left side.

p-d-inline makes the input display inline.

We can add shadows with the p-shadow-{x} class, where x can be 1 to 24.

p-d-flex lets us create a flex layout.

p-d-inline-flex lets us create an inline flex layout.

p-flex-{direction} lets us set the flex-direction. Where direction can be one of:

  • row (default)
  • row-reverse
  • column
  • column-reverse

The order of items inside a flex container can be changed with the p-order-{value} classes.

We can also add layouts that change according to breakpoints:

<template>
  <div class="p-d-flex">
    <div class="p-mr-2 p-order-3 p-order-md-2">Item 1</div>
    <div class="p-mr-2 p-order-1 p-order-md-3">Item 2</div>
    <div class="p-mr-2 p-order-2 p-order-md-1">Item 3</div>
  </div>
</template>

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

We change the order depending on whether the breakpoint is md and up or otherwise.

If it’s md or higher, then we get:

Item 3 Item 1 Item 2

Otherwise, we get:

Item 2 Item 3 Item 1

Conclusion

PrimeVue is one of the earliest UI frameworks to be compatible with Vue 3.