From 73bead91f93d6386d7304a71f2d68f6c8155d404 Mon Sep 17 00:00:00 2001 From: Jenni Nurmi Date: Tue, 11 Dec 2018 12:21:49 +0200 Subject: [PATCH] Create FieldRadioButton component --- .../FieldRadioButton/FieldRadioButton.css | 64 ++++++++++++++ .../FieldRadioButton.example.js | 72 ++++++++++++++++ .../FieldRadioButton/FieldRadioButton.js | 84 +++++++++++++++++++ src/components/index.js | 1 + src/examples.js | 2 + 5 files changed, 223 insertions(+) create mode 100644 src/components/FieldRadioButton/FieldRadioButton.css create mode 100644 src/components/FieldRadioButton/FieldRadioButton.example.js create mode 100644 src/components/FieldRadioButton/FieldRadioButton.js diff --git a/src/components/FieldRadioButton/FieldRadioButton.css b/src/components/FieldRadioButton/FieldRadioButton.css new file mode 100644 index 00000000..f2a933be --- /dev/null +++ b/src/components/FieldRadioButton/FieldRadioButton.css @@ -0,0 +1,64 @@ +@import '../../marketplace.css'; + +.root { + position: relative; +} + +.input { + position: absolute; + opacity: 0; + height: 0; + width: 0; + + /* Display the "check" when checked */ + &:checked + label .checked { + display: inline; + } + + /* Hightlight the text on checked, hover and focus */ + &:focus + label .text, + &:hover + label .text, + &:checked + label .text { + color: var(--matterColorDark); + } +} + +.label { + display: flex; + align-items: center; + padding: 0; +} + +.radioButtonWrapper { + /* This should follow line-height */ + height: 32px; + margin-top: -1px; + margin-right: 12px; + align-self: baseline; + + display: inline-flex; + align-items: center; + cursor: pointer; +} + +.checked { + display: none; + stroke: var(--marketplaceColor); + fill: var(--marketplaceColor); +} + +.notChecked { + stroke: var(--matterColorAnti); +} + +.required { + stroke: var(--attentionColor); +} + +.text { + @apply --marketplaceListingAttributeFontStyles; + color: var(--matterColor); + margin-top: -1px; + margin-bottom: 1px; + cursor: pointer; +} diff --git a/src/components/FieldRadioButton/FieldRadioButton.example.js b/src/components/FieldRadioButton/FieldRadioButton.example.js new file mode 100644 index 00000000..c08289ad --- /dev/null +++ b/src/components/FieldRadioButton/FieldRadioButton.example.js @@ -0,0 +1,72 @@ +import React from 'react'; +import { Form as FinalForm, FormSpy } from 'react-final-form'; +import { Button } from '..'; +import FieldRadioButton from './FieldRadioButton'; +const formName = 'Styleguide.FieldRadioButton.Form'; + +const FormComponent = props => ( + { + const { handleSubmit, onChange, invalid, pristine, submitting } = fieldRenderProps; + + const submitDisabled = invalid || pristine || submitting; + + const required = true; + + const showAsRequired = pristine && required; + + return ( +
{ + e.preventDefault(); + handleSubmit(e); + }} + > + + + + + + + + ); + }} + /> +); + +export const RadioButtonRequired = { + component: FormComponent, + props: { + onChange: formState => { + if (Object.keys(formState.values).length > 0) { + console.log('form values changed to:', formState.values); + } + }, + onSubmit: values => { + console.log('Submit values of FieldRadioButton: ', values); + }, + }, + group: 'inputs', +}; diff --git a/src/components/FieldRadioButton/FieldRadioButton.js b/src/components/FieldRadioButton/FieldRadioButton.js new file mode 100644 index 00000000..f5a82506 --- /dev/null +++ b/src/components/FieldRadioButton/FieldRadioButton.js @@ -0,0 +1,84 @@ +import React from 'react'; +import { node, string } from 'prop-types'; +import classNames from 'classnames'; +import { Field } from 'react-final-form'; + +import css from './FieldRadioButton.css'; + +const IconRadioButton = props => { + return ( +
+ + + + + + + + +
+ ); +}; + +IconRadioButton.defaultProps = { className: null }; + +IconRadioButton.propTypes = { className: string }; + +const FieldRadioButtonComponent = props => { + const { rootClassName, className, svgClassName, id, label, showAsRequired, ...rest } = props; + + const classes = classNames(rootClassName || css.root, className); + const radioButtonProps = { + id, + className: css.input, + component: 'input', + type: 'radio', + ...rest, + }; + + return ( + + + + + ); +}; + +FieldRadioButtonComponent.defaultProps = { + className: null, + rootClassName: null, + svgClassName: null, + label: null, +}; + +FieldRadioButtonComponent.propTypes = { + className: string, + rootClassName: string, + svgClassName: string, + + // Id is needed to connect the label with input. + id: string.isRequired, + label: node, + + // Name groups several RadioButtones to an array of selected values + name: string.isRequired, + + // RadioButton needs a value that is passed forward when user checks the RadioButton + value: string.isRequired, +}; + +export default FieldRadioButtonComponent; diff --git a/src/components/index.js b/src/components/index.js index 6c7ceb7c..a59280c9 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -21,6 +21,7 @@ export { default as FieldCheckboxGroup } from './FieldCheckboxGroup/FieldCheckbo export { default as FieldCurrencyInput } from './FieldCurrencyInput/FieldCurrencyInput'; export { default as FieldDateInput } from './FieldDateInput/FieldDateInput'; export { default as FieldDateRangeInput } from './FieldDateRangeInput/FieldDateRangeInput'; +export { default as FieldRadioButton } from './FieldRadioButton/FieldRadioButton'; export { default as FieldPhoneNumberInput } from './FieldPhoneNumberInput/FieldPhoneNumberInput'; export { default as FieldReviewRating } from './FieldReviewRating/FieldReviewRating'; export { default as FieldSelect } from './FieldSelect/FieldSelect'; diff --git a/src/examples.js b/src/examples.js index fe7ec969..0c75352a 100644 --- a/src/examples.js +++ b/src/examples.js @@ -13,6 +13,7 @@ import * as FieldCurrencyInput from './components/FieldCurrencyInput/FieldCurren import * as FieldDateInput from './components/FieldDateInput/FieldDateInput.example'; import * as FieldDateRangeInput from './components/FieldDateRangeInput/FieldDateRangeInput.example'; import * as FieldPhoneNumberInput from './components/FieldPhoneNumberInput/FieldPhoneNumberInput.example'; +import * as FieldRadioButton from './components/FieldRadioButton/FieldRadioButton.example'; import * as FieldRangeSlider from './components/FieldRangeSlider/FieldRangeSlider.example'; import * as FieldReviewRating from './components/FieldReviewRating/FieldReviewRating.example'; import * as FieldSelect from './components/FieldSelect/FieldSelect.example'; @@ -107,6 +108,7 @@ export { FieldDateInput, FieldDateRangeInput, FieldPhoneNumberInput, + FieldRadioButton, FieldRangeSlider, FieldReviewRating, FieldSelect,