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

[select] feat(Select): new prop "matchTargetWidth" #4841

Merged
merged 5 commits into from
Aug 16, 2021
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
Expand Up @@ -34,6 +34,7 @@ export interface ISelectExampleState {
resetOnSelect: boolean;
disableItems: boolean;
disabled: boolean;
matchTargetWidth: false;
}

export class SelectExample extends React.PureComponent<IExampleProps, ISelectExampleState> {
Expand All @@ -45,6 +46,7 @@ export class SelectExample extends React.PureComponent<IExampleProps, ISelectExa
disabled: false,
filterable: true,
hasInitialContent: false,
matchTargetWidth: false,
minimal: false,
resetOnClose: false,
resetOnQuery: true,
Expand All @@ -71,6 +73,8 @@ export class SelectExample extends React.PureComponent<IExampleProps, ISelectExa

private handleResetOnSelectChange = this.handleSwitchChange("resetOnSelect");

private handleMatchTargetWidthChange = this.handleSwitchChange("matchTargetWidth");

public render() {
const { allowCreate, disabled, disableItems, minimal, ...flags } = this.state;

Expand Down Expand Up @@ -124,6 +128,11 @@ export class SelectExample extends React.PureComponent<IExampleProps, ISelectExa
checked={this.state.disableItems}
onChange={this.handleItemDisabledChange}
/>
<Switch
label="Match target width"
checked={this.state.matchTargetWidth}
onChange={this.handleMatchTargetWidthChange}
/>
<Switch
label="Allow creating new items"
checked={this.state.allowCreate}
Expand Down
1 change: 1 addition & 0 deletions packages/select/src/common/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export const OMNIBAR = `${NS}-omnibar`;
export const OMNIBAR_OVERLAY = `${OMNIBAR}-overlay`;
export const SELECT = `${NS}-select`;
export const SELECT_POPOVER = `${SELECT}-popover`;
export const SELECT_MATCH_TARGET_WIDTH = `${SELECT}-match-target-width`;
9 changes: 9 additions & 0 deletions packages/select/src/components/select/_select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ $select-popover-max-height: $pt-grid-size * 30 !default;
$select-popover-max-width: $pt-grid-size * 40 !default;

.#{$ns}-select-popover {
&.#{$ns}-select-match-target-width {
width: 100%;

.#{$ns}-menu {
max-width: none;
min-width: 0;
}
}

.#{$ns}-popover-content {
// use padding on container rather than margin on input group
// because top margin leaves some empty space with no background color.
Expand Down
40 changes: 38 additions & 2 deletions packages/select/src/components/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ export interface ISelectProps<T> extends IListItemsProps<T> {
*/
inputProps?: InputGroupProps2;

/**
* Whether the select popover should be styled so that it matches the width of the target.
* This is done using a popper.js modifier passed through `popoverProps`.
*
* Note that setting `matchTargetWidth={true}` will also set `popoverProps.usePortal={false}` and `popoverProps.wrapperTagName="div"`.
*
* @default false
*/
matchTargetWidth?: boolean;

/** Props to spread to `Popover`. Note that `content` cannot be changed. */
// eslint-disable-next-line @typescript-eslint/ban-types
popoverProps?: Partial<IPopoverProps> & object;
Expand Down Expand Up @@ -129,7 +139,31 @@ export class Select<T> extends AbstractPureComponent2<SelectProps<T>, ISelectSta

private renderQueryList = (listProps: IQueryListRendererProps<T>) => {
// not using defaultProps cuz they're hard to type with generics (can't use <T> on static members)
const { filterable = true, disabled = false, inputProps = {}, popoverProps = {} } = this.props;
const {
filterable = true,
disabled = false,
inputProps = {},
popoverProps = {},
matchTargetWidth,
} = this.props;

if (matchTargetWidth) {
if (popoverProps.modifiers == null) {
popoverProps.modifiers = {};
}

popoverProps.modifiers.minWidth = {
enabled: true,
fn: data => {
data.styles.width = `${data.offsets.reference.width}px`;
return data;
},
order: 800,
};

popoverProps.usePortal = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

is this strictly necessary? if so, we should make a note of it in the matchTargetWidth prop documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, it is necessary. I've updated the prop docs with a note!

Copy link
Contributor

Choose a reason for hiding this comment

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

cool 👍🏽

popoverProps.wrapperTagName = "div";
}

const input = (
<InputGroup
Expand All @@ -155,7 +189,9 @@ export class Select<T> extends AbstractPureComponent2<SelectProps<T>, ISelectSta
{...popoverProps}
className={classNames(listProps.className, popoverProps.className)}
onInteraction={this.handlePopoverInteraction}
popoverClassName={classNames(Classes.SELECT_POPOVER, popoverProps.popoverClassName)}
popoverClassName={classNames(Classes.SELECT_POPOVER, popoverProps.popoverClassName, {
[Classes.SELECT_MATCH_TARGET_WIDTH]: matchTargetWidth,
})}
onOpening={this.handlePopoverOpening}
onOpened={this.handlePopoverOpened}
onClosing={this.handlePopoverClosing}
Expand Down