2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-05-17 02:29:37 +03:00
Files
vue-select/docs/guide/components.md
T
Jeff Sagal efc5093207 Add API for overwriting default components (#850)
* implement API for overwriting child components

* add test coverage

* update documentation for Components & Styling

* update docs

* refactor API, update docs

* remove the service worker

* fix tests
2019-04-25 15:03:43 -07:00

2.3 KiB

Vue Select utilizes child components throughout, and exposes an API to overwrite these components with your own, using the components {Object} prop. When implementing the components prop in your code, Vue Select merge it's default components with any keys that you set in the object.

Your object will be merged with the object that is exported below:

<<< @/src/components/childComponents.js{4-7}

You can override the value of any of these keys with your own components.

Available Components

Deselect

You may wish to roll your own deselect button. Deselect is used within tags on multiple selects, and serves as the clear button for single selects. Maybe you just want to use a simple <button>Clear</button> instead.

<v-select :components="{Deselect}" />
computed: {
  Deselect() {
    return Vue.component('Deselect', {
      render (createElement) {
        return createElement('button', 'Clear')
      }
    })
  }
}

The same approach applies for multiple selects:

OpenIndicator

The OpenIndicator component is the 'caret' used within the component that adjusts orientation based on whether the dropdown is open or closed.

<v-select :components="{OpenIndicator}" />
computed: {
  OpenIndicator () {
    return Vue.component('OpenIndicator', {
      render (createElement) {
        return createElement('button', '🤘🏻');
      },
    });
  },
},

Setting at Registration

If you want to all instances of Vue Select to use your custom components throughout your app, while only having to set the implementation once, you can do so when registering Vue Select as a component.

import Vue from 'vue';
import vSelect from 'vue-select';

/**
* Create custom components to override defaults.
* @type {{OpenIndicator: *, Deselect: *}}
*/
const components = {
  Deselect: Vue.component('Deselect', {
    render: (createElement) => createElement('button', '❌'),
  }),
  OpenIndicator: Vue.component('OpenIndicator', {
    render: (createElement) => createElement('span', '🔽'),
  }),
};

// Set the components prop default to return our fresh components 
vSelect.props.components.default = () => components;

// Register the component
Vue.component(vSelect)