UNPKG

@paraboly/pwc-multi-filter

Version:

A wrapper over pwc-tabview and pwc-filter. Provides means of dynamically managing multiple filters via a single component.

95 lines (90 loc) 3.21 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const core = require('./core-d69cee64.js'); require('./_commonjsHelpers-ab75601c.js'); require('./linq-23ba6bed.js'); const utils = require('./utils-54b3bea2.js'); const lodash = require('./lodash-23b825de.js'); const PwcDynamicFormContent = class { constructor(hostRef) { core.registerInstance(this, hostRef); this.resolvedItems = []; this.fieldRefs = []; this.itemsAddedViaMethod = []; this.defaultItems = []; this.items = this.defaultItems; } itemsWatchHandler(newValue) { if (newValue === null || newValue === undefined) { this.items = this.defaultItems; } else { this.resolvedItems = [ ...utils.resolveJson(newValue), ...this.itemsAddedViaMethod ]; } } async addItem(config) { if (config) { this.itemsAddedViaMethod = [...this.itemsAddedViaMethod, config]; this.resolvedItems = [...this.resolvedItems, config]; this.rootElement.forceUpdate(); } } async removeItem(id) { if (id !== null || id !== undefined) { lodash._.remove(this.itemsAddedViaMethod, { id }); lodash._.remove(this.resolvedItems, { id }); this.rootElement.forceUpdate(); } } async getFieldRefs() { return this.fieldRefs || []; } componentWillLoad() { this.itemsWatchHandler(this.items); } handleFieldRef(fieldConfig, fieldRef) { if (fieldRef) { this.fieldRefs = lodash._.unionBy(this.fieldRefs, [fieldRef], i => i.config.name); } else { lodash._.remove(this.fieldRefs, i => i.config.name === fieldConfig.name); } } constructField(fieldConfig) { if (fieldConfig) { let nameIndexer = 0; const nameGenerator = () => { return "pwc-dynamic-form___generated-name-" + nameIndexer++; }; let idIndexer = 0; const idGenerator = () => { return "pwc-dynamic-form___generated-id-" + idIndexer++; }; fieldConfig.name = fieldConfig.name || nameGenerator(); fieldConfig.id = fieldConfig.id || idGenerator(); return (core.h("pwc-dynamic-form-field", { key: fieldConfig.id + fieldConfig.name, config: fieldConfig, ref: this.handleFieldRef.bind(this, fieldConfig) })); } else { return ""; } } render() { if (this.resolvedItems) { // we are mutating the config, therefore we have to clone it to leave the user input intact. const resolvedItemsClone = lodash._.cloneDeep(this.resolvedItems); return resolvedItemsClone.map(this.constructField.bind(this)); } else { return ""; } } get rootElement() { return core.getElement(this); } static get watchers() { return { "items": ["itemsWatchHandler"] }; } static get style() { return ""; } }; exports.pwc_dynamic_form_content = PwcDynamicFormContent;