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

Improvements for Picker component #336

Merged
merged 4 commits into from
Jul 8, 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$base-class: 'picker';

.#{$base-class} {
box-sizing: border-box;
color: var(--content-default);
display: inline-block;
position: relative;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ describe('<Picker> component', () => {
userEvent.click(getByTestId('picker-trigger__clear-icon'));
expect(onSelect).toHaveBeenCalledWith(null);
});

it('should render selected option if selectedOption is provided', () => {
const { getByText } = renderComponent({
...defaultProps,
selectedOption: { key: 'three', name: 'Option three' },
});

expect(getByText('Option three')).toBeVisible();
});
});
34 changes: 34 additions & 0 deletions packages/react-components/src/components/Picker/Picker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,37 @@ PickerWithGroupedOptions.args = {
],
onSelect: (item) => console.log(item),
};

export const PickerWithDisabledSearch = StoryTemplate.bind({});
PickerWithDisabledSearch.args = {
searchDisabled: true,
options: [
{ key: 'groupA', name: 'Group A title header', groupHeader: true },
{ key: 'one', name: 'Option one' },
{ key: 'two', name: 'Option two' },
{ key: 'three', name: 'Option three' },
{ key: 'groupB', name: 'Group B title header', groupHeader: true },
{ key: 'four', name: 'Option four' },
{ key: 'five', name: 'Option five' },
{ key: 'six', name: 'Option six', disabled: true },
{ key: 'seven', name: 'Option seven', disabled: true },
],
onSelect: (item) => console.log(item),
};

export const PickerWithSelectedOption = StoryTemplate.bind({});
PickerWithSelectedOption.args = {
selectedOption: { key: 'two', name: 'Option two' },
options: [
{ key: 'groupA', name: 'Group A title header', groupHeader: true },
{ key: 'one', name: 'Option one' },
{ key: 'two', name: 'Option two' },
{ key: 'three', name: 'Option three' },
{ key: 'groupB', name: 'Group B title header', groupHeader: true },
{ key: 'four', name: 'Option four' },
{ key: 'five', name: 'Option five' },
{ key: 'six', name: 'Option six', disabled: true },
{ key: 'seven', name: 'Option seven', disabled: true },
],
onSelect: (item) => console.log(item),
};
15 changes: 11 additions & 4 deletions packages/react-components/src/components/Picker/Picker.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import * as React from 'react';
import { Error } from '@livechat/design-system-icons/react/material';
import cx from 'clsx';

import { Trigger, TriggerSize } from './Trigger';
import { IPickerListItem, PickerList } from './PickerList';
import { Icon } from '../Icon';
import styles from './Picker.module.scss';
import cx from 'clsx';
import { KeyCodes } from '../../utils/keyCodes';

import styles from './Picker.module.scss';

const baseClass = 'picker';

export interface IPickerProps {
disabled?: boolean;
label?: string;
error?: string;
options: IPickerListItem[];
selectedOption?: IPickerListItem;
size?: TriggerSize;
placeholder?: string;
isRequired?: boolean;
noSearchResultText?: string;
searchDisabled?: boolean;
sgraczyk marked this conversation as resolved.
Show resolved Hide resolved
onSelect: (selectedItem: IPickerListItem | null) => void;
}

Expand All @@ -26,15 +30,17 @@ export const Picker: React.FC<IPickerProps> = ({
error,
label,
options,
selectedOption,
size = 'medium',
placeholder = 'Select option',
isRequired,
noSearchResultText = 'No results found',
searchDisabled = false,
onSelect,
}) => {
const [isListOpen, setIsListOpen] = React.useState<boolean>(false);
const [selectedItem, setSelectedItem] =
React.useState<IPickerListItem | null>(null);
React.useState<IPickerListItem | null>(selectedOption || null);
const [searchPhrase, setSearchPhrase] = React.useState<string | null>(null);
const triggerRef = React.useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -107,7 +113,7 @@ export const Picker: React.FC<IPickerProps> = ({
}, [searchPhrase]);

return (
<div ref={triggerRef} className={baseClass}>
<div ref={triggerRef} className={styles[baseClass]}>
{label && (
<div
className={cx(styles[`${baseClass}__label`], {
Expand All @@ -119,6 +125,7 @@ export const Picker: React.FC<IPickerProps> = ({
)}
<div className={styles[`${baseClass}__container`]}>
<Trigger
isSearchDisabled={searchDisabled}
isError={!!error}
isOpen={isListOpen}
isDisabled={disabled}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import '../../utils/StackingContextLevel';

$base-class: 'picker-list';

.#{$base-class} {
Expand All @@ -14,6 +16,7 @@ $base-class: 'picker-list';
padding: 4px 0;
position: absolute;
width: 341px;
z-index: $stacking-context-level-dropdown;

&--compact {
top: 36px;
Expand Down
11 changes: 11 additions & 0 deletions packages/react-components/src/components/Picker/Trigger.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import styles from './Trigger.module.scss';
const baseClass = 'picker-trigger';

const defaultProps = {
isSearchDisabled: false,
isItemSelected: false,
isOpen: false,
onClick: () => noop,
Expand Down Expand Up @@ -111,4 +112,14 @@ describe('<Trigger> component', () => {

expect(queryByTestId(`${baseClass}__clear-icon`)).toBeNull();
});

it('should render Trigger without input if isOpen and isSearchDisabled', () => {
const { queryByRole } = renderComponent({
...defaultProps,
isOpen: true,
isSearchDisabled: true,
});

expect(queryByRole('textbox')).toBeNull();
});
});
16 changes: 12 additions & 4 deletions packages/react-components/src/components/Picker/Trigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const baseClass = 'picker-trigger';
export type TriggerSize = 'compact' | 'medium' | 'large';

export interface ITriggerProps {
isSearchDisabled: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we have isSearchDisabled as this is an internal part of Picker component, where our naming convention perfectly fits, the consumer doesn't see it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for now, we'll rethink this later

isDisabled?: boolean;
isError?: boolean;
isItemSelected: boolean;
Expand All @@ -27,6 +28,7 @@ export interface ITriggerProps {

export const Trigger: React.FC<ITriggerProps> = ({
children,
isSearchDisabled,
isDisabled,
isError,
isItemSelected,
Expand Down Expand Up @@ -55,10 +57,16 @@ export const Trigger: React.FC<ITriggerProps> = ({
}
};

document.addEventListener('keydown', onKeyDown);
if (!isSearchDisabled) {
document.addEventListener('keydown', onKeyDown);
}

return () => document.removeEventListener('keydown', onKeyDown);
}, []);
return () => {
if (!isSearchDisabled) {
document.removeEventListener('keydown', onKeyDown);
}
};
}, [isSearchDisabled]);

const handleTriggerClick = () => {
onClick();
Expand All @@ -80,7 +88,7 @@ export const Trigger: React.FC<ITriggerProps> = ({
onClick={handleTriggerClick}
tabIndex={0}
>
{isOpen ? (
{isOpen && !isSearchDisabled ? (
<input
className={styles[`${baseClass}__input`]}
placeholder="Select option"
Expand Down
5 changes: 5 additions & 0 deletions packages/react-components/src/utils/StackingContextLevel.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Definition of z-index values to be used across application.
*/

$stacking-context-level-dropdown: 10000;
Comment on lines +1 to +5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok now, however, I was wondering if 1000 would be enough.