@zappie/foundations 0.22.3 → 0.23.0
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/CHANGELOG.md +12 -0
- package/README.md +35 -4
- package/dist/index.d.ts +18 -3
- package/dist/item-form/root.js +1 -1
- package/dist/item-form/root.js.map +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @zappie/foundations
|
|
2
2
|
|
|
3
|
+
## 0.23.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- b2f87d3: An item on more than one of the branch's menus can be added without naming a menu. `cart.add(item)` and `cart.validate` file such a line under the first menu in display order that lists it, `withDefaultMenu(menu, input)`, and the line follows that menu's hours and pre-order terms. The cart refuses the line only when no menu lists the item. `ItemForm.Root` takes a `menuId` prop for the menu the shopper opened the item from, and uses the same default without one. The form state has `itemIssues`, the issues that belong to no option group, and `onInvalid` receives them as its second argument. The menu messages name a fix the shopper can make.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [3a92894]
|
|
12
|
+
- Updated dependencies [b2f87d3]
|
|
13
|
+
- @zappie/sdk@0.25.0
|
|
14
|
+
|
|
3
15
|
## 0.22.3
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -719,13 +719,32 @@ closedForOrders(branch); // closed, and not taking orders for later
|
|
|
719
719
|
|
|
720
720
|
`src/hours/ADOPTING.md` maps each site's own copies onto these.
|
|
721
721
|
|
|
722
|
+
## Item form menu
|
|
723
|
+
|
|
724
|
+
Pass `menuId` to `ItemForm.Root` with the menu the shopper opened the item
|
|
725
|
+
from. When the item is on more than one of the branch's menus, it decides the
|
|
726
|
+
line's hours and pre-order terms. A `menuId` in `value` or `defaultValue` wins
|
|
727
|
+
over the prop. Without either, the form uses the first menu in display order
|
|
728
|
+
that lists the item, the same default `cart.add` uses, and `state.value.menuId`
|
|
729
|
+
says which.
|
|
730
|
+
|
|
731
|
+
```tsx
|
|
732
|
+
<ItemForm.Root item={item} menuId={openedFrom?.id} onAdded={close}>
|
|
733
|
+
```
|
|
734
|
+
|
|
722
735
|
## Item form focus
|
|
723
736
|
|
|
724
737
|
When an add fails validation, `ItemForm.Root` focuses the first invalid group
|
|
725
|
-
with `preventScroll`, so the page doesn't jump. Pass `onInvalid(groups
|
|
726
|
-
scroll it into view yourself. `groups` lists the invalid groups
|
|
727
|
-
order, each with its `groupId`, its `group` (undefined for a group
|
|
728
|
-
template renders from its own model), its `element` and its `issues`.
|
|
738
|
+
with `preventScroll`, so the page doesn't jump. Pass `onInvalid(groups,
|
|
739
|
+
itemIssues)` to scroll it into view yourself. `groups` lists the invalid groups
|
|
740
|
+
in display order, each with its `groupId`, its `group` (undefined for a group
|
|
741
|
+
the template renders from its own model), its `element` and its `issues`.
|
|
742
|
+
|
|
743
|
+
`itemIssues` holds the issues that belong to no group: the item is sold out,
|
|
744
|
+
no menu here lists it, or the quantity is out of range. No group shows them,
|
|
745
|
+
so show them by Add. The form state has the same list as `state.itemIssues`,
|
|
746
|
+
next to `state.issues`, which holds every issue. Keep Add enabled: a disabled
|
|
747
|
+
Add can't explain why it does nothing.
|
|
729
748
|
|
|
730
749
|
```tsx
|
|
731
750
|
<ItemForm.Root
|
|
@@ -734,6 +753,18 @@ template renders from its own model), its `element` and its `issues`.
|
|
|
734
753
|
groups[0]?.element?.scrollIntoView({ behavior: "smooth", block: "center" })
|
|
735
754
|
}
|
|
736
755
|
>
|
|
756
|
+
{(state) => (
|
|
757
|
+
<Fragment>
|
|
758
|
+
…
|
|
759
|
+
{state.itemIssues.map((issue) => (
|
|
760
|
+
<p key={issue.code} role="alert">
|
|
761
|
+
{issue.message}
|
|
762
|
+
</p>
|
|
763
|
+
))}
|
|
764
|
+
<ItemForm.Add>Add</ItemForm.Add>
|
|
765
|
+
</Fragment>
|
|
766
|
+
)}
|
|
767
|
+
</ItemForm.Root>
|
|
737
768
|
```
|
|
738
769
|
|
|
739
770
|
## Branch map
|
package/dist/index.d.ts
CHANGED
|
@@ -581,7 +581,13 @@ type ItemFormContext = {
|
|
|
581
581
|
setQuantity: (quantity: number) => void;
|
|
582
582
|
unitPrice: Money$1;
|
|
583
583
|
total: Money$1;
|
|
584
|
+
/** Every issue the shopper should see, shown once they try to add. */
|
|
584
585
|
issues: CartIssue[];
|
|
586
|
+
/**
|
|
587
|
+
* The issues in `issues` that belong to no option group, such as an item that is sold out or on
|
|
588
|
+
* no menu here, or a quantity out of range. No group shows these, so show them by Add.
|
|
589
|
+
*/
|
|
590
|
+
itemIssues: CartIssue[];
|
|
585
591
|
canAdd: boolean;
|
|
586
592
|
add: () => CartLine | null;
|
|
587
593
|
status: "incomplete" | "valid" | "added";
|
|
@@ -618,9 +624,18 @@ type ItemFormProps = Omit<PartProps<ItemFormContext>, "onInvalid"> & {
|
|
|
618
624
|
* Called when an add fails validation, after the first invalid group gets
|
|
619
625
|
* focus without scrolling. Groups are in display order. Scroll to
|
|
620
626
|
* `groups[0].element` yourself, for example smoothly. The list is empty
|
|
621
|
-
* when no issue belongs to a group.
|
|
627
|
+
* when no issue belongs to a group. `itemIssues` holds the issues that
|
|
628
|
+
* belong to no group, the same as the context's `itemIssues`: show them,
|
|
629
|
+
* since no group will.
|
|
630
|
+
*/
|
|
631
|
+
onInvalid?: (groups: ItemFormInvalidGroup[], itemIssues: CartIssue[]) => void;
|
|
632
|
+
/**
|
|
633
|
+
* The menu the shopper opened the item from. It decides the line's hours and pre-order terms
|
|
634
|
+
* when the item is on more than one of the branch's menus. A `menuId` in `value` or
|
|
635
|
+
* `defaultValue` wins over it. Without either, the line goes under the first menu in display
|
|
636
|
+
* order that lists the item (`withDefaultMenu` in `@zappie/sdk`).
|
|
622
637
|
*/
|
|
623
|
-
|
|
638
|
+
menuId?: string;
|
|
624
639
|
/**
|
|
625
640
|
* True when the shopper hasn't chosen a branch yet, for example on a shared
|
|
626
641
|
* product link. Add then adds nothing and calls `onBranchRequired` instead,
|
|
@@ -634,7 +649,7 @@ type ItemFormProps = Omit<PartProps<ItemFormContext>, "onInvalid"> & {
|
|
|
634
649
|
*/
|
|
635
650
|
onBranchRequired?: (input: CartLineInput) => void;
|
|
636
651
|
};
|
|
637
|
-
declare function Root$2({ item, value, defaultValue, onValueChange, defaultQuantity, onAdded, onCommit, onInvalid, branchRequired, onBranchRequired, ...props }: ItemFormProps): react.JSX.Element;
|
|
652
|
+
declare function Root$2({ item, value, defaultValue, onValueChange, defaultQuantity, onAdded, onCommit, onInvalid, branchRequired, onBranchRequired, menuId, ...props }: ItemFormProps): react.JSX.Element;
|
|
638
653
|
|
|
639
654
|
type GroupState = {
|
|
640
655
|
group: OptionGroup$1;
|
package/dist/item-form/root.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use client";import{previewItem,ZappieCartError}from"@zappie/sdk";import{useCart,useCartCatalogue,useZappie}from"@zappie/sdk/react";import{createContext,useCallback,useContext,useMemo,useRef,useState}from"react";import{part}from"../internal/slot.js";import{useControllableState}from"../internal/use-controllable-state.js";import{jsx}from"react/jsx-runtime";var ItemFormStateContext=/*@__PURE__ */createContext(null);function useItemForm(){const value=useContext(ItemFormStateContext);if(!value){throw new Error("ItemForm parts require ItemForm.Root")}return value}var GroupRegistry=/*@__PURE__ */createContext(null);function Root({item,value,defaultValue,onValueChange,defaultQuantity,onAdded,onCommit,onInvalid,branchRequired=false,onBranchRequired,...props}){const client=useZappie();const{actions}=useCart();const menu=useCartCatalogue();const[input,setInput]=useControllableState({value,defaultValue:{...defaultValue,itemId:item.id,quantity:defaultQuantity??defaultValue?.quantity??item.quantity.min},onChange:onValueChange});const registry=useRef(new Map);const[added,setAdded]=useState(false);const[attempted,setAttempted]=useState(false);const[failure,setFailure]=useState(null);const normalized=useMemo(()=>
|
|
1
|
+
"use client";import{previewItem,withDefaultMenu,ZappieCartError}from"@zappie/sdk";import{useCart,useCartCatalogue,useZappie}from"@zappie/sdk/react";import{createContext,useCallback,useContext,useMemo,useRef,useState}from"react";import{part}from"../internal/slot.js";import{useControllableState}from"../internal/use-controllable-state.js";import{jsx}from"react/jsx-runtime";var ItemFormStateContext=/*@__PURE__ */createContext(null);function useItemForm(){const value=useContext(ItemFormStateContext);if(!value){throw new Error("ItemForm parts require ItemForm.Root")}return value}var GroupRegistry=/*@__PURE__ */createContext(null);var ungrouped=issues=>issues.filter(issue=>!issue.groupId);function Root({item,value,defaultValue,onValueChange,defaultQuantity,onAdded,onCommit,onInvalid,branchRequired=false,onBranchRequired,menuId,...props}){const client=useZappie();const{actions}=useCart();const menu=useCartCatalogue();const[input,setInput]=useControllableState({value,defaultValue:{...defaultValue,itemId:item.id,quantity:defaultQuantity??defaultValue?.quantity??item.quantity.min},onChange:onValueChange});const registry=useRef(new Map);const[added,setAdded]=useState(false);const[attempted,setAttempted]=useState(false);const[failure,setFailure]=useState(null);const normalized=useMemo(()=>{const line={...input,itemId:item.id};if(line.menuId===void 0&&menuId!==void 0){line.menuId=menuId}return menu?withDefaultMenu(menu,line):line},[input,item.id,menuId,menu]);const preview=useMemo(()=>previewItem(item,normalized),[item,normalized]);const validation=useMemo(()=>client.cart.validate(normalized),[client,normalized,menu]);const issues=failure??(attempted&&!validation.ok?validation.issues:[]);const setValue=useCallback(next=>{setAdded(false);setFailure(null);setInput({...next,itemId:item.id})},[setInput,item.id]);const setQuantity=useCallback(quantity=>setValue({...normalized,quantity}),[setValue,normalized]);const focusIssue=useCallback(errors=>{const ids=new Set([...registry.current.keys(),...preview.groups.map(group=>group.id)]);const invalid=[...ids].flatMap(groupId=>{const issues2=errors.filter(issue=>issue.groupId===groupId);return issues2.length?[{groupId,group:preview.groups.find(group=>group.id===groupId),element:registry.current.get(groupId),issues:issues2}]:[]});invalid[0]?.element?.focus({preventScroll:true});onInvalid?.(invalid,ungrouped(errors))},[preview.groups,onInvalid]);const add=useCallback(()=>{if(branchRequired){onBranchRequired?.(normalized);return null}const result=client.cart.validate(normalized);if(!result.ok){setAttempted(true);setFailure(result.issues);focusIssue(result.issues);return null}try{const line=onCommit?onCommit(normalized):actions.add(item,normalized);setAttempted(false);setAdded(true);onAdded?.(line);return line}catch(error){if(error instanceof ZappieCartError){setAttempted(true);setFailure([...error.issues]);focusIssue([...error.issues]);return null}throw error}},[client,actions,normalized,item,onAdded,onCommit,focusIssue,branchRequired,onBranchRequired]);const context=useMemo(()=>({item,...preview,quantity:normalized.quantity??item.quantity.min,setQuantity,issues,itemIssues:ungrouped(issues),canAdd:validation.ok&&!failure,add,status:added?"added":validation.ok?"valid":"incomplete",value:normalized,setValue,branchRequired}),[item,preview,normalized,setQuantity,issues,validation.ok,failure,add,added,setValue,branchRequired]);return jsx(ItemFormStateContext.Provider,{value:context,children:jsx(GroupRegistry.Provider,{value:registry.current,children:part("div",props,context,{"data-zappie-item":item.id,"data-state":issues.length?"invalid":"valid","data-sold-out":item.isAvailable?void 0:""})})})}export{GroupRegistry,ItemFormStateContext,Root,useItemForm};
|
|
2
2
|
//# sourceMappingURL=root.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/item-form/root.tsx"],
|
|
4
|
-
"mappings": "aACA,OACE,YACA,oBAOK,cACP,OAAS,QAAS,iBAAkB,cAAiB,oBACrD,OACE,cACA,YACA,WACA,QACA,OACA,aACK,QACP,OAAS,SAA4B,sBACrC,OAAS,yBAA4B,
|
|
4
|
+
"mappings": "aACA,OACE,YACA,gBACA,oBAOK,cACP,OAAS,QAAS,iBAAkB,cAAiB,oBACrD,OACE,cACA,YACA,WACA,QACA,OACA,aACK,QACP,OAAS,SAA4B,sBACrC,OAAS,yBAA4B,wCA+P/B,mCAjOC,IAAM,qBACX,eAAgB,cAAsC,IAAI,EACrD,SAAS,aAAc,CAC5B,MAAM,MAAQ,WAAW,oBAAoB,EAC7C,GAAI,CAAC,M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AAa,IAAI,EACjB,WAAW,CAAC,GAAG,MAAM,MAAM,CAAC,EAC5B,WAAW,CAAC,GAAG,MAAM,MAAM,CAAC,EAC5B,OAAO,IACT,CACA,MAAM,KACR,CACF,EAAG,CACD,OACA,QACA,WACA,KACA,QACA,SACA,WACA,eACA,gBACF,CAAC,EACD,MAAM,QAA2B,QAC/B,KAAO,CACL,KACA,GAAG,QACH,SAAU,WAAW,UAAY,KAAK,SAAS,IAC/C,YACA,OACA,WAAY,UAAU,MAAM,EAC5B,OAAQ,WAAW,IAAM,CAAC,QAC1B,IACA,OAAQ,MAAQ,QAAU,WAAW,GAAK,QAAU,aACpD,MAAO,WACP,SACA,cACF,GACA,CACE,KACA,QACA,WACA,YACA,OACA,WAAW,GACX,QACA,IACA,MACA,SACA,cACF,CACF,EACA,OACE,IAAC,qBAAqB,SAArB,CAA8B,MAAO,QACpC,aAAC,cAAc,SAAd,CAAuB,MAAO,SAAS,QACrC,cAAK,MAAO,MAAO,QAAS,CAC3B,mBAAoB,KAAK,GACzB,aAAc,OAAO,OAAS,UAAY,QAC1C,gBAAiB,KAAK,YAAc,OAAY,EAClD,CAAC,EACH,EACF,CAEJ",
|
|
5
5
|
"names": ["issues"]
|
|
6
6
|
}
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var PACKAGE_VERSION=true?"0.
|
|
1
|
+
var PACKAGE_VERSION=true?"0.23.0":"0.0.0-development";export{PACKAGE_VERSION};
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zappie/foundations",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"description": "UI-less React primitives for Zappie storefronts: behaviour, state and accessibility without styles.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -101,7 +101,7 @@
|
|
|
101
101
|
"peerDependencies": {
|
|
102
102
|
"react": "^18.2.0 || ^19.0.0",
|
|
103
103
|
"react-dom": "^18.2.0 || ^19.0.0",
|
|
104
|
-
"@zappie/sdk": "^0.
|
|
104
|
+
"@zappie/sdk": "^0.25.0",
|
|
105
105
|
"react-router": "^8.4.0"
|
|
106
106
|
},
|
|
107
107
|
"peerDependenciesMeta": {
|
|
@@ -118,7 +118,7 @@
|
|
|
118
118
|
"@types/google.maps": "^3.66.7",
|
|
119
119
|
"esbuild": "^0.28.2",
|
|
120
120
|
"react-router": "8.5.0",
|
|
121
|
-
"@zappie/sdk": "0.
|
|
121
|
+
"@zappie/sdk": "0.25.0"
|
|
122
122
|
},
|
|
123
123
|
"license": "MIT",
|
|
124
124
|
"homepage": "https://github.com/zappie/storefront-packages#readme",
|