@choice-ui/radio 0.0.5 → 0.0.6

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/index.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import * as react from 'react';
2
2
  import { HTMLProps, ReactNode } from 'react';
3
3
 
4
+ interface RadioIconProps extends Omit<HTMLProps<HTMLDivElement>, "children"> {
5
+ children?: ReactNode | ((props: {
6
+ value?: boolean;
7
+ }) => ReactNode);
8
+ }
9
+ declare const RadioIcon: react.MemoExoticComponent<react.ForwardRefExoticComponent<Omit<RadioIconProps, "ref"> & react.RefAttributes<HTMLDivElement>>>;
10
+
4
11
  interface RadioLabelProps extends Omit<HTMLProps<HTMLLabelElement>, "htmlFor" | "id" | "disabled"> {
5
12
  children: ReactNode;
6
13
  }
@@ -19,6 +26,7 @@ interface RadioType {
19
26
  (props: RadioProps & {
20
27
  ref?: React.Ref<HTMLInputElement>;
21
28
  }): JSX.Element;
29
+ Icon: typeof RadioIcon;
22
30
  Label: typeof RadioLabel;
23
31
  displayName?: string;
24
32
  }
@@ -50,4 +58,4 @@ interface RadioGroupType {
50
58
  }
51
59
  declare const RadioGroup: RadioGroupType;
52
60
 
53
- export { Radio, RadioGroup, type RadioGroupProps, type RadioProps };
61
+ export { Radio, RadioGroup, type RadioGroupProps, type RadioIconProps, type RadioLabelProps, type RadioProps };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { tcv, tcx } from '@choice-ui/shared';
2
2
  import { Dot } from '@choiceform/icons-react';
3
- import { createContext, memo, forwardRef, useId, useMemo, useContext } from 'react';
3
+ import { createContext, memo, forwardRef, useId, Children, isValidElement, useMemo, useContext } from 'react';
4
4
  import { useEventCallback } from 'usehooks-ts';
5
5
  import { jsx, jsxs } from 'react/jsx-runtime';
6
6
 
@@ -147,6 +147,38 @@ var radioTv = tcv({
147
147
  focused: false
148
148
  }
149
149
  });
150
+ var RadioIcon = memo(
151
+ forwardRef(function RadioIcon2(props, ref) {
152
+ const { className, children, ...rest } = props;
153
+ const { value, disabled, variant } = useRadioContext();
154
+ const tv = radioTv({
155
+ type: "radio",
156
+ variant,
157
+ disabled,
158
+ checked: value
159
+ });
160
+ const renderIcon = () => {
161
+ if (typeof children === "function") {
162
+ return children({ value });
163
+ }
164
+ if (children !== void 0) {
165
+ return children;
166
+ }
167
+ return value ? /* @__PURE__ */ jsx(Dot, {}) : null;
168
+ };
169
+ return /* @__PURE__ */ jsx(
170
+ "div",
171
+ {
172
+ ref,
173
+ className: tcx(tv.box(), className),
174
+ "data-active": value,
175
+ ...rest,
176
+ children: renderIcon()
177
+ }
178
+ );
179
+ })
180
+ );
181
+ RadioIcon.displayName = "Radio.Icon";
150
182
  var RadioLabel = memo(
151
183
  forwardRef(function RadioLabel2(props, ref) {
152
184
  const { children, className, ...rest } = props;
@@ -203,13 +235,21 @@ var RadioBase = forwardRef(function Radio(props, ref) {
203
235
  }
204
236
  onKeyDown?.(e);
205
237
  });
238
+ const isIconElement = (child) => isValidElement(child) && (child.type === RadioIcon || child.type?.displayName === "Radio.Icon");
239
+ const childArray = Children.toArray(children);
240
+ const iconChild = childArray.find(isIconElement);
241
+ const otherChildren = childArray.filter((child) => !isIconElement(child));
206
242
  const renderChildren = () => {
207
- if (typeof children === "string" || typeof children === "number") {
208
- return /* @__PURE__ */ jsx(RadioLabel, { children });
243
+ if (otherChildren.length === 1) {
244
+ const child = otherChildren[0];
245
+ if (typeof child === "string" || typeof child === "number") {
246
+ return /* @__PURE__ */ jsx(RadioLabel, { children: child });
247
+ }
209
248
  }
210
- return children;
249
+ return otherChildren;
211
250
  };
212
- return /* @__PURE__ */ jsx(RadioContext.Provider, { value: { id, descriptionId, disabled }, children: /* @__PURE__ */ jsxs("div", { className: tcx(tv.root(), className), children: [
251
+ const renderDefaultIcon = () => /* @__PURE__ */ jsx("div", { className: tv.box(), children: value && /* @__PURE__ */ jsx(Dot, {}) });
252
+ return /* @__PURE__ */ jsx(RadioContext.Provider, { value: { id, descriptionId, disabled, value, variant }, children: /* @__PURE__ */ jsxs("div", { className: tcx(tv.root(), className), children: [
213
253
  /* @__PURE__ */ jsxs("div", { className: "pointer-events-none relative", children: [
214
254
  /* @__PURE__ */ jsx(
215
255
  "input",
@@ -231,13 +271,14 @@ var RadioBase = forwardRef(function Radio(props, ref) {
231
271
  ...rest
232
272
  }
233
273
  ),
234
- /* @__PURE__ */ jsx("div", { className: tv.box(), children: value && /* @__PURE__ */ jsx(Dot, {}) })
274
+ iconChild ?? renderDefaultIcon()
235
275
  ] }),
236
276
  renderChildren()
237
277
  ] }) });
238
278
  });
239
279
  var MemoizedRadio = memo(RadioBase);
240
280
  var Radio2 = MemoizedRadio;
281
+ Radio2.Icon = RadioIcon;
241
282
  Radio2.Label = RadioLabel;
242
283
  Radio2.displayName = "Radio";
243
284
  var RadioGroupItem = memo(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@choice-ui/radio",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "A radio button component for single selection from a group of mutually exclusive options",
5
5
  "sideEffects": false,
6
6
  "type": "module",