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

feat(TypePicker): create TypePicker and other needed components #390

Merged
merged 4 commits into from
Jan 14, 2020
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
17 changes: 11 additions & 6 deletions app/components/chrome/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import {
faPlus,
faDownload,
faFileAlt,
faCopy
faCopy,
faTimes,
faCheck
} from '@fortawesome/free-solid-svg-icons'

interface IconProps {
Expand All @@ -28,8 +30,8 @@ interface IconProps {
// size sm: .875em
// md: 1.33em
// lg: 2em
size: 'sm' | 'md' | 'lg'
color: 'light' | 'medium' | 'dark'
size?: 'xs' | 'sm' | 'md' | 'lg'
color?: 'light' | 'medium' | 'dark'
}

const icons: {[key: string]: any} = {
Expand Down Expand Up @@ -58,17 +60,20 @@ const icons: {[key: string]: any} = {
'readme': faGlasses,
'commit': faQuestionCircle,
'lock': faLock,
'transform': faCode
'transform': faCode,
'close': faTimes,
'check': faCheck
}

export const iconsList = Object.keys(icons)

const Icon: React.FunctionComponent<IconProps> = ({
icon = 'any',
icon = 'unknown',
size = 'md',
color = 'light'
color = 'dark'
}) => {
const sizes: {[key: string]: FontAwesomeIconProps['size']} = {
'xs': 'xs',
'sm': 'sm',
'md': 'lg',
'lg': '2x'
Expand Down
28 changes: 28 additions & 0 deletions app/components/item/DataType.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react'
import Icon from '../chrome/Icon'

export type DataTypes = 'string' | 'integer' | 'number' | 'boolean' | 'null' | 'any' | 'array' | 'object'
Copy link
Contributor

Choose a reason for hiding this comment

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

This list of types appears in four places: here, app/components/overlay/TypePickerOverlay.tsx, app/components/structure/TypePicker.tsx, and stories/3-Structure.stories.tsx. Could we line-up them up everywhere so that they always follow the same order? It seems like app/components/structure/TypePicker.tsx has the order that matters most, which would mean changing it here.

Copy link
Member Author

@ramfox ramfox Jan 14, 2020

Choose a reason for hiding this comment

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

Okay cool. Now there is one place defining the different strings that count as DataTypes, that is in the DataType component referenced here.

For the actual array of types (defining the order and also adding descriptions), we reference the typesAndDescriptions array defined in TypePicker


interface DataTypeProps {
type: DataTypes
description: string
width?: number
}

const DataType: React.FunctionComponent<DataTypeProps> = ({
type = 'unknown',
description = 'unknown',
width = 200
}) => {
return (<div className='data-type' style={{ width }}>
<div className='type-wrap'>
<div className='label large'>{type}</div>
<Icon icon={type} size='sm' color='medium' />
</div>
<div className='description small'>
{description}
</div>
</div>)
}

export default DataType
45 changes: 45 additions & 0 deletions app/components/nav/TabPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as React from 'react'

import classNames from 'classnames'

interface TabPickerProps {
size: 'sm' | 'md'
color: 'light' | 'dark'
tabs: string[]
onClick: (e: MouseEvent<HTMLDivElement, MouseEvent>) => void
activeTab: string
}

const TabPicker: React.FunctionComponent<TabPickerProps> = ({
size = 'sm',
color = 'dark',
tabs,
onClick,
activeTab
}) => {
return (
<div className={
classNames('tab-picker', {
'small': size === 'sm',
'medium': size === 'md',
'light': color === 'light',
'dark': color === 'dark'
})
}>
{
tabs.map((name: string, i: number) => {
return (<div
key={i}
onClick={onClick}
className={classNames({ 'active': name === activeTab })}
data-value={name}
>
{name}
</div>)
})
}
</div>
)
}

export default TabPicker
68 changes: 68 additions & 0 deletions app/components/overlay/Overlay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable @typescript-eslint/restrict-plus-operands */
import * as React from 'react'
import Icon from '../chrome/Icon'
import classNames from 'classnames'

interface OverlayProps {
title: string
onCancel: () => void
open: boolean
height: number
width: number
navigation: Element | undefined
}

const Overlay: React.FunctionComponent<OverlayProps> = ({
height,
width,
title,
onCancel,
open,
children,
navigation
}) => {
const overlayRef = React.useRef()

React.useEffect(() => {
const isInOverlay = (e: MouseEvent<HTMLDivElement, MouseEvent>) => {
if (!onCancel || !overlayRef || !overlayRef.current) return

// figure out if the user clicking inside the overlay
const rect = overlayRef.current.getBoundingClientRect()

// http://stackoverflow.com/a/26984690/2114
const isIn =
rect.top <= e.clientY &&
e.clientY <= rect.top + rect.height &&
rect.left <= e.clientX &&
e.clientX <= rect.left + rect.width

if (!isIn) {
e.stopPropagation()
onCancel()
}
}

window.addEventListener('click', isInOverlay)
return () => (
window.addEventListener('click', isInOverlay)
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be return function() { ... window.removeEventListener ...} to cleanup?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes thank you... how... was this working?

)
})

return (
<div
style={{ height, width }}
className={classNames('overlay', { 'closed': !open })}
ref={overlayRef}
>
<div className='title-bar'>
<div className='label small'>{title}</div>
<div className='pointer' onClick={onCancel}><Icon icon='close' size='xs' /></div>
</div>
{navigation && <div className='nav'>{navigation}</div>}
<div className='content'>{children}</div>
</div>
)
}

export default Overlay
64 changes: 64 additions & 0 deletions app/components/overlay/TypePickerOverlay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as React from 'react'
import Overlay from './Overlay'
import { typesAndDescriptions } from '../structure/TypePicker'
import DataType from '../item/DataType'
import Icon from '../chrome/Icon'

interface TypePickerOverlayProps {
// function to close the picker
onCancel: () => void
// function called when a data type is selected
onClick: (e: MouseEvent<HTMLDivElement, MouseEvent>) => void
// function called when a tab is selected
onTabClick: (e: MouseEvent<HTMLDivElement, MouseEvent>) => void
// type or types that are currently selected
value: string | string[]
// is the overlay open?
open: boolean
// navigation component, if it exists
navigation: Element | undefined
}

const TypePickerOverlay: React.FunctionComponent<TypePickerOverlayProps> = ({
onCancel,
onClick,
value,
navigation,
open = true
}) => {
const handleOnClick = (e: MouseEvent<HTMLDivElement, MouseEvent>) => {
e.stopPropagation()
onClick(e)
}
return (
<Overlay
title='data type'
onCancel={onCancel}
width={220}
height={200}
open={open}
navigation={navigation}
>
{typesAndDescriptions.map((item, i) => {
return (
<div
key={i}
className='type-picker-overlay-row'
onClick={handleOnClick}
data-value={item.type}
>
<div className='icon-wrap'><Icon
icon='check'
size='xs'
color={
(typeof value === 'string' && value === item.type) || (Array.isArray(value) && value.includes(item.type)) ? 'dark' : 'light'}/>
</div>
<DataType type={item.type} description={item.description} />
</div>
)
})}
</Overlay>
)
}

export default TypePickerOverlay
38 changes: 38 additions & 0 deletions app/components/structure/ColumnType.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react'
import classNames from 'classnames'
import { DataTypes } from '../item/DataType'

import Icon from '../chrome/Icon'

export interface ColumnTypeProps {
type: DataTypes | DataTypes[]
onClick: (e) => void
active?: boolean
}

const ColumnType: React.FunctionComponent<ColumnTypeProps> = ({
type = 'any',
onClick,
active = false
}) => {
let shownType = ''
if (Array.isArray(type)) {
if (type.length > 1) {
shownType = 'multi'
} else if (type.length === 0) {
shownType = 'any'
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should have a branch for type.length == 0, just to avoid the undefined case.

shownType = type[0]
}
} else shownType = type

return <div
className={classNames('column-type', { 'clickable': !!onClick, 'active': active })}
onClick={onClick}
tabIndex={0}
>
<span className='label large'>{shownType}</span> <Icon icon={shownType === 'multi' ? 'unknown' : shownType} size='sm' color='medium'/>
</div>
}

export default ColumnType
Loading