1"use client";
2
3import { cva, type VariantProps } from "class-variance-authority";
4import {
5 Direction as DirectionPrimitive,
6 Slot as SlotPrimitive,
7} from "radix-ui";
8import * as React from "react";
9import { cn } from "@/lib/utils";
10import { Input } from "@/registry/bases/radix/ui/input";
11
12const ROOT_NAME = "SegmentedInput";
13const ITEM_NAME = "SegmentedInputItem";
14
15type Direction = "ltr" | "rtl";
16type Orientation = "horizontal" | "vertical";
17type Size = "default" | "sm" | "lg";
18type Position = "isolated" | "first" | "middle" | "last";
19
20interface SegmentedInputContextValue {
21 dir?: Direction;
22 orientation?: Orientation;
23 size?: Size;
24 disabled?: boolean;
25 invalid?: boolean;
26 required?: boolean;
27}
28
29const SegmentedInputContext =
30 React.createContext<SegmentedInputContextValue | null>(null);
31
32function useSegmentedInputContext(consumerName: string) {
33 const context = React.useContext(SegmentedInputContext);
34 if (!context) {
35 throw new Error(`\`${consumerName}\` must be used within \`${ROOT_NAME}\``);
36 }
37 return context;
38}
39
40interface SegmentedInputProps extends React.ComponentProps<"div"> {
41 dir?: Direction;
42 orientation?: Orientation;
43 size?: Size;
44 asChild?: boolean;
45 disabled?: boolean;
46 invalid?: boolean;
47 required?: boolean;
48}
49
50function SegmentedInput(props: SegmentedInputProps) {
51 const {
52 size = "default",
53 dir: dirProp,
54 orientation = "horizontal",
55 children,
56 className,
57 asChild,
58 disabled,
59 invalid,
60 required,
61 ...rootProps
62 } = props;
63
64 const dir = DirectionPrimitive.useDirection(dirProp);
65
66 const contextValue = React.useMemo<SegmentedInputContextValue>(
67 () => ({
68 dir,
69 orientation,
70 size,
71 disabled,
72 invalid,
73 required,
74 }),
75 [dir, orientation, size, disabled, invalid, required],
76 );
77
78 const childrenArray = React.Children.toArray(children);
79 const childrenCount = childrenArray.length;
80
81 const segmentedInputItems = React.Children.map(children, (child, index) => {
82 if (React.isValidElement<SegmentedInputItemProps>(child)) {
83 if (!child.props.position) {
84 let position: Position;
85
86 if (childrenCount === 1) {
87 position = "isolated";
88 } else if (index === 0) {
89 position = "first";
90 } else if (index === childrenCount - 1) {
91 position = "last";
92 } else {
93 position = "middle";
94 }
95
96 return React.cloneElement(child, { position });
97 }
98 }
99 return child;
100 });
101
102 const RootPrimitive = asChild ? SlotPrimitive.Slot : "div";
103
104 return (
105 <SegmentedInputContext.Provider value={contextValue}>
106 <RootPrimitive
107 role="group"
108 aria-orientation={orientation}
109 data-slot="segmented-input"
110 data-orientation={orientation}
111 data-disabled={disabled ? "" : undefined}
112 data-invalid={invalid ? "" : undefined}
113 data-required={required ? "" : undefined}
114 dir={dir}
115 {...rootProps}
116 className={cn(
117 "flex",
118 orientation === "horizontal" ? "flex-row" : "flex-col",
119 className,
120 )}
121 >
122 {segmentedInputItems}
123 </RootPrimitive>
124 </SegmentedInputContext.Provider>
125 );
126}
127
128const segmentedInputItemVariants = cva("", {
129 variants: {
130 position: {
131 isolated: "",
132 first: "rounded-e-none",
133 middle: "-ms-px rounded-none border-l-0",
134 last: "-ms-px rounded-s-none border-l-0",
135 },
136 orientation: {
137 horizontal: "",
138 vertical: "",
139 },
140 size: {
141 sm: "h-8 px-2 text-xs",
142 default: "h-9 px-3",
143 lg: "h-11 px-4",
144 },
145 },
146 compoundVariants: [
147 {
148 position: "first",
149 orientation: "vertical",
150 class: "ms-0 rounded-e-md rounded-b-none border-l",
151 },
152 {
153 position: "middle",
154 orientation: "vertical",
155 class: "ms-0 -mt-px rounded-none border-t-0 border-l",
156 },
157 {
158 position: "last",
159 orientation: "vertical",
160 class: "ms-0 -mt-px rounded-s-md rounded-t-none border-t-0 border-l",
161 },
162 ],
163 defaultVariants: {
164 position: "isolated",
165 orientation: "horizontal",
166 size: "default",
167 },
168});
169
170interface SegmentedInputItemProps
171 extends React.ComponentProps<"input">,
172 Omit<VariantProps<typeof segmentedInputItemVariants>, "size"> {
173 asChild?: boolean;
174}
175
176function SegmentedInputItem(props: SegmentedInputItemProps) {
177 const { asChild, className, position, disabled, required, ...inputProps } =
178 props;
179 const context = useSegmentedInputContext(ITEM_NAME);
180
181 const isDisabled = disabled ?? context.disabled;
182 const isRequired = required ?? context.required;
183
184 const ItemPrimitive = asChild ? SlotPrimitive.Slot : Input;
185
186 return (
187 <ItemPrimitive
188 aria-invalid={context.invalid}
189 aria-required={isRequired}
190 data-disabled={isDisabled ? "" : undefined}
191 data-invalid={context.invalid ? "" : undefined}
192 data-orientation={context.orientation}
193 data-position={position}
194 data-required={isRequired ? "" : undefined}
195 data-slot="segmented-input-item"
196 disabled={isDisabled}
197 required={isRequired}
198 {...inputProps}
199 className={cn(
200 segmentedInputItemVariants({
201 position,
202 orientation: context.orientation,
203 size: context.size,
204 className,
205 }),
206 )}
207 />
208 );
209}
210
211export { SegmentedInput, SegmentedInputItem, type SegmentedInputProps };
212