Skip to content

Commit

Permalink
[EuiSelectable] export SiteWideRenderOptions and wire-up `isPreFilter…
Browse files Browse the repository at this point in the history
…ed` (#4305)

* selectable changes

* add changelog
  • Loading branch information
Michail Yasonik authored Nov 25, 2020
1 parent c0b8a25 commit ae6b5f4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Export `euiSelectableTemplateSitewideRenderOptions` ([#4305](https://github.com/elastic/eui/pull/4305))

**Bug fixes**

- Expose `isPreFiltered` in `EuiSelectable` props fixing consumer-side searching ([#4305](https://github.com/elastic/eui/pull/4305))
- Fixed stale state argument passed to `searchProps.onChange` in an `EuiSelectable`([#4292](https://github.com/elastic/eui/pull/4292))
- Fixed initial focus of an `EuiButtonGroup` when first item in a popover ([#4288](https://github.com/elastic/eui/pull/4288))
- Fixed visible scrollbar in `EuiComboBox` list ([#4301](https://github.com/elastic/eui/pull/4301))
Expand Down
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export {
EuiSelectableMessage,
EuiSelectableSearch,
EuiSelectableTemplateSitewide,
euiSelectableTemplateSitewideRenderOptions,
} from './selectable';

export { EuiSideNav } from './side_nav';
Expand Down
1 change: 1 addition & 0 deletions src/components/selectable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export {
EuiSelectableTemplateSitewideProps,
EuiSelectableTemplateSitewideOption,
EuiSelectableTemplateSitewideMetaData,
euiSelectableTemplateSitewideRenderOptions,
} from './selectable_templates';
39 changes: 30 additions & 9 deletions src/components/selectable/selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ export type EuiSelectableProps<T = {}> = CommonProps &
* or a node to replace the whole content.
*/
emptyMessage?: ReactElement | string;
/**
* Control whether or not options get filtered internally or if consumer will filter
* Default: false
*/
isPreFiltered?: boolean;
};

export interface EuiSelectableState<T> {
Expand All @@ -153,18 +158,23 @@ export class EuiSelectable<T = {}> extends Component<
options: [],
singleSelection: false,
searchable: false,
isPreFiltered: false,
};
private containerRef = createRef<HTMLDivElement>();
private optionsListRef = createRef<EuiSelectableList<T>>();
rootId = htmlIdGenerator();
constructor(props: EuiSelectableProps<T>) {
super(props);

const { options, singleSelection } = props;
const { options, singleSelection, isPreFiltered } = props;

const initialSearchValue = '';

const visibleOptions = getMatchingOptions<T>(options, initialSearchValue);
const visibleOptions = getMatchingOptions<T>(
options,
initialSearchValue,
isPreFiltered
);

// ensure that the currently selected single option is active if it is in the visibleOptions
const selectedOptions = options.filter((option) => option.checked);
Expand All @@ -187,10 +197,14 @@ export class EuiSelectable<T = {}> extends Component<
nextProps: EuiSelectableProps<T>,
prevState: EuiSelectableState<T>
) {
const { options } = nextProps;
const { options, isPreFiltered } = nextProps;
const { activeOptionIndex, searchValue } = prevState;

const matchingOptions = getMatchingOptions<T>(options, searchValue);
const matchingOptions = getMatchingOptions<T>(
options,
searchValue,
isPreFiltered
);

const stateUpdate = { visibleOptions: matchingOptions, activeOptionIndex };

Expand Down Expand Up @@ -349,17 +363,22 @@ export class EuiSelectable<T = {}> extends Component<
};

onOptionClick = (options: Array<EuiSelectableOption<T>>) => {
const { isPreFiltered, onChange, searchProps } = this.props;
const { searchValue } = this.state;
const visibleOptions = getMatchingOptions<T>(options, searchValue);
const visibleOptions = getMatchingOptions(
options,
searchValue,
isPreFiltered
);

this.setState({ visibleOptions });

if (this.props.onChange) {
this.props.onChange(options);
if (onChange) {
onChange(options);
}

if (this.props.searchProps && this.props.searchProps.onChange) {
this.props.searchProps.onChange(visibleOptions, searchValue);
if (searchProps && searchProps.onChange) {
searchProps.onChange(visibleOptions, searchValue);
}
};

Expand All @@ -383,6 +402,7 @@ export class EuiSelectable<T = {}> extends Component<
loadingMessage,
noMatchesMessage,
emptyMessage,
isPreFiltered,
...rest
} = this.props;

Expand Down Expand Up @@ -555,6 +575,7 @@ export class EuiSelectable<T = {}> extends Component<
listId={this.optionsListRef.current ? listId : undefined} // Only pass the listId if it exists on the page
aria-activedescendant={makeOptionId(activeOptionIndex)} // the current faux-focused option
placeholder={placeholderName}
isPreFiltered={isPreFiltered ?? false}
{...(searchHasAccessibleName
? searchAccessibleName
: { 'aria-label': placeholderName })}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('EuiSelectableSearch', () => {
options={[]}
listId="list"
onChange={() => {}}
isPreFiltered={false}
{...requiredProps}
/>
);
Expand All @@ -44,6 +45,7 @@ describe('EuiSelectableSearch', () => {
options={[]}
listId="list"
onChange={() => {}}
isPreFiltered={false}
defaultValue="Mi"
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type EuiSelectableSearchProps<T> = Omit<
* The id of the visible list to create the appropriate aria controls
*/
listId?: string;
isPreFiltered: boolean;
};

export interface EuiSelectableSearchState {
Expand All @@ -68,7 +69,8 @@ export class EuiSelectableSearch<T> extends Component<
const { searchValue } = this.state;
const matchingOptions = getMatchingOptions<T>(
this.props.options,
searchValue
searchValue,
this.props.isPreFiltered
);
this.props.onChange(matchingOptions, searchValue);
}
Expand All @@ -78,7 +80,8 @@ export class EuiSelectableSearch<T> extends Component<
this.setState({ searchValue: value }, () => {
const matchingOptions = getMatchingOptions<T>(
this.props.options,
value
value,
this.props.isPreFiltered
);
this.props.onChange(matchingOptions, value);
});
Expand All @@ -93,6 +96,7 @@ export class EuiSelectableSearch<T> extends Component<
defaultValue,
listId,
placeholder,
isPreFiltered,
...rest
} = this.props;

Expand Down

0 comments on commit ae6b5f4

Please sign in to comment.