Skip to content

Commit 2ba9ccc

Browse files
committed
Bug/Feat: Fix issues with reinit calling on init. Add index args for groups
1 parent 5c3432b commit 2ba9ccc

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/query/src/behavior.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export class Behavior {
5353
// custom invocation fallback
5454
customInvocation = noop,
5555

56+
// element index information
57+
elementIndex = 0,
58+
totalElements = 1,
59+
5660
// standard
5761
selectors = {},
5862
classNames = {},
@@ -72,6 +76,10 @@ export class Behavior {
7276
this.namespace = namespace;
7377
this.customInvocation = customInvocation;
7478

79+
// store element index information
80+
this.elementIndex = elementIndex;
81+
this.totalElements = totalElements;
82+
7583
this.classNames = classNames;
7684
this.selectors = selectors;
7785
this.errors = errors;
@@ -527,6 +535,13 @@ export class Behavior {
527535
self,
528536
behavior: self,
529537
namespace: self.namespace,
538+
539+
// element index information
540+
index: self.elementIndex,
541+
total: self.totalElements,
542+
isFirst: self.elementIndex === 0,
543+
isLast: self.elementIndex === self.totalElements - 1,
544+
530545
get cache() {
531546
let cache = self.Query.prototype[self.namespace].cache;
532547
if (!cache) {

packages/query/src/register-behavior.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepExtend, isArray, isDevelopment, isString, noop, pick } from '@semantic-ui/utils';
1+
import { deepExtend, filterObject, isArray, isDevelopment, isString, noop, pick } from '@semantic-ui/utils';
22
import { Behavior } from './behavior.js';
33
import { Query } from './query.js';
44

@@ -80,7 +80,11 @@ export const registerBehavior = (behavior) => {
8080

8181
// Create abstraction around behavior initialization
8282
Query.prototype[name] = function(settings) {
83-
// At run time we need to check if defaults are changed from original registration
83+
// ignore undefined settings
84+
settings = filterObject(settings, (value) => value !== undefined);
85+
86+
// At run time we need to check if defaults are changed by user
87+
// this can occur if $.plugin.defaultSettings is modified
8488
const defaultValues = ['defaultSettings', 'classNames', 'errors', 'selector'];
8589
const behaviorDefaults = pick(Query.prototype[name], ...defaultValues);
8690
const runtimeConfig = {
@@ -115,13 +119,17 @@ export const registerBehavior = (behavior) => {
115119

116120
// behavior is stored in namespace like el.behavior
117121
let instance = Behavior.getInstance(element, namespace);
122+
let existingInstance = !!instance;
118123

119124
const behaviorConfig = {
120125
sharedBehavior, // setup can pass shared behavior across instances
121126
$element,
122127
Query,
123128
...runtimeConfig,
124129
settings: runtimeSettings,
130+
// pass element index information
131+
elementIndex: index,
132+
totalElements: $elements.length,
125133
};
126134

127135
// create behavior instance if not defined
@@ -149,7 +157,7 @@ export const registerBehavior = (behavior) => {
149157
returnedValue = response;
150158
}
151159
}
152-
else if (instance !== undefined) {
160+
else if (existingInstance) {
153161
// if they are not calling a method and there are settings
154162
// than they are attempting to reinitialize the behavior with new settings
155163
instance.reinitialize(behaviorConfig);

0 commit comments

Comments
 (0)