-
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.
Merge pull request #10 from miksrv/develop
Develop
- Loading branch information
Showing
30 changed files
with
1,264 additions
and
168 deletions.
There are no files selected for viewing
Binary file not shown.
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,6 +1,6 @@ | ||
{ | ||
"name": "simple-react-ui-kit", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "My small UI framework for projects", | ||
"repository": "https://github.com/miksrv/simple-react-ui-kit.git", | ||
"scripts": { | ||
|
@@ -31,19 +31,19 @@ | |
"author": "Misha Topchilo <[email protected]>", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@changesets/cli": "^2.27.8", | ||
"@eslint/compat": "^1.1.1", | ||
"@changesets/cli": "^2.27.9", | ||
"@eslint/compat": "^1.2.0", | ||
"@eslint/js": "^9.11.1", | ||
"@rollup/plugin-commonjs": "^28.0.0", | ||
"@rollup/plugin-node-resolve": "^15.3.0", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@types/lodash-es": "^4.17.12", | ||
"@types/react": "^18.3.10", | ||
"@types/react": "^18.3.11", | ||
"@types/react-dom": "^18.3.0", | ||
"eslint": "^9.11.1", | ||
"eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-eslint-plugin": "^6.2.0", | ||
"eslint-plugin-import": "^2.30.0", | ||
"eslint-plugin-import": "^2.31.0", | ||
"eslint-plugin-jest": "^28.8.3", | ||
"eslint-plugin-react": "^7.37.1", | ||
"eslint-plugin-simple-import-sort": "^12.1.1", | ||
|
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 |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import React, { useEffect, useRef, useState } from 'react' | ||
|
||
import { cn } from '../../utils' | ||
import Button, { ButtonProps } from '../button' | ||
import Icon, { IconTypes } from '../icon' | ||
|
||
import OptionsList from './OptionsList' | ||
import styles from './styles.module.sass' | ||
|
||
/** | ||
* Dropdown option type, representing a single option in the dropdown list | ||
*/ | ||
export type DropdownOption<T> = { | ||
/** Unique key to identify the option */ | ||
key: T | ||
/** Display value of the option (can be text, number, or a React node) */ | ||
value: React.ReactNode | string | number | ||
/** Optional icon to display next to the option */ | ||
icon?: IconTypes | ||
/** Optional image to display next to the option */ | ||
image?: MediaImage | ||
/** Disable the option from being selected */ | ||
disabled?: boolean | ||
} | ||
|
||
/** | ||
* Dropdown component properties | ||
*/ | ||
export interface DropdownProps<T> extends Pick<ButtonProps, 'size' | 'mode'> { | ||
/** Additional class names for custom styling */ | ||
className?: string | ||
/** Array of options to display in the dropdown */ | ||
options?: DropdownOption<T>[] | ||
/** Mark the dropdown as required */ | ||
required?: boolean | ||
/** Disable the dropdown */ | ||
disabled?: boolean | ||
/** Whether the dropdown can be cleared (reset to no selection) */ | ||
clearable?: boolean | ||
/** Placeholder text to display when no option is selected */ | ||
placeholder?: string | ||
/** Label text for the dropdown */ | ||
label?: string | ||
/** Error message to display when validation fails */ | ||
error?: string | ||
/** Current selected value (key) in the dropdown */ | ||
value?: T | ||
/** Callback function triggered when an option is selected */ | ||
onSelect?: (selectedOption: DropdownOption<T> | undefined) => void | ||
/** Callback function triggered when the dropdown is opened */ | ||
onOpen?: () => void | ||
} | ||
|
||
const Dropdown = <T,>({ | ||
className, | ||
required, | ||
options, | ||
disabled, | ||
clearable, | ||
value, | ||
placeholder, | ||
label, | ||
error, | ||
onSelect, | ||
onOpen, | ||
...props | ||
}: DropdownProps<T>) => { | ||
const dropdownRef = useRef<HTMLDivElement>(null) | ||
const [optionsListTop, setOptionsListTop] = useState<number>(30) | ||
const [isOpen, setIsOpen] = useState<boolean>(false) | ||
const [selectedOption, setSelectedOption] = useState<DropdownOption<T> | undefined>(undefined) | ||
|
||
const toggleDropdown = () => { | ||
if (onOpen) { | ||
onOpen() | ||
} else { | ||
setIsOpen(!isOpen) | ||
} | ||
} | ||
|
||
const handleSelect = (option: DropdownOption<T> | undefined) => { | ||
if (selectedOption?.key !== option?.key) { | ||
setSelectedOption(option) | ||
onSelect?.(option ?? undefined) | ||
} | ||
|
||
setIsOpen(false) | ||
} | ||
|
||
const handleClickOutside = (event: MouseEvent) => { | ||
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { | ||
setIsOpen(false) | ||
} | ||
} | ||
|
||
const handleClearClick = (event: React.MouseEvent) => { | ||
event.stopPropagation() | ||
handleSelect(undefined) | ||
} | ||
|
||
useEffect(() => { | ||
document.addEventListener('mousedown', handleClickOutside) | ||
|
||
setOptionsListTop(dropdownRef?.current?.clientHeight ?? 0) | ||
|
||
return () => { | ||
document.removeEventListener('mousedown', handleClickOutside) | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (!value) { | ||
setSelectedOption(undefined) | ||
} else { | ||
const selected = options?.find((opt) => opt.key === value) | ||
setSelectedOption(selected) | ||
} | ||
}, [value, options]) | ||
|
||
return ( | ||
<div className={cn(className, styles.dropdown, required && styles.required, disabled && styles.disabled)}> | ||
{label && <label className={styles.label}>{label}</label>} | ||
<div | ||
ref={dropdownRef} | ||
className={cn(styles.container, isOpen && styles.open, disabled && styles.disabled)} | ||
> | ||
<Button | ||
size={props?.size ?? 'small'} | ||
mode={props?.mode ?? 'secondary'} | ||
variant={error ? 'negative' : undefined} | ||
disabled={disabled} | ||
onClick={toggleDropdown} | ||
className={cn(styles.dropdownButton, selectedOption && styles.selected, isOpen && styles.open)} | ||
> | ||
<span className={styles.value}> | ||
{selectedOption?.icon && <Icon name={selectedOption.icon} />} | ||
|
||
{selectedOption?.image && ( | ||
<img | ||
className={styles.categoryIcon} | ||
src={selectedOption.image.src} | ||
alt={''} | ||
width={22} | ||
height={26} | ||
/> | ||
)} | ||
|
||
{selectedOption?.value ? ( | ||
selectedOption?.value | ||
) : ( | ||
<span className={styles.placeholder}>{placeholder ?? ''}</span> | ||
)} | ||
</span> | ||
|
||
<span className={styles.buttonContainer}> | ||
{clearable && selectedOption?.key && ( | ||
<button | ||
type={'button'} | ||
className={styles.clearButton} | ||
onClick={handleClearClick} | ||
> | ||
<Icon name={'Close'} /> | ||
</button> | ||
)} | ||
{isOpen ? <Icon name={'KeyboardUp'} /> : <Icon name={'KeyboardDown'} />} | ||
</span> | ||
</Button> | ||
|
||
{!!error?.length && <div className={styles.error}>{error}</div>} | ||
|
||
{isOpen && ( | ||
<OptionsList<T> | ||
style={{ top: optionsListTop }} | ||
options={options} | ||
selectedOption={selectedOption} | ||
onOptionSelect={handleSelect} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Dropdown |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react' | ||
|
||
import { cn } from '../../utils' | ||
import Icon from '../icon' | ||
|
||
import type { DropdownOption } from './Dropdown' | ||
import styles from './styles.module.sass' | ||
|
||
interface DropdownProps<T> extends React.HTMLAttributes<HTMLUListElement> { | ||
options?: DropdownOption<T>[] | ||
selectedOption?: DropdownOption<T> | ||
onOptionSelect?: (selectedOption: DropdownOption<T>) => void | ||
} | ||
|
||
const OptionsList = <T,>({ selectedOption, options, onOptionSelect, ...props }: DropdownProps<T>) => ( | ||
<ul | ||
className={styles.optionsList} | ||
style={props.style} | ||
> | ||
{options?.map((option, i) => ( | ||
<li | ||
key={`option-${option.key ?? i}`} | ||
className={cn(option.key === selectedOption?.key && styles.active, option.disabled && styles.disabled)} | ||
> | ||
<button onClick={() => (!option.disabled ? onOptionSelect?.(option) : undefined)}> | ||
{option?.icon && <Icon name={option.icon} />} | ||
|
||
{option?.image && ( | ||
<img | ||
src={option.image.src} | ||
alt={''} | ||
width={22} | ||
height={26} | ||
/> | ||
)} | ||
|
||
<span>{option.value}</span> | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
|
||
export default OptionsList |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './Dropdown' | ||
export type { DropdownOption, DropdownProps } from './Dropdown' |
Oops, something went wrong.