From 524627de9d55fe177a224010312982e74d1bfd56 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Sun, 7 Oct 2018 10:14:13 +0200 Subject: [PATCH] feat(ui): add Form components --- app/components/UI/FormFieldMessage.js | 43 +++++ app/components/UI/Input.js | 90 ++++++++++ app/components/UI/Label.js | 20 +++ app/components/UI/Range.js | 57 +++++++ app/components/UI/Select.js | 156 +++++++++++++++++ app/components/UI/TextArea.js | 86 ++++++++++ app/components/UI/Toggle.js | 70 ++++++++ app/themes/dark.js | 14 +- package.json | 3 + stories/components/form.stories.js | 140 ++++++++++++++++ test/unit/components/UI/Input.spec.js | 21 +++ test/unit/components/UI/Range.spec.js | 21 +++ test/unit/components/UI/Select.spec.js | 29 ++++ test/unit/components/UI/TextArea.spec.js | 21 +++ test/unit/components/UI/Toggle.spec.js | 21 +++ .../UI/__snapshots__/Input.spec.js.snap | 51 ++++++ .../UI/__snapshots__/Range.spec.js.snap | 48 ++++++ .../UI/__snapshots__/Select.spec.js.snap | 157 ++++++++++++++++++ .../UI/__snapshots__/TextArea.spec.js.snap | 51 ++++++ .../UI/__snapshots__/Toggle.spec.js.snap | 85 ++++++++++ yarn.lock | 42 ++++- 21 files changed, 1224 insertions(+), 2 deletions(-) create mode 100644 app/components/UI/FormFieldMessage.js create mode 100644 app/components/UI/Input.js create mode 100644 app/components/UI/Label.js create mode 100644 app/components/UI/Range.js create mode 100644 app/components/UI/Select.js create mode 100644 app/components/UI/TextArea.js create mode 100644 app/components/UI/Toggle.js create mode 100644 stories/components/form.stories.js create mode 100644 test/unit/components/UI/Input.spec.js create mode 100644 test/unit/components/UI/Range.spec.js create mode 100644 test/unit/components/UI/Select.spec.js create mode 100644 test/unit/components/UI/TextArea.spec.js create mode 100644 test/unit/components/UI/Toggle.spec.js create mode 100644 test/unit/components/UI/__snapshots__/Input.spec.js.snap create mode 100644 test/unit/components/UI/__snapshots__/Range.spec.js.snap create mode 100644 test/unit/components/UI/__snapshots__/Select.spec.js.snap create mode 100644 test/unit/components/UI/__snapshots__/TextArea.spec.js.snap create mode 100644 test/unit/components/UI/__snapshots__/Toggle.spec.js.snap diff --git a/app/components/UI/FormFieldMessage.js b/app/components/UI/FormFieldMessage.js new file mode 100644 index 00000000..abdc21d0 --- /dev/null +++ b/app/components/UI/FormFieldMessage.js @@ -0,0 +1,43 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { Box, Flex } from 'rebass' +import SystemSuccess from 'components/Icon/SystemSuccess' +import SystemWarning from 'components/Icon/SystemWarning' +import SystemError from 'components/Icon/SystemError' + +import styled from 'styled-components' +import { variant } from 'styled-system' + +const messageStyle = variant({ key: 'messages' }) +const Message = styled(Flex)(messageStyle) + +/** + * @render react + * @name FormFieldMessage + * @example + * + */ +class FormFieldMessage extends React.Component { + static displayName = 'FormFieldMessage' + + static propTypes = { + variant: PropTypes.string, + children: PropTypes.node + } + + render() { + const { children, variant } = this.props + return ( + + + {variant === 'success' && } + {variant === 'warning' && } + {variant === 'error' && } + + {children} + + ) + } +} + +export default FormFieldMessage diff --git a/app/components/UI/Input.js b/app/components/UI/Input.js new file mode 100644 index 00000000..003cbc61 --- /dev/null +++ b/app/components/UI/Input.js @@ -0,0 +1,90 @@ +import React from 'react' +import { asField } from 'informed' +import { Input as Base } from 'styled-system-html' +import { withTheme } from 'styled-components' +import { Flex } from 'rebass' +import FormFieldMessage from 'components/UI/FormFieldMessage' + +/** + * @render react + * @name Input + * @example + * + */ +class Input extends React.PureComponent { + static displayName = 'Input' + + render() { + const { + onChange, + onBlur, + initialValue, + field, + forwardedRef, + theme, + fieldApi, + fieldState, + justifyContent, + ...rest + } = this.props + const { setValue, setTouched } = fieldApi + const { value } = fieldState + + let borderColor + + if (fieldState.touched) { + if (fieldState.error) { + borderColor = theme.colors.superRed + } else if (value && !fieldState.error) { + borderColor = theme.colors.superGreen + } + } + + return ( + + { + setValue(e.target.value) + if (onChange) { + onChange(e) + } + }} + onBlur={e => { + setTouched() + if (onBlur) { + onBlur(e) + } + }} + error={fieldState.error} + /> + {fieldState.error && ( + + {fieldState.error} + + )} + + ) + } +} + +export default asField(withTheme(Input)) diff --git a/app/components/UI/Label.js b/app/components/UI/Label.js new file mode 100644 index 00000000..33f8c999 --- /dev/null +++ b/app/components/UI/Label.js @@ -0,0 +1,20 @@ +import React from 'react' +import { Label as Base } from 'styled-system-html' + +/** + * @render react + * @name Label + * @example + *