|
| 1 | +import React, { useState } from 'react' |
| 2 | + |
| 3 | +import { InputGroupButtonDropdown, DropdownToggle, DropdownMenu, DropdownItem, |
| 4 | + InputGroup } from 'reactstrap' |
| 5 | +import classnames from 'classnames' |
| 6 | + |
| 7 | +import Icon from '../../../../../Icon' |
| 8 | +import { FastField, useField } from 'formik' |
| 9 | + |
| 10 | +const CellTypeDropdown = ({ name, disabled=false }) => { |
| 11 | + const [field, , helpers] = useField(name) |
| 12 | + const [dropdownOpen, setDropdownOpen] = useState(false) |
| 13 | + |
| 14 | + return ( |
| 15 | + <InputGroupButtonDropdown |
| 16 | + addonType="append" |
| 17 | + disabled={ disabled } |
| 18 | + isOpen={ dropdownOpen } |
| 19 | + toggle={ () => setDropdownOpen(!dropdownOpen) } |
| 20 | + > |
| 21 | + <DropdownToggle |
| 22 | + caret={ !disabled } |
| 23 | + disabled={ disabled } |
| 24 | + outline color="secondary" |
| 25 | + // Ensure that the right-hand side always has rounded corners |
| 26 | + // (this didn't work if the button was disabled) |
| 27 | + className="rounded-right" |
| 28 | + > |
| 29 | + <Icon |
| 30 | + icon={{ |
| 31 | + string: 'font', |
| 32 | + number: 'tachometer', |
| 33 | + boolean: 'adjust' |
| 34 | + }[field.value]} |
| 35 | + fixedWidth |
| 36 | + /> |
| 37 | + </DropdownToggle> |
| 38 | + <DropdownMenu right> |
| 39 | + <DropdownItem header>Data type</DropdownItem> |
| 40 | + <DropdownItem |
| 41 | + className={ classnames({ |
| 42 | + 'dropdown-item-active': field.value === 'string' |
| 43 | + }) } |
| 44 | + onClick={ () => helpers.setValue('string') } |
| 45 | + > |
| 46 | + Text <span className="text-muted">(categorical)</span> |
| 47 | + </DropdownItem> |
| 48 | + <DropdownItem |
| 49 | + className={ classnames({ |
| 50 | + 'dropdown-item-active': field.value === 'number' |
| 51 | + }) } |
| 52 | + onClick={ () => helpers.setValue('number') } |
| 53 | + > |
| 54 | + Numerical <span className="text-muted">(continuous)</span> |
| 55 | + </DropdownItem> |
| 56 | + <DropdownItem |
| 57 | + className={ classnames({ |
| 58 | + 'dropdown-item-active': field.value === 'boolean' |
| 59 | + }) } |
| 60 | + onClick={ () => helpers.setValue('boolean') } |
| 61 | + > |
| 62 | + Boolean <span className="text-muted">(binary)</span> |
| 63 | + </DropdownItem> |
| 64 | + </DropdownMenu> |
| 65 | + </InputGroupButtonDropdown> |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +export const HeaderCell = ({ index }) => |
| 70 | + <InputGroup> |
| 71 | + <FastField |
| 72 | + name={ `templateParameters.columns[${ index }].name` } |
| 73 | + placeholder={ `parameter${ index }` } |
| 74 | + className="form-control text-monospace font-weight-bolder" |
| 75 | + style={{ height: '42px' }} |
| 76 | + /> |
| 77 | + <CellTypeDropdown name={ `templateParameters.columns[${ index }].type` } /> |
| 78 | + </InputGroup> |
| 79 | + |
| 80 | +export default ({ name, columns }) => |
| 81 | + <thead> |
| 82 | + <tr> |
| 83 | + <th></th> |
| 84 | + { Array(columns).fill(null).map((_, i) => |
| 85 | + <th key={ `${ name }-header-${ i }` }> |
| 86 | + <HeaderCell index={ i } /> |
| 87 | + </th> |
| 88 | + ) } |
| 89 | + <th></th> |
| 90 | + </tr> |
| 91 | + </thead> |
0 commit comments