Skip to content

Commit

Permalink
[MAJOR] expose defaults and original Template rendering functions (C…
Browse files Browse the repository at this point in the history
…hoices-js#708)

* expose original Template rendering functions

* add to types

* add moduleResolution

* use `defaults`
  • Loading branch information
tinovyatkin authored and jshjohnson committed Oct 29, 2019
1 parent 1c75147 commit 9504cfc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,24 @@ classNames: {
**Input types affected:** `text`, `select-one`, `select-multiple`
**Usage:** Function to run on template creation. Through this callback it is possible to provide custom templates for the various components of Choices (see terminology). For Choices to work with custom templates, it is important you maintain the various data attributes defined [here](https://github.com/jshjohnson/Choices/blob/master/src/scripts/templates.js).
If you want just extend a little original template then you may use `Choices.defaults.templates` to get access to
original template function.
**Example:**
```js
const example = new Choices(element, {
callbackOnCreateTemplates: () => ({
input: (...args) =>
Object.assign(Choices.defaults.templates.input.call(this, ...args), {
type: 'email',
}),
}),
});
```
or more complex:
```js
const example = new Choices(element, {
callbackOnCreateTemplates: function(template) {
Expand Down
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"lib": ["esnext", "dom"],
"types": ["cypress"],
"strict": true,
"moduleResolution": "node",
/* Additional Checks */
"noImplicitAny": false,
"noUnusedLocals": true,
Expand Down
21 changes: 18 additions & 3 deletions src/scripts/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,28 @@ import {
diff,
} from './lib/utils';

const USER_DEFAULTS = /** @type {Partial<import('../../types/index').Choices.Options>} */ ({});

/**
* Choices
* @author Josh Johnson<[email protected]>
*/
class Choices {
/* ========================================
= Static properties =
======================================== */

static get defaults() {
return Object.preventExtensions({
get options() {
return USER_DEFAULTS;
},
get templates() {
return TEMPLATES;
},
});
}

constructor(element = '[data-choice]', userConfig = {}) {
if (isType('String', element)) {
const elements = Array.from(document.querySelectorAll(element));
Expand All @@ -56,7 +73,7 @@ class Choices {
}

this.config = merge.all(
[DEFAULT_CONFIG, Choices.userDefaults, userConfig],
[DEFAULT_CONFIG, Choices.defaults.options, userConfig],
// When merging array configs, replace with a copy of the userConfig array,
// instead of concatenating with the default array
{ arrayMerge: (destinationArray, sourceArray) => [...sourceArray] },
Expand Down Expand Up @@ -2108,6 +2125,4 @@ class Choices {
/* ===== End of Private functions ====== */
}

Choices.userDefaults = {};

export default Choices;
4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,10 @@ declare namespace Choices {

// Exporting default class
export default class Choices {
static readonly defaults: {
readonly options: Partial<Choices.Options>;
readonly templates: Choices.Templates;
};
readonly config: Choices.Options;

// State Tracking
Expand Down

0 comments on commit 9504cfc

Please sign in to comment.