Skip to content

Commit

Permalink
feat(select): add empty list placeholder prop (#438)
Browse files Browse the repository at this point in the history
* feat(select): add empty list placeholder prop

* feat(select): fix props

* docs(input-autocomplete): fix story

* feat(select): fix prop name

* feat(select): add missing css
  • Loading branch information
dmitrsavk committed Dec 30, 2020
1 parent 6d387a8 commit 75b0c9c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/select/src/components/base-select/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export const BaseSelect = forwardRef(
label,
placeholder,
fieldProps = {},
optionsListProps = {},
optionProps = {},
valueRenderer,
onChange,
onOpen,
Expand All @@ -58,6 +60,7 @@ export const BaseSelect = forwardRef(
Optgroup = () => null,
Option = () => null,
updatePopover,
showEmptyOptionsList = false,
}: BaseSelectProps,
ref,
) => {
Expand Down Expand Up @@ -231,6 +234,7 @@ export const BaseSelect = forwardRef(
({ option, index, ...rest }: { option: OptionShape; index: number }) => (
<React.Fragment key={option.key}>
{Option({
...(optionProps as object),
...rest,
innerProps: getItemProps({
index,
Expand All @@ -247,7 +251,7 @@ export const BaseSelect = forwardRef(
})}
</React.Fragment>
),
[Option, getItemProps, highlightedIndex, selectedItems, size],
[Option, getItemProps, highlightedIndex, optionProps, selectedItems, size],
);

useEffect(() => {
Expand Down Expand Up @@ -281,6 +285,8 @@ export const BaseSelect = forwardRef(
);
}, [multiple, selectedItems, disabled, name, handleNativeSelectChange, options, menuProps]);

const needRenderOptionsList = flatOptions.length > 0 || showEmptyOptionsList;

return (
<div
{...getComboboxProps({
Expand Down Expand Up @@ -335,9 +341,10 @@ export const BaseSelect = forwardRef(
popperClassName={styles.popover}
update={updatePopover}
>
{flatOptions.length > 0 && (
{needRenderOptionsList && (
<div className={styles.optionsList}>
<OptionsList
{...optionsListProps}
flatOptions={flatOptions}
highlightedIndex={highlightedIndex}
open={open}
Expand Down
13 changes: 11 additions & 2 deletions packages/select/src/components/options-list/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const OptionsList = ({
Option,
options = [],
Optgroup = DefaultOptgroup,
emptyPlaceholder,
}: OptionsListProps) => {
const counter = createCounter();

Expand All @@ -29,11 +30,19 @@ export const OptionsList = ({
[Option, counter, size],
);

return options.length > 0 ? (
if (options.length === 0 && !emptyPlaceholder) {
return null;
}

return (
<div className={cn(styles.optionsList, styles[size])}>
{options.map(option =>
isGroup(option) ? renderGroup(option) : Option({ option, index: counter() }),
)}

{emptyPlaceholder && options.length === 0 && (
<div className={styles.emptyPlaceholder}>{emptyPlaceholder}</div>
)}
</div>
) : null;
);
};
9 changes: 9 additions & 0 deletions packages/select/src/components/options-list/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@
.l {
max-height: var(--select-options-list-l-height);
}

.emptyPlaceholder {
padding: var(--gap-m) var(--gap-s);
color: var(--select-options-list-empty-placeholder-color);
}

.l .emptyPlaceholder {
padding: var(--gap-xl) var(--gap-m);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const VirtualOptionsList = ({
options = [],
overscan = 10,
Optgroup = DefaultOptgroup,
emptyPlaceholder,
}: VirtualOptionsList) => {
const parentRef = useRef<HTMLDivElement>(null);
const prevHighlightedIndex = usePrevious(highlightedIndex) || -1;
Expand Down Expand Up @@ -112,6 +113,10 @@ export const VirtualOptionsList = ({
);
})}
</div>

{emptyPlaceholder && options.length === 0 && (
<div className={styles.emptyPlaceholder}>{emptyPlaceholder}</div>
)}
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@
display: none;
}
}

.emptyPlaceholder {
padding: var(--gap-m) var(--gap-s);
color: var(--select-options-list-empty-placeholder-color);
}

.l .emptyPlaceholder {
padding: var(--gap-xl) var(--gap-m);
}
20 changes: 20 additions & 0 deletions packages/select/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ export type BaseSelectProps = {
*/
fieldProps?: unknown;

/**
* Пропсы, которые будут прокинуты в компонент списка
*/
optionsListProps?: unknown;

/**
* Пропсы, которые будут прокинуты в компонент пункта меню
*/
optionProps?: unknown;

/**
* Компонент выпадающего меню
*/
Expand Down Expand Up @@ -214,6 +224,11 @@ export type BaseSelectProps = {
* Хранит функцию, с помощью которой можно обновить положение поповера
*/
updatePopover?: PopoverProps['update'];

/**
* Показывать OptionsList, если он пустой
*/
showEmptyOptionsList?: boolean;
};

// TODO: использовать InputProps
Expand Down Expand Up @@ -343,6 +358,11 @@ export type OptionsListProps = {
* Компонент группы
*/
Optgroup?: BaseSelectProps['Optgroup'];

/**
* Будет отображаться, если компонент пустой
*/
emptyPlaceholder?: string;
};

export type OptgroupProps = {
Expand Down
1 change: 1 addition & 0 deletions packages/select/src/vars.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
--select-options-list-s-height: 264px;
--select-options-list-m-height: 308px;
--select-options-list-l-height: 396px;
--select-options-list-empty-placeholder-color: var(--color-light-text-secondary);
--select-option-divider-display: none;
--select-option-divider-background: var(--color-light-border-primary);

Expand Down

0 comments on commit 75b0c9c

Please sign in to comment.