Skip to content

Commit

Permalink
fix(select): improve event handlers
Browse files Browse the repository at this point in the history
And the documentation
  • Loading branch information
clementprevot committed Jul 10, 2023
1 parent 131e33c commit a3be64d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/fractal/src/components/Select/Select.recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const selectTrigger: ReturnType<typeof defineRecipe> = defineRecipe({
'& > span[aria-hidden], & > span[aria-hidden] > svg': {
height: '100%',
},
'&[data-placeholder] > span:not([aria-hidden])': {
'&[data-placeholder] > div > span': {
color: `var(--color-text-select-placeholder)`,
fontStyle: 'var(--style-text-placeholder)',
},
Expand Down
5 changes: 3 additions & 2 deletions packages/fractal/src/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ const meta = {
},
},
},
onOpenChange: { control: false },
onValueChange: { control: false },
onClose: { control: false },
onOpen: { control: false },
onSelect: { control: false },
},
args: {
autoFocus: false,
Expand Down
20 changes: 14 additions & 6 deletions packages/fractal/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ export default function Select({
id = uniqueId('fractal-select-'),
label,
name,
onOpenChange,
onValueChange,
onClose,
onOpen,
onSelect,
open,
placeholder,
required = false,
Expand Down Expand Up @@ -82,16 +83,23 @@ export default function Select({
required={required}
{...(open !== undefined ? { open } : {})}
{...(value !== undefined ? { value } : {})}
{...(isFunction(onValueChange)
{...(isFunction(onSelect)
? {
onValueChange: (newValue: string) => onValueChange(newValue),
onValueChange: (newValue: string) => onSelect(newValue),
}
: {})}
{...(isFunction(onOpenChange)
{...(isFunction(onOpen) || isFunction(onClose)
? {
onOpenChange: (isOpened: boolean) => {
setIsOpen(isOpened)
onOpenChange(isOpened)

if (isOpened) {
onOpen?.()

return
}

onClose?.()
},
}
: {})}
Expand Down
11 changes: 7 additions & 4 deletions packages/fractal/src/components/Select/Select.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
} from '@radix-ui/react-select'
import type { AllHTMLAttributes, ReactNode } from 'react'

export interface SelectProps extends AllHTMLAttributes<HTMLSelectElement> {
export interface SelectProps
extends Omit<AllHTMLAttributes<HTMLSelectElement>, 'onSelect'> {
/** Indicates if autocompletion is possible in this select. */
autoComplete?: string
/** Indicates if the select must be opened on render. */
Expand Down Expand Up @@ -65,10 +66,12 @@ export interface SelectProps extends AllHTMLAttributes<HTMLSelectElement> {
* If none is given, the ID (provided or auto-generated) will be used.
*/
name?: string
/** Event handler called when the select dropdown is closed. */
onClose?: () => void
/** Event handler called when the select dropdown is opened. */
onOpenChange?: (isOpen: boolean) => void
/** Event handler called when the selected value is changed. */
onValueChange?: (newValue: string) => void
onOpen?: () => void
/** Event handler called when a value is selected. */
onSelect?: (newValue: string) => void
/**
* The controlled open state of the select.
*
Expand Down

0 comments on commit a3be64d

Please sign in to comment.