,
): JSX.Element | null => {
if (!this.state.isOpen) return null;
let activeItemIndex = this.props.selectedIndex || null;
if (props.activeItem && activeItemIndex === null) {
activeItemIndex = props.filteredItems?.findIndex(
(item) => item.value === props.activeItem?.value,
);
}
if (!props.filteredItems || !props.filteredItems.length)
return this.noResultsUI;
return this.renderList(
props.filteredItems,
activeItemIndex,
props.renderItem,
);
};
renderList = (
items: DropdownOption[],
activeItemIndex: number | null,
renderItem: (item: any, index: number) => JSX.Element | null,
): JSX.Element | null => {
const width: number = Math.floor(this.props.width);
const RowRenderer = (itemProps: any) => (
{renderItem(items[itemProps.index], itemProps.index)}
);
return (
{RowRenderer}
);
};
render() {
const {
compactMode,
disabled,
isLoading,
labelStyle,
labelText,
labelTextColor,
labelTextSize,
widgetId,
} = this.props;
// active focused item
const activeItem = !isEmpty(this.props.options)
? this.props.options[this.state.activeItemIndex]
: undefined;
// get selected option label from selectedIndex
const selectedOption =
!isEmpty(this.props.options) &&
this.props.selectedIndex !== undefined &&
this.props.selectedIndex > -1
? this.props.options[this.props.selectedIndex].label
: this.props.label;
// for display selected option, there is no separate option to show placeholder
const value =
!isNil(selectedOption) && selectedOption !== ""
? selectedOption
: this.props.placeholder || "-- Select --";
return (
{labelText && (
{labelText}
)}
);
}
}
export interface SelectComponentProps extends ComponentProps {
disabled?: boolean;
onOptionSelected: (optionSelected: DropdownOption) => void;
placeholder?: string;
labelText?: string;
labelTextColor?: string;
labelTextSize?: TextSize;
labelStyle?: string;
compactMode: boolean;
selectedIndex?: number;
options: DropdownOption[];
isLoading: boolean;
isFilterable: boolean;
isValid: boolean;
width: number;
dropDownWidth: number;
height: number;
serverSideFiltering: boolean;
hasError?: boolean;
onFilterChange: (text: string) => void;
value?: string;
label?: string;
filterText?: string;
}
export default React.memo(SelectComponent);