UNPKG

@react-beauty/ui-radio

Version:

ui-radio

183 lines (182 loc) 4.32 kB
import { jsx as i, jsxs as w } from "react/jsx-runtime"; import { createContext as E, useContext as C, useState as H, useId as D, forwardRef as u } from "react"; import { styled as d } from "@linaria/react"; const P = /* @__PURE__ */ d("div")({ name: "ElRadioContainer", class: "react-beauty-el-radio-container", propsAsIs: !1 }), T = /* @__PURE__ */ d("label")({ name: "ElRadioLabel", class: "react-beauty-el-radio-label", propsAsIs: !1 }), j = /* @__PURE__ */ d("div")({ name: "ElRadioCircle", class: "react-beauty-el-radio-circle", propsAsIs: !1 }), k = /* @__PURE__ */ d("input")({ name: "ElRadioInput", class: "react-beauty-el-radio-input", propsAsIs: !1 }), F = /* @__PURE__ */ d("span")({ name: "ElRadioHelperText", class: "react-beauty-el-radio-helper-text", propsAsIs: !1 }), G = /* @__PURE__ */ d("div")({ name: "ElRadioWrapper", class: "react-beauty-el-radio-wrapper", propsAsIs: !1 }), L = /* @__PURE__ */ d("div")({ name: "ElRadioGroupContainer", class: "react-beauty-el-radio-group-container", propsAsIs: !1 }), v = E( void 0 ), x = () => C(v), O = ({ children: t, name: s, selectedValue: a, onChange: o, isDisabled: r, hasError: n }) => /* @__PURE__ */ i( v.Provider, { value: { name: s, selectedValue: a, onChange: o, isDisabled: r, hasError: n }, children: t } ), g = E(void 0), A = () => { const t = C(g); if (!t) throw new Error("useRadio must be used within a RadioProvider"); return t; }, W = ({ children: t, hasError: s = !1, isDisabled: a = !1, value: o }) => { const [r, n] = H(!1), l = D(), e = x(), p = (e == null ? void 0 : e.hasError) || s, c = (e == null ? void 0 : e.isDisabled) || a, b = e ? e.selectedValue === o : !1, f = (e == null ? void 0 : e.name) || "", R = (e == null ? void 0 : e.onChange) || (() => { }), h = () => ({ "data-error": p, "data-disabled": c, "data-checked": b, "data-is-focused": r, "data-is-blurred": !r }); return /* @__PURE__ */ i( g.Provider, { value: { hasError: p, isDisabled: c, isChecked: b, isFocused: r, radioId: l, value: o, name: f, onChange: R, setIsFocused: n, getAttributes: h }, children: t } ); }, $ = u( ({ hasError: t = !1, isDisabled: s = !1, value: a, children: o, ...r }, n) => /* @__PURE__ */ i(W, { hasError: t, isDisabled: s, value: a, children: /* @__PURE__ */ i(P, { ref: n, ...r, children: o }) }) ), S = u(({ id: t, onFocus: s, onBlur: a, ...o }, r) => { const { isDisabled: n, isChecked: l, radioId: e, name: p, value: c, onChange: b, getAttributes: f, setIsFocused: R } = A(), h = (I) => { R(!0), s == null || s(I); }, m = (I) => { R(!1), a == null || a(I); }, y = () => { b(c); }; return /* @__PURE__ */ w(G, { ...f(), children: [ /* @__PURE__ */ i( k, { ref: r, id: t || e, name: p, value: c, type: "radio", checked: l, disabled: n, onFocus: h, onBlur: m, onChange: y, ...o } ), /* @__PURE__ */ i(j, { "data-disabled": n }) ] }); }), V = u(({ children: t, htmlFor: s, ...a }, o) => { const { radioId: r, getAttributes: n } = A(); return /* @__PURE__ */ i( T, { ref: o, htmlFor: s || r, ...a, ...n(), children: t } ); }), q = u((t, s) => { const a = x(); if (!a) throw new Error("RadioGroupHelperText must be used within a RadioGroup"); const o = { "data-error": a.hasError, "data-disabled": a.isDisabled }; return /* @__PURE__ */ i(F, { ref: s, ...t, ...o }); }), z = Object.assign( u( ({ children: t, name: s, selectedValue: a, onValueChange: o, isDisabled: r = !1, hasError: n = !1, ...l }, e) => /* @__PURE__ */ i( O, { name: s, selectedValue: a, onChange: o, isDisabled: r, hasError: n, children: /* @__PURE__ */ i(L, { ref: e, ...l, children: t }) } ) ), { HelperText: q } ), J = { Field: S, Label: V }, Q = Object.assign( $, J ), U = z; export { U as RadioGroup, Q as RadioInput };