Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAJOR] expose defaults and original Template rendering functions #708

Merged
merged 5 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
},
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌


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 @@ -2092,6 +2109,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