import React, { Fragment } from 'react';
import { Margin, Padding } from 'styled-components-spacing';
import PropTypes from 'prop-types';
import styled, { withTheme } from 'styled-components';
import { Row, Col as BaseCol } from 'joyent-react-styled-flexboxgrid';
import { Field } from 'redux-form';
import remcalc from 'remcalc';
import titleCase from 'title-case';
import Flex, { FlexItem } from 'styled-flex-component';
import is from 'styled-is';
import {
H4,
Button,
Message,
MessageTitle,
MessageDescription,
Card as BaseCard,
CardOutlet,
CardHeader,
CardHeaderMeta,
FormGroup,
Input,
Textarea,
FormLabel,
FormMeta,
DeleteIcon,
ArrowIcon
} from 'joyent-ui-toolkit';
const CollapsedKeyValue = styled.div`
word-break: break-all;
line-height: 1.5;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: block;
`;
const Card = styled(BaseCard)`
${is('borderless')`
border: none;
box-shadow: none;
`};
`;
const Header = styled(CardHeader)`
border-top: 0;
border-left: 0;
border-right: 0;
`;
const MarginalButton = styled(Button)`
margin-right: ${remcalc(6)};
`;
const PaddingMaxWidth = styled(Padding)`
word-wrap: break-word;
overflow-wrap: break-word;
width: 100%;
box-sizing: border-box;
`;
const Meta = styled(CardHeaderMeta)`
height: ${remcalc(47)};
max-width: 98%;
`;
const Bold = styled.span`
font-weight: ${props => props.theme.font.weight.semibold};
`;
const Col = styled(BaseCol)`
${is('mobile')`
display: flex;
flex-direction: column-reverse;
`};
`;
const TextareaKeyValue = ({
id = null,
type,
submitting,
onlyName,
onlyValue,
editor,
mobile
}) => (
{onlyValue ? null : (
{titleCase(type)} key
)}
{onlyName ? null : (
{titleCase(type)} value
)}
);
const InputKeyValue = ({
id = null,
type,
submitting,
typeLabel,
onlyName,
onlyValue,
fluid = false
}) => (
{onlyValue ? null : (
{titleCase(type)} {typeLabel}
)}
{onlyName ? null : (
{titleCase(type)} value
)}
);
export const KeyValue = ({
id = null,
disabled = false,
input = 'input',
type = 'metadata',
typeLabel = 'key',
method = 'add',
initialValues = {},
error = null,
expanded = true,
submitting = false,
pristine = true,
invalid = false,
removing = false,
onToggleExpanded,
onCancel = () => null,
onRemove = () => null,
theme = {},
editor = null,
onlyName = false,
onlyValue = false,
noRemove = false,
borderless = false,
shadow = true,
customHeader,
headless = false
}) => {
const handleHeaderClick = method === 'edit' && onToggleExpanded;
const mobile = theme.screen === 'mobile';
return (
{headless ? null : (
{method === 'add' || method === 'create' ? (
{`${titleCase(method)} ${type}`}
) : (
{customHeader ? customHeader : null}
{initialValues.name ? (
{expanded ? (
{`${initialValues.name}${': '}`}
) : (
{`${initialValues.name}${': '}`}
)}
{initialValues.value}
) : null}
)}
{handleHeaderClick ? (
) : null}
)}
{expanded ? (
{error && !submitting ? (
Ooops!
{error}
) : null}
{input === 'input' ? (
) : null}
{input === 'textarea' ? (
) : null}
{input !== 'textarea' && input !== 'input'
? input(submitting)
: null}
Cancel
{!noRemove && (
)}
) : null}
);
};
KeyValue.propTypes = {
input: PropTypes.oneOf(['input', 'textarea']).isRequired,
type: PropTypes.string.isRequired,
method: PropTypes.oneOf(['add', 'edit']).isRequired,
initialValues: PropTypes.shape({
name: PropTypes.string,
value: PropTypes.string
}).isRequired,
removing: PropTypes.bool.isRequired,
expanded: PropTypes.bool.isRequired,
onToggleExpanded: PropTypes.func,
onCancel: PropTypes.func,
onRemove: PropTypes.func
};
export default withTheme(({ handleSubmit, ...rest }) => (
));