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

APP-3784 Autocomplete - Custom filtering #123

Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@types/lodash": "^4.14.154",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
"@types/react-select": "^4.0.11",
"@types/react-virtualized": "^9.21.10",
"@types/storybook__react": "^5.2.1",
"@types/styled-components": "^5.1.0",
Expand Down
5 changes: 4 additions & 1 deletion spec/components/dropdown/Dropdown.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const CustomComponent = (props) => {
return (<div>{props?.data?.label}</div>);
}
}

describe('Dropdown component test suite =>', () => {
const dropdownProps = {
options: [],
Expand All @@ -35,12 +36,14 @@ describe('Dropdown component test suite =>', () => {
});

it('should select first option', async () => {
const { getByText } = render(<Dropdown options={dropdownProps.options} />);
const { getByText } = render(<Dropdown options={dropdownProps.options} noOptionMessage="no options message"/>);
const input = screen.getByRole('textbox');
userEvent.click(input);
const option = screen.getByText('banana');
userEvent.click(option);
expect(getByText('banana')).toBeTruthy();
userEvent.type(input, 'zz');
expect(screen.getByText('no options message')).toBeTruthy();
});

it('should render costum render dropdown', async () => {
Expand Down
164 changes: 107 additions & 57 deletions src/components/dropdown/CustomRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,143 @@ import Icon from '../icon';
const stopPropagation = (e) => {
e.preventDefault();
e.stopPropagation();
}
};

/** The following components are defined to override
/** The following components are defined to override
* the appereace of the react-select library components **/

export const DefaultOptionRenderer = (props:any) => {
export const DefaultOptionRenderer = (props: any) => {
const OptionRenderer = props?.selectProps?.optionRenderer;
const rendererProps = {data: props.data}
return (<div> {OptionRenderer ?
<components.Option {...props} >
<OptionRenderer {...rendererProps} />
</components.Option> : <components.Option {...props} />}</div>
const rendererProps = { data: props.data, inputValue: props.selectProps?.inputValue };
return (
<div>
{OptionRenderer ? (
<components.Option {...props}>
<OptionRenderer {...rendererProps} />
</components.Option>
) : (
<components.Option {...props} />
)}
</div>
);
};

export const SingleValue = (props:any) => {
export const SingleValue = (props: any) => {
const OptionRenderer = props.selectProps.optionRenderer;
const rendererProps = {data: props.data}
return (<div> {OptionRenderer ?
<components.SingleValue {...props}>
<OptionRenderer {...rendererProps} />
</components.SingleValue> : <components.SingleValue {...props} />}</div>
const rendererProps = { data: props.data };
return (
<div>
{OptionRenderer ? (
<components.SingleValue {...props}>
<OptionRenderer {...rendererProps} />
</components.SingleValue>
) : (
<components.SingleValue {...props} />
)}
</div>
);
};

export const DefaultTagRenderer = (props:any) => {
export const DefaultTagRenderer = (props: any) => {
const TagRender = props.selectProps?.tagRenderer;
const rendererProps = {remove: props.removeProps.onClick, data:props.data};
return (<div> {TagRender ?
<components.MultiValue {...props} className="tk-tag">
<div onMouseDown={stopPropagation}>
<TagRender {...rendererProps}/>
</div>
</components.MultiValue>
: <components.MultiValue {...props} >
<div onMouseDown={stopPropagation} >
<span className="tk-pr-1">{props.data?.label}</span>
<Icon iconName="cross" onClick={() => props.removeProps.onClick()}/>
</div>
</components.MultiValue>}</div>
const rendererProps = { remove: props.removeProps.onClick, data: props.data };
return (
<div>
{TagRender ? (
<components.MultiValue {...props} className="tk-tag">
<div onMouseDown={stopPropagation}>
<TagRender {...rendererProps} />
</div>
</components.MultiValue>
) : (
<components.MultiValue {...props}>
<div onMouseDown={stopPropagation}>
<span className="tk-pr-1">{props.data?.label}</span>
<Icon
iconName="cross"
onClick={() => props.removeProps.onClick()}
/>
</div>
</components.MultiValue>
)}
</div>
);
}
};

export const MultiValueContainerOverride = ({ children, ...props }:any) => {
export const MultiValueContainerOverride = ({ children, ...props }: any) => {
return (
<components.MultiValueContainer {...props}>
<div>{children}</div>
</components.MultiValueContainer>
);
}
};

export const MultiValueRemove = ()=>{return null};
export const MultiValueRemove = () => {
return null;
};

export const DropdownIndicator = (props:any) => {
return (<div>{props?.selectProps?.isMulti ?
(<div>{props?.selectProps?.isMulti && props?.selectProps?.displayArrowIndicator ?
<components.DropdownIndicator {...props} /> : <components.DropdownIndicator {...props} className="tk-d-none" />}</div>) :
<components.DropdownIndicator {...props}>
<Icon iconName={props?.selectProps.menuIsOpen ? 'drop-up' : 'drop-down'} className="tk-select__single-value"></Icon>
</components.DropdownIndicator>}</div>
export const DropdownIndicator = (props: any) => {
return (
<div>
{props?.selectProps?.isMulti ? (
<div>
{props?.selectProps?.isMulti &&
props?.selectProps?.displayArrowIndicator ? (
<components.DropdownIndicator {...props} />
) : (
<components.DropdownIndicator {...props} className="tk-d-none" />
)}
</div>
) : (
<components.DropdownIndicator {...props}>
<Icon
iconName={props?.selectProps.menuIsOpen ? 'drop-up' : 'drop-down'}
className="tk-select__single-value"
></Icon>
</components.DropdownIndicator>
)}
</div>
);
};

export const ClearIndicator = (props:any) => {
export const ClearIndicator = (props: any) => {
return (
<components.ClearIndicator {...props}>
<Icon iconName="cross-round"></Icon>
</components.ClearIndicator>
);
};

export const Control = ({ children, ...props }:any) => {
export const Control = ({ children, ...props }: any) => {
const iconName = props.selectProps.iconName;
return (<div className="tk-input-group__header">
{<label className="tk-label tk-mb-h">{props?.selectProps?.label}</label>}
{iconName ?
<components.Control {...props} className="tk-input__container" >
<div className="tk-input__icon">
<Icon
iconName={iconName}
tabIndex={0}
></Icon>
</div>
{children}
</components.Control> : <components.Control {...props} >
{children}
</components.Control>}
</div>
return (
<div className="tk-input-group__header">
{<label className="tk-label tk-mb-h">{props?.selectProps?.label}</label>}
{iconName ? (
<components.Control {...props} className="tk-input__container">
<div className="tk-input__icon">
<Icon iconName={iconName} tabIndex={0}></Icon>
</div>
{children}
</components.Control>
) : (
<components.Control {...props}>{children}</components.Control>
)}
</div>
);
}
};

export const NoOptionsMessage = (props: any) => {
const noOptionMessage = props.selectProps?.noOptionMessage;
return (
<div>
{noOptionMessage ? (
<components.NoOptionsMessage {...props}>
<div>{noOptionMessage}</div>
</components.NoOptionsMessage>
) : (
<components.NoOptionsMessage {...props} />
)}
</div>
);
};
Loading