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

WIP: Fixes select option overflow #163

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

WIP on select calculation

  • Loading branch information
ryanml committed Oct 2, 2018
commit 69860e88365d6e6c67e4e8d2cf3c505fe1ecfa25
@@ -53,7 +53,9 @@ exports[`Select tests basic tests matches the snapshot 1`] = `
}

.c6 {
position: fixed;
position: absolute;
top: calc(100% + 4px);
left: 0;
width: 100%;
border-radius: 3px;
box-shadow: 0 2px 5px 0 rgba(223,223,232,0.5);
@@ -29,10 +29,55 @@ export interface Props {
type?: Type
}

export interface OptionsPosition {
position: string
top: string
left: string
}

interface State {
value: string | number
selected: React.ReactNode
show: boolean
optionsPosition: OptionsPosition
}

/* To do: Move all InetersectionObserver related interfaces to a d.ts file */

interface Bounds {
height: number
width: number
top: number
left: number
right: number
bottom: number
}

interface IntersectionObserverEntry {
time: number
rootBounds: Bounds
boundingClientRect: Bounds
intersectionRect: Bounds
intersectionRatio: number
target: Element
isIntersecting: boolean
}

interface IntersectionObserver {
root: Element | null
rootMargin: string
thresholds: number[]

disconnect (): void
observe (target: Element): void
unobserve (target: Element): void
takeRecords (): IntersectionObserverEntry[]
}

interface IntersectionObserverOptions {
root: Element | null
rootMargin: string
threshold: number
}

/*
@@ -43,15 +88,21 @@ interface State {
- add empty first choice?
*/
export default class Select extends React.PureComponent<Props, State> {
private selectOptions: HTMLDivElement | null
private intersectionObserver: IntersectionObserver | null

constructor (props: Props) {
super(props)

const obj = this.getDefaultValue(props)
this.state = {
value: obj.value,
selected: obj.selected,
show: false
show: false,
optionsPosition: this.defaultOptionsPosition
}
this.selectOptions = null
this.intersectionObserver = null
}

static defaultProps = {
@@ -130,7 +181,36 @@ export default class Select extends React.PureComponent<Props, State> {
}
}

get observerOptions (): IntersectionObserverOptions {
return {
root: null,
rootMargin: '0px',
threshold: 1.0
}
}

get defaultOptionsPosition (): OptionsPosition {
return {
position: 'absolute',
top: 'calc(100% + 4px)',
left: '0'
}
}

onSelectClip = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
entries.map((entry: IntersectionObserverEntry) => {
console.log({ entry })
})
}

onSelectClick = () => {
if (!this.state.show && this.selectOptions) {
if (!this.intersectionObserver) {
this.intersectionObserver = new IntersectionObserver(this.onSelectClip, this.observerOptions)
this.intersectionObserver.observe(this.selectOptions)
}
}

this.setState({
show: !this.state.show
})
@@ -142,6 +222,10 @@ export default class Select extends React.PureComponent<Props, State> {
})
}

refSet = (node: HTMLDivElement) => {
this.selectOptions = node
}

render () {
const { id, children, disabled, value, type, floating, showAllContents } = this.props

@@ -172,7 +256,9 @@ export default class Select extends React.PureComponent<Props, State> {
</StyledSelectArrow>
</StyledSelect>
<StyledOptions
innerRef={this.refSet}
show={this.state.show}
opts={this.state.optionsPosition}
showAllContents={showAllContents}
>
{data}
@@ -3,7 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import styled, { css } from 'styled-components'
import { Props } from './index'
import { Props, OptionsPosition } from './index'

const getSelectColors = (p: StyleProps) => {
let color = '#686978'
@@ -43,8 +43,11 @@ interface StyleProps extends Props {
show?: boolean
selected?: boolean
showAllContents?: boolean
opts?: OptionsPosition
}

const getOptsAtt = (opts: any | undefined, att: string) => opts[att] || false

export const StyledWrapper = styled<StyleProps, 'div'>('div')`
width: 100%;
`
@@ -83,7 +86,9 @@ export const StyledSelectText = styled<StyleProps, 'div'>('div')`
`

export const StyledOptions = styled<StyleProps, 'div'>('div')`
position: fixed;
position: ${p => getOptsAtt(p.opts, 'position') || 'absolute'};
top: ${p => getOptsAtt(p.opts, 'top') || 'calc(100% + 4px)'};
left: ${p => getOptsAtt(p.opts, 'left') || '0'};
width: ${p => p.showAllContents ? 'auto' : '100%'};
border-radius: 3px;
box-shadow: 0 2px 5px 0 rgba(223, 223, 232, 0.5);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.