@lasterp/shared 1.0.0-alpha.17 → 1.0.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/node/index.js +37 -12
- package/dist/rn/index.js +37 -12
- package/package.json +1 -1
package/dist/node/index.js
CHANGED
|
@@ -466,7 +466,7 @@ var useFrappeUpdateDoc = () => {
|
|
|
466
466
|
};
|
|
467
467
|
};
|
|
468
468
|
// src/hooks/use-variant-selector/hook.ts
|
|
469
|
-
import { useMemo as useMemo2, useState as useState5 } from "react";
|
|
469
|
+
import { useCallback as useCallback4, useMemo as useMemo2, useState as useState5 } from "react";
|
|
470
470
|
|
|
471
471
|
// src/hooks/use-variant-selector/utils.ts
|
|
472
472
|
function findVariant(variants, specs, caseInsensitive) {
|
|
@@ -515,14 +515,18 @@ var useVariantSelector = (props) => {
|
|
|
515
515
|
}, [variants, selectedSpecs, attributes]);
|
|
516
516
|
const options = useMemo2(() => {
|
|
517
517
|
const result = {};
|
|
518
|
-
attributes.forEach((attr) => {
|
|
518
|
+
attributes.forEach((attr, attrIndex) => {
|
|
519
|
+
const constraints = {};
|
|
520
|
+
for (let i = 0;i < attrIndex; i++) {
|
|
521
|
+
const key = attributes[i]?.key;
|
|
522
|
+
if (key && selectedSpecs[key])
|
|
523
|
+
constraints[key] = selectedSpecs[key];
|
|
524
|
+
}
|
|
525
|
+
const matchingVariants = findVariants(variants, constraints);
|
|
526
|
+
const availableValues = new Set(matchingVariants.map((v) => v.specs[attr.key]));
|
|
519
527
|
result[attr.key] = attr.values.map((value) => {
|
|
520
528
|
const isSelected = selectedSpecs[attr.key] === value;
|
|
521
|
-
const
|
|
522
|
-
delete specs[attr.key];
|
|
523
|
-
const matchingVariants = findVariants(variants, specs);
|
|
524
|
-
const availableValuesForAttr = new Set(matchingVariants.map((v) => v.specs[attr.key]));
|
|
525
|
-
const isAvailable = availableValuesForAttr.has(value);
|
|
529
|
+
const isAvailable = availableValues.has(value);
|
|
526
530
|
return {
|
|
527
531
|
key: attr.key,
|
|
528
532
|
value,
|
|
@@ -532,13 +536,34 @@ var useVariantSelector = (props) => {
|
|
|
532
536
|
});
|
|
533
537
|
return result;
|
|
534
538
|
}, [variants, attributes, selectedSpecs]);
|
|
535
|
-
const onOptionSelect = (key, value) => {
|
|
539
|
+
const onOptionSelect = useCallback4((key, value) => {
|
|
536
540
|
setSelectedSpecs((prev) => {
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
|
|
541
|
+
const newSpecs = { ...prev, [key]: value };
|
|
542
|
+
const attrIndex = attributes.findIndex((a) => a.key === key);
|
|
543
|
+
for (let i = attrIndex + 1;i < attributes.length; i++) {
|
|
544
|
+
const downstreamAttr = attributes[i];
|
|
545
|
+
if (!downstreamAttr)
|
|
546
|
+
continue;
|
|
547
|
+
const constraints = {};
|
|
548
|
+
for (let j = 0;j < i; j++) {
|
|
549
|
+
const k = attributes[j]?.key;
|
|
550
|
+
if (k && newSpecs[k])
|
|
551
|
+
constraints[k] = newSpecs[k];
|
|
552
|
+
}
|
|
553
|
+
const matching = findVariants(variants, constraints);
|
|
554
|
+
const availableValues = new Set(matching.map((v) => v.specs[downstreamAttr.key]));
|
|
555
|
+
if (!availableValues.has(newSpecs[downstreamAttr.key])) {
|
|
556
|
+
const firstAvailable = downstreamAttr.values.find((v) => availableValues.has(v));
|
|
557
|
+
if (firstAvailable) {
|
|
558
|
+
newSpecs[downstreamAttr.key] = firstAvailable;
|
|
559
|
+
} else {
|
|
560
|
+
delete newSpecs[downstreamAttr.key];
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return newSpecs;
|
|
540
565
|
});
|
|
541
|
-
};
|
|
566
|
+
}, [attributes, variants]);
|
|
542
567
|
return {
|
|
543
568
|
variantId,
|
|
544
569
|
onOptionSelect,
|
package/dist/rn/index.js
CHANGED
|
@@ -466,7 +466,7 @@ var useFrappeUpdateDoc = () => {
|
|
|
466
466
|
};
|
|
467
467
|
};
|
|
468
468
|
// src/hooks/use-variant-selector/hook.ts
|
|
469
|
-
import { useMemo as useMemo2, useState as useState5 } from "react";
|
|
469
|
+
import { useCallback as useCallback4, useMemo as useMemo2, useState as useState5 } from "react";
|
|
470
470
|
|
|
471
471
|
// src/hooks/use-variant-selector/utils.ts
|
|
472
472
|
function findVariant(variants, specs, caseInsensitive) {
|
|
@@ -515,14 +515,18 @@ var useVariantSelector = (props) => {
|
|
|
515
515
|
}, [variants, selectedSpecs, attributes]);
|
|
516
516
|
const options = useMemo2(() => {
|
|
517
517
|
const result = {};
|
|
518
|
-
attributes.forEach((attr) => {
|
|
518
|
+
attributes.forEach((attr, attrIndex) => {
|
|
519
|
+
const constraints = {};
|
|
520
|
+
for (let i = 0;i < attrIndex; i++) {
|
|
521
|
+
const key = attributes[i]?.key;
|
|
522
|
+
if (key && selectedSpecs[key])
|
|
523
|
+
constraints[key] = selectedSpecs[key];
|
|
524
|
+
}
|
|
525
|
+
const matchingVariants = findVariants(variants, constraints);
|
|
526
|
+
const availableValues = new Set(matchingVariants.map((v) => v.specs[attr.key]));
|
|
519
527
|
result[attr.key] = attr.values.map((value) => {
|
|
520
528
|
const isSelected = selectedSpecs[attr.key] === value;
|
|
521
|
-
const
|
|
522
|
-
delete specs[attr.key];
|
|
523
|
-
const matchingVariants = findVariants(variants, specs);
|
|
524
|
-
const availableValuesForAttr = new Set(matchingVariants.map((v) => v.specs[attr.key]));
|
|
525
|
-
const isAvailable = availableValuesForAttr.has(value);
|
|
529
|
+
const isAvailable = availableValues.has(value);
|
|
526
530
|
return {
|
|
527
531
|
key: attr.key,
|
|
528
532
|
value,
|
|
@@ -532,13 +536,34 @@ var useVariantSelector = (props) => {
|
|
|
532
536
|
});
|
|
533
537
|
return result;
|
|
534
538
|
}, [variants, attributes, selectedSpecs]);
|
|
535
|
-
const onOptionSelect = (key, value) => {
|
|
539
|
+
const onOptionSelect = useCallback4((key, value) => {
|
|
536
540
|
setSelectedSpecs((prev) => {
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
|
|
541
|
+
const newSpecs = { ...prev, [key]: value };
|
|
542
|
+
const attrIndex = attributes.findIndex((a) => a.key === key);
|
|
543
|
+
for (let i = attrIndex + 1;i < attributes.length; i++) {
|
|
544
|
+
const downstreamAttr = attributes[i];
|
|
545
|
+
if (!downstreamAttr)
|
|
546
|
+
continue;
|
|
547
|
+
const constraints = {};
|
|
548
|
+
for (let j = 0;j < i; j++) {
|
|
549
|
+
const k = attributes[j]?.key;
|
|
550
|
+
if (k && newSpecs[k])
|
|
551
|
+
constraints[k] = newSpecs[k];
|
|
552
|
+
}
|
|
553
|
+
const matching = findVariants(variants, constraints);
|
|
554
|
+
const availableValues = new Set(matching.map((v) => v.specs[downstreamAttr.key]));
|
|
555
|
+
if (!availableValues.has(newSpecs[downstreamAttr.key])) {
|
|
556
|
+
const firstAvailable = downstreamAttr.values.find((v) => availableValues.has(v));
|
|
557
|
+
if (firstAvailable) {
|
|
558
|
+
newSpecs[downstreamAttr.key] = firstAvailable;
|
|
559
|
+
} else {
|
|
560
|
+
delete newSpecs[downstreamAttr.key];
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return newSpecs;
|
|
540
565
|
});
|
|
541
|
-
};
|
|
566
|
+
}, [attributes, variants]);
|
|
542
567
|
return {
|
|
543
568
|
variantId,
|
|
544
569
|
onOptionSelect,
|