-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OPHJOD-281: Remove classnames and simplify dropdown menu
- Loading branch information
Showing
8 changed files
with
111 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,104 @@ | ||
import { useId } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
export interface DropdownMenuOptionsData<T extends string = string> { | ||
value: T; | ||
export interface DropdownMenuSelectProps<T extends DropdownMenuOptionsData> { | ||
/** Unique id for the component */ | ||
id: string; | ||
/** Component is disabled for user interaction */ | ||
disabled?: boolean; | ||
/** Aria label for screenreaders */ | ||
ariaLabel?: string; | ||
/** Hide label. Still available for screenreaders */ | ||
hideLabel?: boolean; | ||
/** Options for component */ | ||
options: T[]; | ||
/** Default value to be selected initially */ | ||
defaultValue?: T; | ||
/** Controlled mode */ | ||
selected?: T; | ||
/** Callback on selection change */ | ||
onChange?: (value: T) => void; | ||
} | ||
|
||
const DropdownMenuSelect = <T extends DropdownMenuOptionsData = DropdownMenuOptionsData>({ | ||
id, | ||
disabled, | ||
ariaLabel, | ||
onChange, | ||
defaultValue, | ||
options, | ||
}: DropdownMenuSelectProps<T>) => ( | ||
<select | ||
id={id} | ||
disabled={disabled} | ||
aria-label={ariaLabel} | ||
className="min-w-[120px] justify-self-end rounded-lg border border-jod-dark bg-jod-white p-2 hover:bg-purple-100 focus:outline-none focus:ring focus:ring-purple-500 disabled:border-gray-500 disabled:bg-gray-200 disabled:text-gray-500 disabled:hover:bg-gray-200" | ||
onChange={(event: React.ChangeEvent<HTMLSelectElement>) => { | ||
if (onChange) { | ||
onChange(options.find((option) => option.value === event.target.value)!); | ||
} | ||
}} | ||
defaultValue={defaultValue?.value} | ||
> | ||
{options.map((option, index) => ( | ||
<option key={index} value={option.value}> | ||
{option.label} | ||
</option> | ||
))} | ||
</select> | ||
); | ||
|
||
export interface DropdownMenuOptionsData { | ||
value: string; | ||
label: string; | ||
} | ||
|
||
interface DropdownMenuProps<T extends DropdownMenuOptionsData, U extends string = string> { | ||
interface DropdownMenuProps<T extends DropdownMenuOptionsData> { | ||
/** Label for the component */ | ||
label: string; | ||
/** Hide label. Still available for screenreaders */ | ||
hideLabel?: boolean; | ||
/** Options for component */ | ||
options: T[]; | ||
/** Default value to be selected initially */ | ||
defaultValue?: U; | ||
defaultValue?: T; | ||
/** Controlled mode */ | ||
selected?: U; | ||
selected?: T; | ||
/** Callback on selection change */ | ||
onChange?: (value: U) => void; | ||
onChange?: (value: T) => void; | ||
/** Component is disabled for user interaction */ | ||
disabled?: boolean; | ||
} | ||
|
||
export const DropdownMenu = < | ||
U extends string = string, | ||
T extends DropdownMenuOptionsData<string> = DropdownMenuOptionsData<string>, | ||
>({ | ||
export const DropdownMenu = <T extends DropdownMenuOptionsData = DropdownMenuOptionsData>({ | ||
label, | ||
hideLabel = false, | ||
options, | ||
defaultValue, | ||
onChange: propOnChange, | ||
onChange, | ||
disabled = false, | ||
}: DropdownMenuProps<T, U>) => { | ||
const labelId = useId(); | ||
return ( | ||
<div className="flex flex-row"> | ||
<label | ||
htmlFor={labelId} | ||
className={classNames('mr-2 self-center font-bold text-jod-black', { | ||
'sr-only': hideLabel, | ||
})} | ||
> | ||
}: DropdownMenuProps<T>) => { | ||
const selectId = useId(); | ||
return hideLabel ? ( | ||
<DropdownMenuSelect | ||
id={selectId} | ||
disabled={disabled} | ||
ariaLabel={label} | ||
onChange={onChange} | ||
defaultValue={defaultValue} | ||
options={options} | ||
/> | ||
) : ( | ||
<div className="flex items-center gap-2"> | ||
<label htmlFor={selectId} className="font-bold text-jod-black"> | ||
{label} | ||
</label> | ||
<select | ||
<DropdownMenuSelect | ||
id={selectId} | ||
disabled={disabled} | ||
id={labelId} | ||
className={classNames( | ||
'min-w-[120px] justify-self-end rounded-lg border border-jod-dark bg-jod-white p-2 hover:bg-purple-100 focus:outline-none focus:ring focus:ring-purple-500', | ||
{ | ||
'disabled:border-gray-500 disabled:bg-gray-200 disabled:text-gray-500 disabled:hover:bg-gray-200': disabled, | ||
}, | ||
)} | ||
onChange={(event: React.ChangeEvent<HTMLSelectElement>) => { | ||
if (propOnChange) { | ||
propOnChange(event.target.value as U); | ||
} | ||
}} | ||
onChange={onChange} | ||
defaultValue={defaultValue} | ||
> | ||
{options.map((option) => ( | ||
<option key={option.value} value={option.value}> | ||
{option.label} | ||
</option> | ||
))} | ||
</select> | ||
options={options} | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters