Skip to content

Commit

Permalink
Added auto positioning in input select
Browse files Browse the repository at this point in the history
  • Loading branch information
MehulJain27 committed Nov 27, 2023
1 parent 2a6f0d1 commit 6dadeeb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
23 changes: 9 additions & 14 deletions components/dropdown/src/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,15 @@ export default class Dropdown extends Component<
const { height, width } = this.state
const { auto } = this.props

const maxWidth = reference.width
const maxHeight = reference.height

// Choose:
const up = auto
? target.bottom + height > maxHeight && target.top - height >= 0
: this.props.up
const left = auto ? target.left + width < maxWidth : this.props.left
const right = auto
? left
? false
: target.right - width > 0
: this.props.right
const down = auto ? !up : this.down
const scrollableHeight =
document.body.scrollHeight - (window.scrollY + window.innerHeight)
const spaceBelow = window.innerHeight - target.bottom
const spaceRight = window.innerWidth - target.left

const down = auto ? spaceBelow + scrollableHeight > height : this.down
const up = auto ? !down : this.props.up
const left = auto ? target.left > 0 : this.props.left
const right = auto ? spaceRight > 0 : this.props.right

const layout = { up, left, right, down }

Expand Down
6 changes: 5 additions & 1 deletion components/input-select/docs/Overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ return (
</>
)
```

Below is an Input Select with auto set as true, which automatically positions the dropdown above the selection
<InputSelect
auto
options={[1,2,3,4,5]}
/>
</Documenter>


8 changes: 8 additions & 0 deletions components/input-select/src/input-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export interface Props<Value = any, Option = any> extends BaseProps {
*/
up?: boolean

/**
* Whether to auto compute the position of dropdown options around the input selection.
*/
auto?: boolean

/*** Field Context Passed from parent Field */
__fieldContext?: FieldContext
/*** Visually Representing error state of component */
Expand Down Expand Up @@ -157,6 +162,7 @@ export default class InputSelect<Value = any, Option = any> extends Component<
return <div className={classnames('empty')}>No results found</div>
},
up: false,
auto: false,
}

constructor(props) {
Expand Down Expand Up @@ -213,6 +219,7 @@ export default class InputSelect<Value = any, Option = any> extends Component<
disabled,
readOnly,
up,
auto,
__fieldContext = {},
error,
variant = 'bordered',
Expand All @@ -227,6 +234,7 @@ export default class InputSelect<Value = any, Option = any> extends Component<
right
up={up}
container
auto={auto}
className={classnames('container', { disabled })}
isOpen={this.state.isOpen}
onOpen={disabled ? null : this.handleOpen}
Expand Down

0 comments on commit 6dadeeb

Please sign in to comment.