From ada3357cf532a9163a471069c4c0bb429099bc83 Mon Sep 17 00:00:00 2001 From: Gary Thomas Date: Wed, 1 Aug 2018 17:05:11 -0700 Subject: [PATCH] gt - Update FormChoice to support Bootstrap 4 - Change markup - remove deprecated props - Move to class Component --- src/components/FormChoice.d.ts | 2 - src/components/FormChoice.js | 99 ++++++++++++++++++------------ test/components/FormChoice.spec.js | 10 ++- 3 files changed, 64 insertions(+), 47 deletions(-) diff --git a/src/components/FormChoice.d.ts b/src/components/FormChoice.d.ts index 48033a965..8fba9a103 100644 --- a/src/components/FormChoice.d.ts +++ b/src/components/FormChoice.d.ts @@ -3,8 +3,6 @@ import * as React from 'react'; interface FormChoiceProps extends Omit, 'disabled'> { inline?: boolean; - color?: string; - state?: string; disabled?: boolean; checked?: boolean; type: 'checkbox' | 'radio' | 'select'; diff --git a/src/components/FormChoice.js b/src/components/FormChoice.js index e7f03f490..21ba535cc 100644 --- a/src/components/FormChoice.js +++ b/src/components/FormChoice.js @@ -1,60 +1,81 @@ import React from 'react'; +import PropTypes from 'prop-types'; import classname from 'classnames'; import FormGroup from './FormGroup'; import Input from './Input'; import Label from './Label'; -const FormChoice = props => { - const { - inline, - color, - state, - disabled, - checked, - children, - type, - value, - selected, - ...attributes - } = props; - - if (type === 'select') { - return ; + } - const computedValue = value || children; + const labelClasses = classname('form-check', { 'form-check-inline': inline }); - let computedChecked; + const computedValue = value || children; - if (type === 'checkbox') { - computedChecked = selected && selected.indexOf(computedValue) > -1; - } else if (type === 'radio') { - computedChecked = selected && selected === computedValue; - } else { - throw new Error(`Type '${type}' is not supported`); - } + let computedChecked; + + if (type === 'checkbox') { + computedChecked = selected && selected.indexOf(computedValue) > -1; + } else if (type === 'radio') { + computedChecked = selected && selected === computedValue; + } else { + throw new Error(`Type '${type}' is not supported`); + } - const item = ( - - ); + const item = ( +
+ + +
+ ); - if (inline) { - return item; - } else { + if (inline) { + return item; + } return ( + > + {item} + ); } -}; +} export default FormChoice; diff --git a/test/components/FormChoice.spec.js b/test/components/FormChoice.spec.js index a8f5055f3..9779fbf0c 100644 --- a/test/components/FormChoice.spec.js +++ b/test/components/FormChoice.spec.js @@ -38,7 +38,7 @@ describe('', () => { ); assert.equal(component.find(Input).prop('type'), 'checkbox'); - assert.equal(component.find('span').text(), 'my type'); + assert.equal(component.find(Label).props().children, 'my type'); assert.equal(component.type(), FormGroup); }); @@ -48,8 +48,7 @@ describe('', () => { ); assert.equal(component.find(Input).prop('type'), 'checkbox'); - assert.equal(component.find('span').text(), 'my type'); - assert.equal(component.type(), Label); + assert.equal(component.find(Label).props().children, 'my type'); }); describe('with computed value', () => { @@ -120,7 +119,7 @@ describe('', () => { ); assert.equal(component.find(Input).prop('type'), 'radio'); - assert.equal(component.find('span').text(), 'my type'); + assert.equal(component.find(Label).props().children, 'my type'); assert.equal(component.type(), FormGroup); }); @@ -130,8 +129,7 @@ describe('', () => { ); assert.equal(component.find(Input).prop('type'), 'radio'); - assert.equal(component.find('span').text(), 'my type'); - assert.equal(component.type(), Label); + assert.equal(component.find(Label).props().children, 'my type'); }); describe('with computed value', () => {