Categories
Ant Design Vue

Ant Design Vue — Cascade Selection Box and Checkbox

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Cascade Selection Box Default Value

We can set the default value of a cascade selection box with the default-value prop:

<template>
  <a-cascader :options="options" @change="onChange" :default-value="['fruit', 'apple']"/>
</template>
<script>
export default {
  data() {
    return {
      options: [
        {
          value: "fruit",
          label: "Fruit",
          children: [
            {
              value: "apple",
              label: "Apple"
            }
          ]
        },
        {
          value: "drink",
          label: "Drink",
          disabled: true,
          children: [
            {
              value: "coffee",
              label: "Coffee"
            }
          ]
        }
      ]
    };
  },
  methods: {
    onChange(value) {
      console.log(value);
    }
  }
};
</script>

We just pass in an array of values from top-level down to set it.

Also, we can add an icon on the right of the input by populating the suffixIcon prop:

<template>
  <a-cascader :options="options" @change="onChange">
    <a-icon slot="suffixIcon" type="smile" class="test"/>
  </a-cascader>
</template>
<script>
export default {
  data() {
    return {
      options: [
        {
          value: "fruit",
          label: "Fruit",
          children: [
            {
              value: "apple",
              label: "Apple"
            }
          ]
        },
        {
          value: "drink",
          label: "Drink",
          disabled: true,
          children: [
            {
              value: "coffee",
              label: "Coffee"
            }
          ]
        }
      ]
    };
  },
  methods: {
    onChange(value) {
      console.log(value);
    }
  }
};
</script>

Cascade Selection Field Names

We can set the field-names prop to set the property names of the value and label:

<template>
  <a-cascader
    :options="options"
    @change="onChange"
    :field-names="{ label: 'name', value: 'code', children: 'items' }"
  ></a-cascader>
</template>
<script>
export default {
  data() {
    return {
      options: [
        {
          code: "fruit",
          name: "Fruit",
          items: [
            {
              code: "apple",
              name: "Apple"
            }
          ]
        },
        {
          code: "drink",
          name: "Drink",
          children: [
            {
              code: "coffee",
              name: "Coffee"
            }
          ]
        }
      ]
    };
  },
  methods: {
    onChange(value) {
      console.log(value);
    }
  }
};
</script>

The object has the label , value , and children as the keys.

And we have the property names we want to set them to as the values.

Checkbox

We can add a checkbox to with the a-checkbox component:

<template>
  <a-checkbox @change="onChange">Checkbox</a-checkbox>
</template>
<script>
export default {
  methods: {
    onChange(e) {
      console.log(e.target.checked);
    }
  }
};
</script>

The change event is emitted when we check or uncheck the checkbox.

e.target.checked has the checked value.

Checkbox Group

We can add the a-checkbox-group component to add a group of checkboxes:

<template>
  <div>
    <div>
      <a-checkbox
        :indeterminate="indeterminate"
        :checked="checkAll"
        @change="onCheckAllChange"
      >Check all</a-checkbox>
    </div>
    <br>
    <a-checkbox-group v-model="checkedList" :options="plainOptions" @change="onChange"/>
  </div>
</template>
<script>
const plainOptions = ["Apple", "Pear", "Orange"];
const defaultCheckedList = ["Apple", "Orange"];
export default {
  data() {
    return {
      checkedList: defaultCheckedList,
      indeterminate: true,
      checkAll: false,
      plainOptions
    };
  },
  methods: {
    onChange(checkedList) {
      this.indeterminate =
        !!checkedList.length && checkedList.length < plainOptions.length;
      this.checkAll = checkedList.length === plainOptions.length;
    },
    onCheckAllChange(e) {
      Object.assign(this, {
        checkedList: e.target.checked ? plainOptions : [],
        indeterminate: false,
        checkAll: e.target.checked
      });
    }
  }
};
</script>

The indeterminate prop lets us set whether the checkbox is indeterminate.

a-checkbox-group has the checkbox group.

options has an array of options.

Conclusion

We can add the cascade selection box with various options.

Also, we can add the checkbox group.

Categories
Ant Design Vue

Ant Design Vue — Autocomplete

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Autocomplete Input

We can add an autocomplete input withn the a-auto-complete component:

<template>
  <a-auto-complete
    v-model="value"
    :data-source="dataSource"
    style="width: 200px"
    placeholder="input here"
    @select="onSelect"
    @search="onSearch"
    @change="onChange"
  />
</template>
<script>
export default {
  data() {
    return {
      value: "",
      dataSource: ["apple", "orange", "grape"]
    };
  },
  watch: {
    value(val) {
      console.log("value", val);
    }
  },
  methods: {
    onSearch(searchText) {
      this.dataSource = this.dataSource.filter(d => d.includes(searchText));
    },
    onSelect(value) {
      console.log("onSelect", value);
    },
    onChange(value) {
      console.log("onChange", value);
    }
  }
};
</script>

We add the v-model directive to bind the inputted value to a reactive property.

It also emits the select , search , and change events.

select is emitted when we select an item.

search is emitted when we are searching.

change is emitted when we change the input.

data-source has an array with the data source.

Also, we can display the autocomplete item by populate the dataSource slot:

<template>
  <div style="width: 250px">
    <a-auto-complete
      class="certain-category-search"
      dropdown-class-name="certain-category-search-dropdown"
      :dropdown-match-select-width="false"
      :dropdown-style="{ width: '300px' }"
      size="large"
      style="width: 100%"
      placeholder="input here"
      option-label-prop="value"
    >
      <template slot="dataSource">
        <a-select-opt-group v-for="group in dataSource" :key="group.title">
          <span slot="label">
            {{ group.title }}
            <a
              style="float: right"
              href="https://www.google.com/search?q=foo"
              target="_blank"
              rel="noopener noreferrer"
            >more</a>
          </span>
          <a-select-option v-for="opt in group.children" :key="opt.title" :value="opt.title">
            {{ opt.title }}
            <span class="certain-search-item-count">{{ opt.count }} people</span>
          </a-select-option>
        </a-select-opt-group>
        <a-select-option key="all" disabled class="show-all">
          <a
            href="https://www.google.com/search?q=baz"
            target="_blank"
            rel="noopener noreferrer"
          >View all results</a>
        </a-select-option>
      </template>
      <a-input>
        <a-icon slot="suffix" type="search" class="certain-category-icon"/>
      </a-input>
    </a-auto-complete>
  </div>
</template>
<script>
const dataSource = [
  {
    title: "Libraries",
    children: [
      {
        title: "foo",
        count: 10000
      },
      {
        title: "bar",
        count: 10600
      }
    ]
  },
  {
    title: "Articles",
    children: [
      {
        title: "baz",
        count: 100000
      }
    ]
  }
];
export default {
  data() {
    return {
      dataSource
    };
  }
};
</script>

We loop through the items in the dataSource reactive property to display the items.

Also, we set the class attribute to set the classes for our auto-complete.

The input can be customized. We just add the input we want to use inside the a-auto-complete component inside:

<template>
  <a-auto-complete
    :data-source="dataSource"
    style="width: 200px"
    @search="handleSearch"
    @select="onSelect"
  >
    <a-textarea
      placeholder="input here"
      class="custom"
      style="height: 50px"
      @keypress="handleKeyPress"
    />
  </a-auto-complete>
</template>
<script>
export default {
  data() {
    return {
      dataSource: ["apple", "orange", "grape"]
    };
  },
  methods: {
    onSelect(value) {
      console.log("onSelect", value);
    },
    handleSearch(value) {
      this.dataSource = this.dataSource.filter(d => d.includes(value));
    },
    handleKeyPress(ev) {
      console.log("handleKeyPress", ev);
    }
  }
};
</script>

We put it in the a-textarea inside the a-auto-complete to use a text area instead of a text input.

Conclusion

We can add autocomplete inputs our Vue app with Ant Design Vue.

Categories
Ant Design Vue

Getting Started with Ant Design Vue

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Getting Started

We can install AntD Vue by running:

npm i --save ant-design-vue

Then we can register the plugin by writing:

import Vue from "vue";
import App from "./App.vue";
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";
Vue.use(Antd);
Vue.config.productionTip = false;

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

This will register the components and load the CSS.

Buttons

We can add buttons with AntD Vue by using the a-button component”

<template>
  <div id="app">
    <a-button type="primary">Primary</a-button>
  </div>
</template>

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

The type is the color type.

We can add a button group by writing:

<template>
  <div id="app">
    <a-button-group>
      <a-button>L</a-button>
      <a-button disabled>M</a-button>
      <a-button disabled>R</a-button>
    </a-button-group>
  </div>
</template>

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

We can add an icon with the icon prop:

<template>
  <div id="app">
    <a-button icon="search">Search</a-button>
  </div>
</template>

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

To make a button a block-level element, we can add the block prop:

<template>
  <div id="app">
    <a-button type="primary" block>Primary</a-button>
  </div>
</template>

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

Also, we can make it show a loading indicator with the loading prop:

<template>
  <div id="app">
    <a-button type="primary" loading>Loading</a-button>
  </div>
</template>

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

The size can be changed with the size prop:

<template>
  <div id="app">
    <a-button type="primary" size="large">Primary</a-button>
  </div>
</template>

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

Icon

AntD Vue comes with various icons.

For example, we can add one by writing:

<template>
  <div id="app">
    <a-icon type="step-backward"/>
  </div>
</template>

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

We use the a-icon component to add the icon.

There are many more we can add. The list is at https://www.antdv.com/components/icon/.

Grid

Ant Design Vue comes with its own layout grid. We can add it with the a-row and a-col components:

<template>
  <div id="app">
    <a-row>
      <a-col :span="12">col-12</a-col>
      <a-col :span="12">col-12</a-col>
    </a-row>
  </div>
</template>

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

We set the span prop to a value from 0 to 12 to make the layout.

It supports flexbox, and we can set the options with the justify and align props:

<template>
  <div id="app">
    <a-row type="flex" justify="center" align="top">
      <a-col :span="4">
        <p class="height-100">col-4</p>
      </a-col>
      <a-col :span="4">
        <p class="height-50">col-4</p>
      </a-col>
      <a-col :span="4">
        <p class="height-120">col-4</p>
      </a-col>
      <a-col :span="4">
        <p class="height-80">col-4</p>
      </a-col>
    </a-row>
  </div>
</template>

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

Now they are all centered.

Conclusion

We can use Ant Design Vue to create a good looking UI in our Vue app.

Categories
Ant Design Vue

Ant Design Vue — Submenus

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Submenus

We can add a submenu with the a-sub-menu component:

<template>
  <div id="app">
    <a-menu
      style="width: 256px"
      :default-selected-keys="['1']"
      :open-keys.sync="openKeys"
      mode="inline"
      @click="handleClick"
    >
      <a-sub-menu key="sub1" @titleClick="titleClick">
        <span slot="title">
          <a-icon type="mail"/>
          <span>Navigation One</span>
        </span>
        <a-menu-item-group key="g1">
          <template slot="title">
            <a-icon type="qq"/>
            <span>Item 1</span>
          </template>
          <a-menu-item key="1">Option 1</a-menu-item>
          <a-menu-item key="2">Option 2</a-menu-item>
        </a-menu-item-group>
        <a-menu-item-group key="g2" title="Item 2">
          <a-menu-item key="3">Option 3</a-menu-item>
          <a-menu-item key="4">Option 4</a-menu-item>
        </a-menu-item-group>
      </a-sub-menu>
      <a-sub-menu key="sub2" [@titleClick](http://twitter.com/titleClick "Twitter profile for @titleClick")="titleClick">
        <span slot="title">
          <a-icon type="appstore"/>
          <span>Navigation Two</span>
        </span>
        <a-menu-item key="5">Option 5</a-menu-item>
        <a-menu-item key="6">Option 6</a-menu-item>
        <a-sub-menu key="sub3" title="Submenu">
          <a-menu-item key="7">Option 7</a-menu-item>
          <a-menu-item key="8">Option 8</a-menu-item>
        </a-sub-menu>
      </a-sub-menu>
      <a-sub-menu key="sub4">
        <span slot="title">
          <a-icon type="setting"/>
          <span>Navigation Three</span>
        </span>
        <a-menu-item key="9">Option 9</a-menu-item>
        <a-menu-item key="10">Option 10</a-menu-item>
      </a-sub-menu>
    </a-menu>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      current: ["mail"],
      openKeys: ["sub1"]
    };
  },
  watch: {
    openKeys(val) {
      console.log("openKeys", val);
    }
  },
  methods: {
    handleClick(e) {
      console.log("click", e);
    },
    titleClick(e) {
      console.log("titleClick", e);
    }
  }
};
</script>

We can make the menu toggleable by adding a button:

<template>
  <div id="app">
    <a-button type="primary" style="margin-bottom: 16px" @click="toggleCollapsed">
      <a-icon :type="collapsed ? 'menu-unfold' : 'menu-fold'"/>
    </a-button>
    <a-menu
      :default-selected-keys="['1']"
      :default-open-keys="['sub1']"
      mode="inline"
      theme="dark"
      :inline-collapsed="collapsed"
    >
      <a-menu-item key="1">
        <a-icon type="pie-chart"/>
        <span>Option 1</span>
      </a-menu-item>
      <a-menu-item key="2">
        <a-icon type="desktop"/>
        <span>Option 2</span>
      </a-menu-item>
      <a-menu-item key="3">
        <a-icon type="inbox"/>
        <span>Option 3</span>
      </a-menu-item>
      <a-sub-menu key="sub1">
        <span slot="title">
          <a-icon type="mail"/>
          <span>Navigation One</span>
        </span>
        <a-menu-item key="5">Option 5</a-menu-item>
        <a-menu-item key="6">Option 6</a-menu-item>
      </a-sub-menu>
      <a-sub-menu key="sub2">
        <span slot="title">
          <a-icon type="appstore"/>
          <span>Navigation Two</span>
        </span>
        <a-menu-item key="7">Option 7</a-menu-item>
        <a-menu-item key="8">Option 8</a-menu-item>>
        <a-sub-menu key="sub3" title="Submenu">
          <a-menu-item key="9">Option 9</a-menu-item>
          <a-menu-item key="10">Option 10</a-menu-item>
        </a-sub-menu>
      </a-sub-menu>
    </a-menu>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      collapsed: false
    };
  },
  methods: {
    toggleCollapsed() {
      this.collapsed = !this.collapsed;
    }
  }
};
</script>

The inline-collapsed prop is used to set whether the menu is collapsed or not.

We can also make it only open one submenu only bu writing:

<template>
  <div id="app">
    <a-menu mode="inline" :open-keys="openKeys" style="width: 256px" @openChange="onOpenChange">
      <a-sub-menu key="sub1">
        <span slot="title">
          <a-icon type="mail"/>
          <span>Navigation One</span>
        </span>
        <a-menu-item key="1">Option 1</a-menu-item>
        <a-menu-item key="2">Option 2</a-menu-item>
      </a-sub-menu>
      <a-sub-menu key="sub2">
        <span slot="title">
          <a-icon type="appstore"/>
          <span>Navigation Two</span>
        </span>
        <a-menu-item key="3">Option 3</a-menu-item>
        <a-menu-item key="4">Option 4</a-menu-item>
        <a-sub-menu key="sub3" title="Submenu">
          <a-menu-item key="5">Option 5</a-menu-item>
          <a-menu-item key="6">Option 6</a-menu-item>
        </a-sub-menu>
      </a-sub-menu>
      <a-sub-menu key="sub4">
        <span slot="title">
          <a-icon type="setting"/>
          <span>Navigation Three</span>
        </span>
        <a-menu-item key="7">Option 7</a-menu-item>
        <a-menu-item key="8">Option 8</a-menu-item>
      </a-sub-menu>
    </a-menu>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      rootSubmenuKeys: ["sub1", "sub2", "sub4"],
      openKeys: ["sub1"]
    };
  },
  methods: {
    onOpenChange(openKeys) {
      const latestOpenKey = openKeys.find(
        key => this.openKeys.indexOf(key) === -1
      );
      if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
        this.openKeys = openKeys;
      } else {
        this.openKeys = latestOpenKey ? [latestOpenKey] : [];
      }
    }
  }
};
</script>

Set set the openKeys prop to the key prop value of the menu to open.

The openChange is emitted when we click on a submenu, so we can set the menu item to open there by getting the openKeys and setting it to the one that we clicked on.

Conclusion

We can add menus and submenus with Ant Design Vue.

Categories
Ant Design Vue

Ant Design Vue — Spacing and Breadcrumbs

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Space

We can add components with spacing with the a-space component.

For example, we can write:

<template>
  <div id="app">
    <a-space size="small">
      <a-button type="primary">Primary</a-button>
      <a-button>Default</a-button>
      <a-button type="dashed">Dashed</a-button>
      <a-button type="link">Link</a-button>
    </a-space>
  </div>
</template>

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

We set the size of the space with the size prop.

Affix

We make a component stick to a viewport with the a-affix component.

For example, we can write:

<template>
  <div id="app">
    <a-affix :offset-top="top">
      <a-button type="primary" @click="top += 10">Affix top</a-button>
    </a-affix>
    <br>
    <a-affix :offset-bottom="bottom">
      <a-button type="primary" @click="bottom += 10">Affix bottom</a-button>
    </a-affix>
  </div>
</template>

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

We make them stick to the top with the offset-top prop.

And we make the component stick to the bottom with the offset-bottom prop.

Breadcrumb

We can add a breadcrumb with the a-breadcrumb component.

For example, we can write:

<template>
  <div id="app">
    <a-breadcrumb>
      <a-breadcrumb-item>Home</a-breadcrumb-item>
      <a-breadcrumb-item>
        <a href>Application Center</a>
      </a-breadcrumb-item>
      <a-breadcrumb-item>
        <a href>Application List</a>
      </a-breadcrumb-item>
      <a-breadcrumb-item>An Application</a-breadcrumb-item>
    </a-breadcrumb>
  </div>
</template>

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

We can also add breadcrumb item with icons by writing:

<template>
  <div id="app">
   <a-breadcrumb>
    <a-breadcrumb-item href="">
      <a-icon type="home" />
    </a-breadcrumb-item>
    <a-breadcrumb-item href="">
      <a-icon type="user" />
      <span>Application List</span>
    </a-breadcrumb-item>
    <a-breadcrumb-item>
      Application
    </a-breadcrumb-item>
  </a-breadcrumb>
  </div>
</template>

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

It also works with Vue Router.

For example, we can write:

main.js

import Vue from "vue";
import App from "./App.vue";
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";
import VueRouter from "vue-router";
Vue.use(Antd);
Vue.use(VueRouter);
Vue.config.productionTip = false;

const routes = [];

const router = new VueRouter({
  routes
});

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

App.vue

<template>
  <div id="app">
    <a-breadcrumb :routes="routes">
      <template slot="itemRender" slot-scope="{ route, params, routes, paths }">
        <span v-if="routes.indexOf(route) === routes.length - 1">{{ route.breadcrumbName }}</span>
        <router-link v-else :to="`${basePath}/${paths.join('/')}`">{{ route.breadcrumbName }}</router-link>
      </template>
    </a-breadcrumb>
    <br>
    {{ $route.path }}
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      basePath: "/components/breadcrumb",
      routes: [
        {
          path: "index",
          breadcrumbName: "home"
        },
        {
          path: "first",
          breadcrumbName: "first",
          children: [
            {
              path: "/general",
              breadcrumbName: "General"
            },
            {
              path: "/layout",
              breadcrumbName: "Layout"
            }
          ]
        },
        {
          path: "second",
          breadcrumbName: "second"
        }
      ]
    };
  }
};
</script>

to add breadcrumb with router-link components.

We put the router-link s into the itemRender slot to render them.

The routes prop takes an array of objects with the path , children and breadcrumbName properties.

Conclusion

We can add breadcrumbs for navigation with Ant Design Vue.