blowfish/node_modules/@headlessui/react/dist/components/radio-group/radio-group.js

2 lines
5.9 KiB
JavaScript
Raw Normal View History

2023-01-29 22:30:24 +00:00
import g,{createContext as J,useContext as q,useMemo as A,useReducer as se,useRef as B,useEffect as ue}from"react";import{forwardRefWithAs as Q,render as X,compact as de}from'../../utils/render.js';import{useId as Y}from'../../hooks/use-id.js';import{match as ce}from'../../utils/match.js';import{useIsoMorphicEffect as fe}from'../../hooks/use-iso-morphic-effect.js';import{Keys as P}from'../../components/keyboard.js';import{focusIn as Z,Focus as H,FocusResult as z,sortByDomNode as Te}from'../../utils/focus-management.js';import{useFlags as me}from'../../hooks/use-flags.js';import{Label as be,useLabels as ee}from'../../components/label/label.js';import{Description as Re,useDescriptions as te}from'../../components/description/description.js';import{useTreeWalker as ye}from'../../hooks/use-tree-walker.js';import{useSyncRefs as re}from'../../hooks/use-sync-refs.js';import{Hidden as ge,Features as ve}from'../../internal/hidden.js';import{attemptSubmit as Oe,objectToFormEntries as Ee}from'../../utils/form.js';import{getOwnerDocument as Ae}from'../../utils/owner.js';import{useEvent as v}from'../../hooks/use-event.js';import{useControllable as Pe}from'../../hooks/use-controllable.js';import{isDisabledReactIssue7711 as oe}from'../../utils/bugs.js';import{useLatestValue as De}from'../../hooks/use-latest-value.js';import{useDisposables as Ge}from'../../hooks/use-disposables.js';var he=(t=>(t[t.RegisterOption=0]="RegisterOption",t[t.UnregisterOption=1]="UnregisterOption",t))(he||{});let ke={[0](n,r){let t=[...n.options,{id:r.id,element:r.element,propsRef:r.propsRef}];return{...n,options:Te(t,d=>d.element.current)}},[1](n,r){let t=n.options.slice(),d=n.options.findIndex(c=>c.id===r.id);return d===-1?n:(t.splice(d,1),{...n,options:t})}},$=J(null);$.displayName="RadioGroupDataContext";function ne(n){let r=q($);if(r===null){let t=new Error(`<${n} /> is missing a parent <RadioGroup /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(t,ne),t}return r}let V=J(null);V.displayName="RadioGroupActionsContext";function ie(n){let r=q(V);if(r===null){let t=new Error(`<${n} /> is missing a parent <RadioGroup /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(t,ie),t}return r}function Fe(n,r){return ce(r.type,ke,n,r)}let Le="div",Ce=Q(function(r,t){let d=Y(),{id:c=`headlessui-radiogroup-${d}`,value:D,defaultValue:O,name:I,onChange:E,by:G=(e,i)=>e===i,disabled:h=!1,...U}=r,T=v(typeof G=="string"?(e,i)=>{let o=G;return(e==null?void 0:e[o])===(i==null?void 0:i[o])}:G),[k,F]=se(Fe,{options:[]}),a=k.options,[M,N]=ee(),[m,L]=te(),C=B(null),W=re(C,t),[l,x]=Pe(D,E,O),s=A(()=>a.find(e=>!e.propsRef.current.disabled),[a]),b=A(()=>a.some(e=>T(e.propsRef.current.value,l)),[a,l]),u=v(e=>{var o;if(h||T(e,l))return!1;let i=(o=a.find(f=>T(f.propsRef.current.value,e)))==null?void 0:o.propsRef.current;return i!=null&&i.disabled?!1:(x==null||x(e),!0)});ye({container:C.current,accept(e){return e.getAttribute("role")==="radio"?NodeFilter.FILTER_REJECT:e.hasAttribute("role")?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT},walk(e){e.setAttribute("role","none")}});let K=v(e=>{let i=C.current;if(!i)return;let o=Ae(i),f=a.filter(p=>p.propsRef.current.disabled===!1).map(p=>p.element.current);switch(e.key){case P.Enter:Oe(e.currentTarget);break;case P.ArrowLeft:case P.ArrowUp:if(e.preventDefault(),e.stopPropagation(),Z(f,H.Previous|H.WrapAround)===z.Success){let y=a.find(j=>j.element.current===(o==null?void 0:o.activeElement));y&&u(y.propsRef.current.value)}break;case P.ArrowRight:case P.ArrowDown:if(e.preventDefault(),e.stopPropagation(),Z(f,H.Next|H.WrapAround)===z.Success){let y=a.find(j=>j.element.current===(o==null?void 0:o.activeElement));y&&u(y.propsRef.current.value)}break;case P.Space:{e.preventDefault(),e.stopPropagation();let p=a.find(y=>y.element.current===(o==null?void 0:o.activeElement));p&&u(p.propsRef.current.value)}break}}),w=v(e=>(F({type:0,...e}),()=>F({type:1,id:e.id}))),R=A(()=>({value:l,firstOption:s,containsCheckedOption:b,disabled:h,compare:T,...k}),[l,s,b,h,T,k]),S=A(()=>({registerOption:w,change:u}),[w,u]),ae={re