Skip to content

Add renderControlLabel prop #29

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions __stories__/helpers/constants/options-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CityOption, PackageOption } from '../../types';

export const PACKAGE_OPTIONS: PackageOption[] = [
{ id: 1, name: 'react' },
{ id: 1, name: 'react', description: 'React is a JavaScript library for creating user interfaces'},
{ id: 2, name: 'react-dom' },
{ id: 3, name: 'reactstrap' },
{ id: 4, name: 'react-scripts' },
{ id: 4, name: 'react-scripts', description: 'Configuration and scripts for Create React App.' },
{ id: 5, name: 'react-window' }
];

Expand Down
16 changes: 15 additions & 1 deletion __stories__/helpers/styled/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,24 @@ export const OptionContainer = styled.div`
flex-direction: row;
`;

export const OptionContent = styled.div`
height: 100%;
display: flex;
flex-direction: column;
`;

export const OptionName = styled.span`
color: #515151;
font-size: 1em;
font-weight: 600;
margin-left: 1px;
margin-bottom: 1.5px;
`;
`;

export const OptionDescription = styled.span`
color: #515151;
font-size: 1em;
font-weight: 300;
margin-left: 1px;
margin-bottom: 1.5px;
`;
23 changes: 23 additions & 0 deletions __stories__/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import {
CardBody,
OtherSpan,
OptionContainer,
OptionContent,
OptionName,
OptionDescription,
ReactSvg,
ChevronDownSvg,
MenuPortalElement,
Expand Down Expand Up @@ -798,6 +800,25 @@ export const Advanced = () => {
const getIsOptionDisabled = useCallback((x: PackageOption): boolean => x.name === PACKAGE_OPTIONS[3].name, []);

const renderOptionLabel = useCallback(
(option: PackageOption): ReactNode => (
<OptionContainer>
<ReactSvg
{...REACT_SVG_PROPS}
isDisabled={getIsOptionDisabled(option)}
>
<path {...REACT_SVG_PATH_PROPS} />
<circle {...REACT_SVG_CIRCLE_PROPS} />
</ReactSvg>
<OptionContent>
<OptionName>{option.name}</OptionName>
{option.description && <OptionDescription>{option.description}</OptionDescription>}
</OptionContent>
</OptionContainer>
),
[getIsOptionDisabled]
);

const renderControlLabel = useCallback(
(option: PackageOption): ReactNode => (
<OptionContainer>
<ReactSvg
Expand Down Expand Up @@ -875,7 +896,9 @@ export const Advanced = () => {
themeConfig={THEME_CONFIG}
caretIcon={customCaretIcon}
getOptionValue={getOptionValue}
menuItemSize={70}
renderOptionLabel={renderOptionLabel}
renderControlLabel={renderControlLabel}
getIsOptionDisabled={getIsOptionDisabled}
/>
</SelectContainer>
Expand Down
1 change: 1 addition & 0 deletions __stories__/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export type CityOption = Readonly<{
export type PackageOption = Readonly<{
id: number;
name: string;
description?: string;
}>;
5 changes: 4 additions & 1 deletion src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export type SelectProps = Readonly<{
onInputBlur?: FocusEventHandler<HTMLInputElement>;
onInputFocus?: FocusEventHandler<HTMLInputElement>;
renderOptionLabel?: (data: OptionData) => ReactNode;
renderControlLabel?: (data: OptionData) => ReactNode;
getIsOptionDisabled?: (data: OptionData) => boolean;
getFilterOptionString?: (option: MenuOption) => string;
renderMultiOptions?: (params: MultiParams) => ReactNode;
Expand Down Expand Up @@ -261,6 +262,7 @@ const Select = forwardRef<SelectRef, SelectProps>((
blurInputOnSelect,
menuItemDirection,
renderOptionLabel,
renderControlLabel,
renderMultiOptions,
menuScrollDuration,
filterIgnoreAccents,
Expand Down Expand Up @@ -313,6 +315,7 @@ const Select = forwardRef<SelectRef, SelectProps>((
const getOptionLabelFn = useMemo<OptionLabelCallback>(() => getOptionLabel || GET_OPTION_LABEL_DEFAULT, [getOptionLabel]);
const getOptionValueFn = useMemo<OptionValueCallback>(() => getOptionValue || GET_OPTION_VALUE_DEFAULT, [getOptionValue]);
const renderOptionLabelFn = useMemo<RenderLabelCallback>(() => renderOptionLabel || getOptionLabelFn, [renderOptionLabel, getOptionLabelFn]);
const renderControlLabelFn = useMemo<RenderLabelCallback>(() => renderControlLabel || renderOptionLabelFn, [renderControlLabel, renderOptionLabelFn]);

// Custom hook abstraction that debounces search input value (opt-in)
const debouncedInputValue = useDebounce<string>(inputValue, inputDelay);
Expand Down Expand Up @@ -764,7 +767,7 @@ const Select = forwardRef<SelectRef, SelectProps>((
selectedOption={selectedOption}
focusedMultiValue={focusedMultiValue}
renderMultiOptions={renderMultiOptions}
renderOptionLabel={renderOptionLabelFn}
renderOptionLabel={renderControlLabelFn}
removeSelectedOption={removeSelectedOption}
/>
<AutosizeInput
Expand Down