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 2 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 MATCH_TARGET_WIDTH = "full-width";
adidahiya marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -29,3 +29,12 @@ $select-popover-max-width: $pt-grid-size * 40 !default;
}
}
}

.full-width {
adidahiya marked this conversation as resolved.
Show resolved Hide resolved
width: 100%;

.#{$ns}-menu {
max-width: none;
min-width: 0;
}
}
39 changes: 37 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,13 @@ export interface ISelectProps<T> extends IListItemsProps<T> {
*/
inputProps?: InputGroupProps2;

/**
* Whether the select popover should match the width of the target.
*
* @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 +136,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 +186,11 @@ 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,
matchTargetWidth ? Classes.MATCH_TARGET_WIDTH : undefined,
adidahiya marked this conversation as resolved.
Show resolved Hide resolved
)}
onOpening={this.handlePopoverOpening}
onOpened={this.handlePopoverOpened}
onClosing={this.handlePopoverClosing}
Expand Down