-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from all commits
279dd52
bd901a1
2b6bca3
32d4c5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' | ||
|
||
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 |
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 |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should have a branch for |
||
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 theDataType
component referenced here.For the actual array of types (defining the order and also adding descriptions), we reference the
typesAndDescriptions
array defined inTypePicker