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

Fix #3649: Listbox emptyMessage, emptyFilterMessage #3658

Merged
merged 1 commit into from
Nov 16, 2022
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
12 changes: 12 additions & 0 deletions api-generator/components/listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ const ListBoxProps = [
default: 'null',
description: 'Custom template for the filter element.'
},
{
name: 'emptyMessage',
type: 'string',
default: 'No results found',
description: 'Text to display when there are no options available.'
},
{
name: 'emptyFilterMessage',
type: 'any',
default: 'No results found',
description: 'Template to display when filtering does not return any results.'
},
{
name: 'optionGroupTemplate',
type: 'any',
Expand Down
12 changes: 12 additions & 0 deletions components/doc/listbox/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ export function ApiDoc(props) {
<td>null</td>
<td>Property name or getter function that refers to the children options of option group.</td>
</tr>
<tr>
<td>emptyMessage</td>
<td>string</td>
<td>No results found</td>
<td>Text to display when there are no options available.</td>
</tr>
<tr>
<td>emptyFilterMessage</td>
<td>any</td>
<td>No results found</td>
<td>Template to display when filtering does not return any results.</td>
</tr>
<tr>
<td>itemTemplate</td>
<td>any</td>
Expand Down
64 changes: 39 additions & 25 deletions components/lib/listbox/ListBox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { FilterService } from '../api/Api';
import { FilterService, localeOption } from '../api/Api';
import { useMountEffect } from '../hooks/Hooks';
import { Tooltip } from '../tooltip/Tooltip';
import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';
Expand Down Expand Up @@ -343,7 +343,19 @@ export const ListBox = React.memo(
};

const createItems = () => {
return visibleOptions ? visibleOptions.map(createItem) : null;
if (ObjectUtils.isNotEmpty(visibleOptions)) {
return visibleOptions.map(createItem);
} else if (hasFilter) {
return createEmptyMessage(props.emptyFilterMessage, true);
}

return createEmptyMessage(props.emptyMessage);
};

const createEmptyMessage = (emptyMessage, isFilter) => {
const message = ObjectUtils.getJSXElement(emptyMessage, props) || localeOption(isFilter ? 'emptyFilterMessage' : 'emptyMessage');

return <li className="p-listbox-empty-message">{message}</li>;
};

const createList = () => {
Expand Down Expand Up @@ -411,36 +423,38 @@ export const ListBox = React.memo(
ListBox.displayName = 'ListBox';
ListBox.defaultProps = {
__TYPE: 'ListBox',
id: null,
value: null,
options: null,
optionLabel: null,
optionValue: null,
optionDisabled: null,
optionGroupLabel: null,
optionGroupChildren: null,
optionGroupTemplate: null,
itemTemplate: null,
style: null,
listStyle: null,
listClassName: null,
className: null,
virtualScrollerOptions: null,
disabled: null,
dataKey: null,
multiple: false,
metaKeySelection: false,
disabled: null,
emptyFilterMessage: null,
emptyMessage: null,
filter: false,
filterTemplate: null,
filterBy: null,
filterValue: null,
filterInputProps: null,
filterLocale: undefined,
filterMatchMode: 'contains',
filterPlaceholder: null,
filterLocale: undefined,
filterInputProps: null,
filterTemplate: null,
filterValue: null,
id: null,
itemTemplate: null,
listClassName: null,
listStyle: null,
metaKeySelection: false,
multiple: false,
onChange: null,
onFilterValueChange: null,
optionDisabled: null,
optionGroupChildren: null,
optionGroupLabel: null,
optionGroupTemplate: null,
optionLabel: null,
optionValue: null,
options: null,
style: null,
tabIndex: 0,
tooltip: null,
tooltipOptions: null,
onChange: null,
onFilterValueChange: null
value: null,
virtualScrollerOptions: null
};
6 changes: 6 additions & 0 deletions components/lib/listbox/listbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type ListBoxItemTemplateType = React.ReactNode | ((option: any) => React.ReactNo

type ListBoxFilterTemplateType = React.ReactNode | ((options: ListBoxFilterTemplateOptions) => React.ReactNode);

type ListBoxEmptyMessageType = React.ReactNode | ((props: ListBoxProps) => React.ReactNode);

type ListBoxEmptyFilterMessageType = React.ReactNode | ((props: ListBoxProps) => React.ReactNode);

type ListBoxOptionDisabledType = string | ((option: any) => boolean);

interface ListBoxChangeTargetOptions {
Expand Down Expand Up @@ -53,6 +57,8 @@ export interface ListBoxProps extends Omit<React.DetailedHTMLProps<React.InputHT
children?: React.ReactNode;
dataKey?: string;
disabled?: boolean;
emptyMessage?: ListBoxEmptyMessageType;
emptyFilterMessage?: ListBoxEmptyFilterMessageType;
filter?: boolean;
filterBy?: string;
filterInputProps?: any;
Expand Down