-
-
Notifications
You must be signed in to change notification settings - Fork 53
Select primitive t1 #1117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select primitive t1 #1117
Changes from all commits
470a0a2
d0baaf4
6e23a8f
fc0afb7
8d9fba6
dccbe44
da24529
d7c7bc3
34769e7
f86c071
112e855
e438333
8ed6369
90f8640
9a1929e
43e677f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 'use client'; | ||
| import SelectContent from './fragments/SelectContent'; | ||
| import SelectItem from './fragments/SelectItem'; | ||
| import SelectTrigger from './fragments/SelectTrigger'; | ||
| import SelectRoot from './fragments/SelectRoot'; | ||
| import SelectIndicator from './fragments/SelectIndicator'; | ||
| import SelectPortal from './fragments/SelectPortal'; | ||
|
|
||
| const Select = () => { | ||
| console.warn('Direct usage of Select is not supported. Please use Select.Root, Select.Content, etc. instead.'); | ||
| return null; | ||
| }; | ||
|
|
||
| Select.Root = SelectRoot; | ||
| Select.Content = SelectContent; | ||
| Select.Item = SelectItem; | ||
| Select.Trigger = SelectTrigger; | ||
| Select.Portal = SelectPortal; | ||
| Select.Indicator = SelectIndicator; | ||
|
|
||
| export default Select; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import React from 'react'; | ||
|
|
||
| interface SelectRootContextType { | ||
| rootClass: string; | ||
| } | ||
|
|
||
| export const SelectRootContext = React.createContext<SelectRootContextType>({ | ||
| rootClass: '' | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'use client'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React, { useEffect, useRef, useContext } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import SelectPrimitive from '~/core/primitives/Select/Select'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { SelectRootContext } from '../contexts/SelectRootContext'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function SelectContent({ customRootClass, children, position = 'popper', ...props }: any) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve type safety by defining proper prop types. The component uses +interface SelectContentProps {
+ customRootClass?: string;
+ children: React.ReactNode;
+ position?: 'popper' | 'item-aligned';
+ [key: string]: any; // for additional props to spread
+}
+
-function SelectContent({ customRootClass, children, position = 'popper', ...props }: any) {
+function SelectContent({ customRootClass, children, position = 'popper', ...props }: SelectContentProps) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { rootClass } = useContext(SelectRootContext); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <SelectPrimitive.Content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className={`${rootClass}-content`} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| position={position} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data-position={position} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {...props} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {children} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </SelectPrimitive.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+6
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Clarify the purpose of the The If function SelectContent({ customRootClass, children, position = 'popper', ...props }: any) {
const { rootClass } = useContext(SelectRootContext);
+ const effectiveRootClass = customRootClass || rootClass;
return (
<SelectPrimitive.Content
- className={`${rootClass}-content`}
+ className={`${effectiveRootClass}-content`}
position={position}
data-position={position}
{...props}
>
{children}
</SelectPrimitive.Content>
);
}Alternatively, if it's not needed, remove it from the props: -function SelectContent({ customRootClass, children, position = 'popper', ...props }: any) {
+function SelectContent({ children, position = 'popper', ...props }: any) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default SelectContent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| 'use client'; | ||
| import React, { useContext } from 'react'; | ||
| import { SelectRootContext } from '../contexts/SelectRootContext'; | ||
|
|
||
| function SelectIndicator() { | ||
| const { rootClass } = useContext(SelectRootContext); | ||
| return ( | ||
| <div className={`${rootClass}-item-indicator`}> | ||
| <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
| <path d="M13.3332 4L5.99984 11.3333L2.6665 8" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/> | ||
| </svg> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default SelectIndicator; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||||||||||||||||||
| 'use client'; | ||||||||||||||||||||||||||||||
| import React, { useContext } from 'react'; | ||||||||||||||||||||||||||||||
| import SelectPrimitive from '~/core/primitives/Select/Select'; | ||||||||||||||||||||||||||||||
| import { SelectRootContext } from '../contexts/SelectRootContext'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| function SelectItem({ customRootClass, children, value, disabled, ...props }: any) { | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace Using -function SelectItem({ customRootClass, children, value, disabled, ...props }: any) {
+interface SelectItemProps {
+ customRootClass?: string;
+ children: React.ReactNode;
+ value: string;
+ disabled?: boolean;
+ [key: string]: any; // for additional props
+}
+
+function SelectItem({ customRootClass, children, value, disabled, ...props }: SelectItemProps) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| const { rootClass } = useContext(SelectRootContext); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||
| <SelectPrimitive.Item | ||||||||||||||||||||||||||||||
| className={`${rootClass}-item`} | ||||||||||||||||||||||||||||||
| value={value} | ||||||||||||||||||||||||||||||
| disabled={disabled} | ||||||||||||||||||||||||||||||
| data-disabled={disabled ? '' : undefined} | ||||||||||||||||||||||||||||||
| role="option" | ||||||||||||||||||||||||||||||
| aria-disabled={disabled ? 'true' : undefined} | ||||||||||||||||||||||||||||||
| {...props} | ||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||
| <span className={`${rootClass}-text`}>{children}</span> | ||||||||||||||||||||||||||||||
| </SelectPrimitive.Item> | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export default SelectItem; | ||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use client'; | ||
| import React, { useContext } from 'react'; | ||
| import SelectPrimitive from '~/core/primitives/Select/Select'; | ||
|
|
||
| export type SelectPortalProps = { | ||
| children: React.ReactNode; | ||
| }; | ||
|
|
||
| const SelectPortal = ({ children }: SelectPortalProps) => { | ||
| const rootElement = document.querySelector('#rad-ui-theme-container') || document.body as HTMLElement | null; | ||
|
|
||
| return ( | ||
| <SelectPrimitive.Portal> | ||
| {children} | ||
| </SelectPrimitive.Portal> | ||
|
|
||
| ); | ||
| }; | ||
|
|
||
| export default SelectPortal; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import React, { useState, useCallback } from 'react'; | ||
| import SelectPrimitive from '~/core/primitives/Select/Select'; | ||
| import { customClassSwitcher } from '~/core/customClassSwitcher'; | ||
| import { SelectRootContext } from '../contexts/SelectRootContext'; | ||
|
|
||
| const COMPONENT_NAME = 'Select'; | ||
|
|
||
| function SelectRoot({ customRootClass, children, defaultValue, value, onValueChange, ...props }: any) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve type safety by replacing 'any' with proper types. Using +type SelectRootProps = {
+ customRootClass?: string;
+ children?: React.ReactNode;
+ defaultValue?: string;
+ value?: string;
+ onValueChange?: (value: string) => void;
+} & React.ComponentPropsWithoutRef<'div'>;
-function SelectRoot({ customRootClass, children, defaultValue, value, onValueChange, ...props }: any) {
+function SelectRoot({ customRootClass, children, defaultValue, value, onValueChange, ...props }: SelectRootProps) {🤖 Prompt for AI Agents |
||
| const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); | ||
|
|
||
| return ( | ||
| <SelectRootContext.Provider value={{ rootClass }}> | ||
| <SelectPrimitive.Root | ||
| className={`${rootClass}-root`} | ||
| defaultValue={defaultValue} | ||
| value={value} | ||
| onValueChange={onValueChange} | ||
| {...props} | ||
| > | ||
| {children} | ||
| </SelectPrimitive.Root> | ||
| </SelectRootContext.Provider> | ||
|
|
||
| ); | ||
| } | ||
|
|
||
| export default SelectRoot; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| 'use client'; | ||
| import React, { useContext } from 'react'; | ||
| import SelectPrimitive from '~/core/primitives/Select/Select'; | ||
|
|
||
| import { SelectRootContext } from '../contexts/SelectRootContext'; | ||
|
|
||
| function SelectTrigger({ customRootClass, children, disabled, placeholder, ...props }: any) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace Similar to SelectItem, this component should use a proper TypeScript interface to maintain type safety across the Select component system. -function SelectTrigger({ customRootClass, children, disabled, placeholder, ...props }: any) {
+interface SelectTriggerProps {
+ customRootClass?: string;
+ children: React.ReactNode;
+ disabled?: boolean;
+ placeholder?: string;
+ [key: string]: any;
+}
+
+function SelectTrigger({ customRootClass, children, disabled, placeholder, ...props }: SelectTriggerProps) {🤖 Prompt for AI Agents |
||
| const { rootClass } = useContext(SelectRootContext); | ||
| const triggerRef = React.useRef<HTMLDivElement>(null); | ||
| console.log(triggerRef); | ||
| return ( | ||
| <SelectPrimitive.Trigger | ||
| className={`${rootClass}-trigger`} | ||
| aria-disabled={disabled ? 'true' : undefined} | ||
| data-placeholder={placeholder ? '' : undefined} | ||
| ref={triggerRef} | ||
| {...props} | ||
| > | ||
|
|
||
| {children} | ||
|
|
||
| </SelectPrimitive.Trigger> | ||
| ); | ||
| } | ||
|
|
||
| export default SelectTrigger; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,133 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import Select from '../Select'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import SelectPrimitive from '~/core/primitives/Select/Select'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title: 'WIP/Select', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| component: Select | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const Basic = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="w-[240px]"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span>Select an option</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Portal> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="apple"> <Select.Indicator />Apple</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="banana"> <Select.Indicator />Banana</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="orange"> <Select.Indicator />Orange</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="grape"> <Select.Indicator />Grape</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Portal> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const WithDisabledOptions = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="w-[240px]"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span>Select a fruit</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="apple">Apple</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="banana">Banana</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="orange" disabled>Orange (Sold Out)</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="grape">Grape</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="pear" disabled>Pear (Sold Out)</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const WithInitialValue = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="w-[240px]"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Root defaultValue="react"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span>Favorite Framework</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="react">React</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="angular">Angular</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="vue">Vue</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="svelte">Svelte</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const MultipleSelects = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="flex flex-col gap-4"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="w-[240px]"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span>Select a color</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="red">Red</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="green">Green</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="blue">Blue</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="yellow">Yellow</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="w-[240px]"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span>Select a size</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="sm">Small</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="md">Medium</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="lg">Large</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value="xl">Extra Large</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const ControlledExample = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [value, setValue] = React.useState(''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="w-[240px]"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Root defaultValue="option1" value={value} onValueChange={setValue}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| helo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Trigger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value='option1'>Option 1</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value='option2'>Option 2</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Select.Item value='option3'>Option 3</Select.Item> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Content> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Select.Root> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className='mt-32'> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <p className='text-gray-950'>Value: {value}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </SandboxEditor> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+111
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix conflicting props and state in controlled example. The controlled example has several issues:
export const ControlledExample = () => {
- const [value, setValue] = React.useState('');
+ const [value, setValue] = React.useState('option1');
return (
<SandboxEditor>
<div className="w-[240px]">
- <Select.Root defaultValue="option1" value={value} onValueChange={setValue}>
+ <Select.Root value={value} onValueChange={setValue}>
<Select.Trigger>
- helo
+ <span>Select an option</span>
</Select.Trigger>📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import SelectPrimitiveContent from './fragments/SelectPrimitiveContent'; | ||
| import SelectPrimitiveItem from './fragments/SelectPrimitiveItem'; | ||
| import SelectPrimitiveTrigger from './fragments/SelectPrimitiveTrigger'; | ||
| import SelectPrimitiveRoot from './fragments/SelectPrimitiveRoot'; | ||
| import SelectPrimitivePortal from './fragments/SelectPrimitivePortal'; | ||
| import SelectPrimitiveOverlay from './fragments/SelectPrimitiveOverlay'; | ||
|
|
||
| const SelectPrimitive = () => { | ||
| console.warn('Direct usage of Select is not supported. Please use Select.Root, Select.Content, etc. instead.'); | ||
| return null; | ||
| }; | ||
|
|
||
| SelectPrimitive.Root = SelectPrimitiveRoot; | ||
| SelectPrimitive.Content = SelectPrimitiveContent; | ||
| SelectPrimitive.Portal = SelectPrimitivePortal; | ||
| SelectPrimitive.Overlay = SelectPrimitiveOverlay; | ||
| SelectPrimitive.Item = SelectPrimitiveItem; | ||
| SelectPrimitive.Trigger = SelectPrimitiveTrigger; | ||
|
|
||
| export default SelectPrimitive; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { createContext } from 'react'; | ||
|
|
||
| export type SelectPrimitiveContextType = { | ||
| isOpen: boolean, | ||
| setIsOpen: React.Dispatch<React.SetStateAction<boolean>>, | ||
| selectedValue: string, | ||
| setSelectedValue: React.Dispatch<React.SetStateAction<string>> | ||
| handleSelect: (value: string) => void | ||
| refs: { | ||
| reference: React.RefObject<any>; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve type safety by replacing Several properties use +import type { FloatingContext, ReferenceType, UseFloatingReturn } from '@floating-ui/react';
export type SelectPrimitiveContextType = {
isOpen: boolean,
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>,
selectedValue: string,
setSelectedValue: React.Dispatch<React.SetStateAction<string>>
handleSelect: (value: string) => void
refs: {
- reference: React.RefObject<any>;
- floating: React.RefObject<any>;
- setReference: (node: any) => void;
- setFloating: (node: any) => void;
+ reference: React.RefObject<ReferenceType>;
+ floating: React.RefObject<HTMLElement>;
+ setReference: (node: ReferenceType | null) => void;
+ setFloating: (node: HTMLElement | null) => void;
};
floatingStyles: React.CSSProperties;
- floatingContext: any;
- getReferenceProps: () => any;
- getFloatingProps: () => any;
- getItemProps: (userProps?: any) => any;
+ floatingContext: FloatingContext;
+ getReferenceProps: () => React.HTMLProps<Element>;
+ getFloatingProps: () => React.HTMLProps<HTMLElement>;
+ getItemProps: (userProps?: React.HTMLProps<HTMLElement>) => React.HTMLProps<HTMLElement>;
}Also applies to: 16-19 🤖 Prompt for AI Agents |
||
| floating: React.RefObject<any>; | ||
| setReference: (node: any) => void; | ||
| setFloating: (node: any) => void; | ||
| }; | ||
| floatingStyles: React.CSSProperties; | ||
| floatingContext: any; | ||
| getReferenceProps: () => any; | ||
| getFloatingProps: () => any; | ||
| getItemProps: (userProps?: any) => any; | ||
| } | ||
|
|
||
| export const SelectPrimitiveContext = createContext<SelectPrimitiveContextType>({} as SelectPrimitiveContextType); | ||
Uh oh!
There was an error while loading. Please reload this page.