Files
2024-06-11 23:18:25 +02:00

2 lines
6.0 KiB
JavaScript

import O,{createContext as J,useContext as V,useMemo as A,useReducer as se,useRef as j,useEffect as ue}from"react";import{forwardRefWithAs as X,render as q,compact as de}from'../../utils/render.js';import{useId as Q}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 D}from'../../components/keyboard.js';import{focusIn as Y,Focus as I,FocusResult as Z,sortByDomNode as Te}from'../../utils/focus-management.js';import{useFlags as me}from'../../hooks/use-flags.js';import{Label as ye,useLabels as z}from'../../components/label/label.js';import{Description as Re,useDescriptions as ee}from'../../components/description/description.js';import{useTreeWalker as be}from'../../hooks/use-tree-walker.js';import{useSyncRefs as te}from'../../hooks/use-sync-refs.js';import{Hidden as ge,Features as Oe}from'../../internal/hidden.js';import{attemptSubmit as Ee,objectToFormEntries as ve}from'../../utils/form.js';import{getOwnerDocument as Pe}from'../../utils/owner.js';import{useEvent as E}from'../../hooks/use-event.js';import{useControllable as Ae}from'../../hooks/use-controllable.js';import{isDisabledReactIssue7711 as re}from'../../utils/bugs.js';import{useLatestValue as De}from'../../hooks/use-latest-value.js';import{useDisposables as Ge}from'../../hooks/use-disposables.js';var Ce=(t=>(t[t.RegisterOption=0]="RegisterOption",t[t.UnregisterOption=1]="UnregisterOption",t))(Ce||{});let ke={[0](r,o){let t=[...r.options,{id:o.id,element:o.element,propsRef:o.propsRef}];return{...r,options:Te(t,p=>p.element.current)}},[1](r,o){let t=r.options.slice(),p=r.options.findIndex(T=>T.id===o.id);return p===-1?r:(t.splice(p,1),{...r,options:t})}},B=J(null);B.displayName="RadioGroupDataContext";function oe(r){let o=V(B);if(o===null){let t=new Error(`<${r} /> is missing a parent <RadioGroup /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(t,oe),t}return o}let $=J(null);$.displayName="RadioGroupActionsContext";function ne(r){let o=V($);if(o===null){let t=new Error(`<${r} /> is missing a parent <RadioGroup /> component.`);throw Error.captureStackTrace&&Error.captureStackTrace(t,ne),t}return o}function Le(r,o){return ce(o.type,ke,r,o)}let he="div";function Fe(r,o){let t=Q(),{id:p=`headlessui-radiogroup-${t}`,value:T,defaultValue:v,form:S,name:m,onChange:M,by:G=(e,i)=>e===i,disabled:C=!1,...H}=r,y=E(typeof G=="string"?(e,i)=>{let n=G;return(e==null?void 0:e[n])===(i==null?void 0:i[n])}:G),[P,h]=se(Le,{options:[]}),a=P.options,[N,R]=z(),[k,U]=ee(),L=j(null),W=te(L,o),[l,s]=Ae(T,M,v),b=A(()=>a.find(e=>!e.propsRef.current.disabled),[a]),F=A(()=>a.some(e=>y(e.propsRef.current.value,l)),[a,l]),d=E(e=>{var n;if(C||y(e,l))return!1;let i=(n=a.find(f=>y(f.propsRef.current.value,e)))==null?void 0:n.propsRef.current;return i!=null&&i.disabled?!1:(s==null||s(e),!0)});be({container:L.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 x=E(e=>{let i=L.current;if(!i)return;let n=Pe(i),f=a.filter(u=>u.propsRef.current.disabled===!1).map(u=>u.element.current);switch(e.key){case D.Enter:Ee(e.currentTarget);break;case D.ArrowLeft:case D.ArrowUp:if(e.preventDefault(),e.stopPropagation(),Y(f,I.Previous|I.WrapAround)===Z.Success){let g=a.find(K=>K.element.current===(n==null?void 0:n.activeElement));g&&d(g.propsRef.current.value)}break;case D.ArrowRight:case D.ArrowDown:if(e.preventDefault(),e.stopPropagation(),Y(f,I.Next|I.WrapAround)===Z.Success){let g=a.find(K=>K.element.current===(n==null?void 0:n.activeElement));g&&d(g.propsRef.current.value)}break;case D.Space:{e.preventDefault(),e.stopPropagation();let u=a.find(g=>g.element.current===(n==null?void 0:n.activeElement));u&&d(u.propsRef.current.value)}break}}),c=E(e=>(h({type:0,...e}),()=>h({type:1,id:e.id}))),_=A(()=>({value:l,firstOption:b,containsCheckedOption:F,disabled:C,compare:y,...P}),[l,b,F,C,y,P]),ie=A(()=>({registerOption:c,change:d}),[c,d]),ae={ref:W,id:p,role:"radiogroup","aria-labelledby":N,"aria-describedby":k,onKeyDown:x},pe=A(()=>({value:l}),[l]),w=j(null),le=Ge();return ue(()=>{w.current&&v!==void 0&&le.addEventListener(w.current,"reset",()=>{d(v)})},[w,d]),O.createElement(U,{name:"RadioGroup.Description"},O.createElement(R,{name:"RadioGroup.Label"},O.createElement($.Provider,{value:ie},O.createElement(B.Provider,{value:_},m!=null&&l!=null&&ve({[m]:l}).map(([e,i],n)=>O.createElement(ge,{features:Oe.Hidden,ref:n===0?f=>{var u;w.current=(u=f==null?void 0:f.closest("form"))!=null?u:null}:void 0,...de({key:e,as:"input",type:"radio",checked:i!=null,hidden:!0,readOnly:!0,form:S,name:e,value:i})})),q({ourProps:ae,theirProps:H,slot:pe,defaultTag:he,name:"RadioGroup"})))))}var xe=(t=>(t[t.Empty=1]="Empty",t[t.Active=2]="Active",t))(xe||{});let _e="div";function we(r,o){var x;let t=Q(),{id:p=`headlessui-radiogroup-option-${t}`,value:T,disabled:v=!1,...S}=r,m=j(null),M=te(m,o),[G,C]=z(),[H,y]=ee(),{addFlag:P,removeFlag:h,hasFlag:a}=me(1),N=De({value:T,disabled:v}),R=oe("RadioGroup.Option"),k=ne("RadioGroup.Option");fe(()=>k.registerOption({id:p,element:m,propsRef:N}),[p,k,m,r]);let U=E(c=>{var _;if(re(c.currentTarget))return c.preventDefault();k.change(T)&&(P(2),(_=m.current)==null||_.focus())}),L=E(c=>{if(re(c.currentTarget))return c.preventDefault();P(2)}),W=E(()=>h(2)),l=((x=R.firstOption)==null?void 0:x.id)===p,s=R.disabled||v,b=R.compare(R.value,T),F={ref:M,id:p,role:"radio","aria-checked":b?"true":"false","aria-labelledby":G,"aria-describedby":H,"aria-disabled":s?!0:void 0,tabIndex:(()=>s?-1:b||!R.containsCheckedOption&&l?0:-1)(),onClick:s?void 0:U,onFocus:s?void 0:L,onBlur:s?void 0:W},d=A(()=>({checked:b,disabled:s,active:a(2)}),[b,s,a]);return O.createElement(y,{name:"RadioGroup.Description"},O.createElement(C,{name:"RadioGroup.Label"},q({ourProps:F,theirProps:S,slot:d,defaultTag:_e,name:"RadioGroup.Option"})))}let Ie=X(Fe),Se=X(we),yt=Object.assign(Ie,{Option:Se,Label:ye,Description:Re});export{yt as RadioGroup};