Skip to content
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

add default item to dropdown #389

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/components/Dropdown/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ export const _Dropdown = {

<br />

<Dropdown
size={'xs'}
options={[
{
icon: (
<MassaToken size={16} className="bg-c-default rounded-full" />
),
item: 'MAS',
onClick: () => console.log('option 1'),
},
{ icon: <MassaLogo size={16} />, item: 'MAS' },
{ icon: <MassaLogo size={16} />, item: 'MAS' },
]}
defaultItem={{ item: 'Select a token' }}
/>

<br />

<Dropdown
size={'xs'}
readOnly={true}
Expand Down
23 changes: 15 additions & 8 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@ interface IOption extends ComponentPropsWithoutRef<'div'> {
select?: number;
}

interface IDropdownProps extends ComponentPropsWithoutRef<'div'> {
interface DropdownProps extends ComponentPropsWithoutRef<'div'> {
options: IOption[];
size?: 'xs' | 'md';
select?: number;
readOnly?: boolean;
defaultItem?: IOption;
}

function Icon({ toggle }: { toggle: boolean }) {
let IconclassName = 'max-w-fit stroke-current';
let IconClassName = 'max-w-fit stroke-current';

return toggle ? (
<FiChevronUp className={IconclassName} />
<FiChevronUp className={IconClassName} />
) : (
<FiChevronDown className={IconclassName} />
<FiChevronDown className={IconClassName} />
);
}

export function Dropdown(props: IDropdownProps) {
let { size = 'md', select = 0, options, readOnly = false } = props;
export function Dropdown(props: DropdownProps) {
let {
size = 'md',
select = 0,
options,
readOnly = false,
defaultItem,
} = props;

const ref = useRef(null);

Expand All @@ -51,7 +58,7 @@ export function Dropdown(props: IDropdownProps) {

const firstObject = options[select];
const [toggle, setToggle] = useState(false);
const [selected, setSelected] = useState(firstObject);
const [selected, setSelected] = useState(defaultItem ?? firstObject);
const hidden = toggle ? '' : 'hidden';

useClickOutside(ref, () => {
Expand All @@ -60,7 +67,7 @@ export function Dropdown(props: IDropdownProps) {

// refresh the selected item when the select prop changes
useEffect(() => {
setSelected(options[select]);
setSelected(defaultItem ?? options[select]);
}, [select, options]);

const customButtonClass = classes[size].button;
Expand Down
Loading