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 icon and title selector in badge inputs #169

Merged
merged 1 commit into from
Nov 24, 2023
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
8 changes: 8 additions & 0 deletions src/components/BadgeInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface Props<O, N extends NameType, K extends OptionKey> {
options: O[] | null | undefined;
keySelector: (datum: O) => K;
labelSelector: (datum: O) => string;
titleSelector?: (datum: O) => string | undefined;
iconSelector?: (datum: O) => React.ReactNode | undefined;
onChange?: (value: K | undefined, name: N) => void;
disabled?: boolean | undefined;
selectedButtonVariant?: ButtonVariant;
Expand Down Expand Up @@ -46,6 +48,8 @@ function BadgeInput<O, N extends NameType, K extends OptionKey>(props: Props<O,
disabled,
labelSelector,
keySelector,
titleSelector,
iconSelector,
value,
listClassName,
containerClassName,
Expand Down Expand Up @@ -76,6 +80,8 @@ function BadgeInput<O, N extends NameType, K extends OptionKey>(props: Props<O,
const rendererParams = useCallback((key: K, data: O) => ({
name: keySelector(data),
children: labelSelector(data),
title: titleSelector ? titleSelector(data) : undefined,
icons: iconSelector ? iconSelector(data) : undefined,
disabled,
variant: value === key ? selectedButtonVariant : buttonVariant,
onClick: handleOptionClick,
Expand All @@ -87,6 +93,8 @@ function BadgeInput<O, N extends NameType, K extends OptionKey>(props: Props<O,
),
}), [
smallButtons,
titleSelector,
iconSelector,
value,
selectedButtonVariant,
buttonClassName,
Expand Down
8 changes: 8 additions & 0 deletions src/components/MultiBadgeInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface Props<O, N extends NameType, K extends OptionKey> {
options: O[] | null | undefined;
keySelector: (datum: O) => K;
labelSelector: (datum: O) => string;
titleSelector?: (datum: O) => string | undefined;
iconSelector?: (datum: O) => React.ReactNode | undefined;
onChange?: (value: K[] | undefined, name: N) => void;
disabled?: boolean | undefined;
selectedButtonVariant?: ButtonVariant;
Expand Down Expand Up @@ -48,6 +50,8 @@ function MultiBadgeInput<O, N extends NameType, K extends OptionKey>(props: Prop
disabled,
labelSelector,
keySelector,
titleSelector,
iconSelector,
value,
listClassName,
containerClassName,
Expand Down Expand Up @@ -96,6 +100,8 @@ function MultiBadgeInput<O, N extends NameType, K extends OptionKey>(props: Prop
const rendererParams = useCallback((key: K, data: O) => ({
name: keySelector(data),
children: labelSelector(data),
title: titleSelector ? titleSelector(data) : undefined,
icons: iconSelector ? iconSelector(data) : undefined,
disabled,
variant: valueMap?.[key] ? selectedButtonVariant : buttonVariant,
onClick: handleOptionClick,
Expand All @@ -107,6 +113,8 @@ function MultiBadgeInput<O, N extends NameType, K extends OptionKey>(props: Prop
),
}), [
smallButtons,
titleSelector,
iconSelector,
valueMap,
selectedButtonVariant,
buttonClassName,
Expand Down
8 changes: 8 additions & 0 deletions storybook/stories/BadgeInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Story } from '@storybook/react/types-6-0';
import { IoBugOutline } from 'react-icons/io5';
import { useArgs } from '@storybook/client-api';

import BadgeInput, { Props as BadgeInputProps } from '../../src/components/BadgeInput';
Expand All @@ -21,14 +22,19 @@ const Template: Story<BadgeInputProps<string, string, string>> = (args) => {
{
key: 'cats',
label: 'Cats',
title: 'Meow!',
icon: undefined,
},
{
key: 'dogs',
label: 'Dogs',
title: 'Bhow Bhow!',
icon: undefined,
},
{
key: 'monkeys',
label: 'Monkeys',
icon: (<IoBugOutline />),
},
];

Expand All @@ -40,6 +46,8 @@ const Template: Story<BadgeInputProps<string, string, string>> = (args) => {
options={suggestionOptions}
keySelector={(s) => s.key}
labelSelector={(s) => s.label}
titleSelector={(s) => s.title}
iconSelector={(s) => s.icon}
onChange={handleChange}
/>
);
Expand Down
Loading