Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the classnames library in TextWidget #315

Merged
merged 8 commits into from Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/building-a-form/about-the-schema-and-uischema-objects.md
Expand Up @@ -184,10 +184,14 @@ The us-forms-system code includes additional `uiSchema` functionality not found
'value': <p>Some text</p>
},

// A string of class names that are added to the widget for the current field.
// A list of class names that are added to the widget for the current field.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the docs update! Since the documentation on this option applies to several widgets I think they all should be changed for consistency. The other references are in CurrencyWidget, SelectWidget, and ArrayCountWidget. Can you update those as well? I'm fine with just having the existing test on TestWidget for class names rather than duplicating it for the other widgets.

// `widgetClassNames` is similar to the default `classNames` property, but it puts the
// class names on the input/select/etc element itself, rather than a surrounding `div`.
widgetClassNames: '',
// `widgetClassNames` accepts anything that the classnames library accepts.
// See https://github.com/JedWatson/classnames#usage for more details.
widgetClassNames: 'first-class second-class',
widgetClassNames: ['first-class', 'second-class'],
widgetClassNames: { 'first-class': true, 'second-class': false },

// For array fields, this component is shown when the item in the array is rendered as
// read-only on a page that is not a review page.
Expand Down
3 changes: 2 additions & 1 deletion src/js/widgets/ArrayCountWidget.jsx
@@ -1,4 +1,5 @@
import React from 'react';
import classnames from 'classnames';

export default class ArrayCountWidget extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -69,7 +70,7 @@ export default class ArrayCountWidget extends React.Component {
name={props.id}
disabled={props.disabled}
autoComplete={props.options.autocomplete || false}
className={props.options.widgetClassNames}
className={classnames(props.options.widgetClassNames)}
value={typeof this.state.userCount === 'undefined' ? '' : this.state.userCount}
onBlur={() => props.onBlur(props.id)}
onChange={this.updateArrayLength}/>
Expand Down
3 changes: 2 additions & 1 deletion src/js/widgets/CurrencyWidget.jsx
@@ -1,4 +1,5 @@
import React from 'react';
import classnames from 'classnames';

export default class CurrencyWidget extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class CurrencyWidget extends React.Component {
name={id}
disabled={disabled}
autoComplete={options.autocomplete || false}
className={options.widgetClassNames}
className={classnames(this.props.options.widgetClassNames)}
value={typeof value === 'undefined' ? '' : value}
onBlur={this.onBlur}
onChange={this.handleChange}/>
Expand Down
3 changes: 2 additions & 1 deletion src/js/widgets/SelectWidget.jsx
@@ -1,4 +1,5 @@
import React from 'react';
import classnames from 'classnames';
import { asNumber } from '@department-of-veterans-affairs/react-jsonschema-form/lib/utils';
import onlyUpdateForKeys from 'recompose/onlyUpdateForKeys';

Expand Down Expand Up @@ -34,7 +35,7 @@ function SelectWidget({
id={id}
name={id}
multiple={multiple}
className={options.widgetClassNames}
className={classnames(options.widgetClassNames)}
value={value || ''}
required={required}
disabled={disabled}
Expand Down
3 changes: 2 additions & 1 deletion src/js/widgets/TextWidget.jsx
@@ -1,4 +1,5 @@
import React from 'react';
import classnames from 'classnames';

const numberTypes = new Set(['number', 'integer']);

Expand All @@ -14,7 +15,7 @@ export default function TextWidget(props) {
disabled={props.disabled}
maxLength={props.schema.maxLength}
autoComplete={props.options.autocomplete || false}
className={props.options.widgetClassNames}
className={classnames(props.options.widgetClassNames)}
value={typeof props.value === 'undefined' ? '' : props.value}
onBlur={() => props.onBlur(props.id)}
onChange={(event) => props.onChange(event.target.value ? event.target.value : undefined)}/>
Expand Down
50 changes: 50 additions & 0 deletions test/js/widgets/TextWidget.unit.spec.jsx
Expand Up @@ -84,4 +84,54 @@ describe('Schemaform <TextWidget>', () => {
tree.subTree('input').props.onBlur();
expect(onBlur.calledWith('1')).to.be.true;
});
it('should accept a string widgetClassNames prop', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tests!

const tree = SkinDeep.shallowRender(
<TextWidget
id="1"
value="testing"
schema={{ type: 'string' }}
required
disabled={false}
options={{
widgetClassNames: 'first-class second-class'
}}/>
);
expect(SkinDeep.hasClass(tree, 'first-class')).to.be.true;
expect(SkinDeep.hasClass(tree, 'second-class')).to.be.true;
});
it('should accept an array widgetClassNames prop', () => {
const tree = SkinDeep.shallowRender(
<TextWidget
id="1"
value="testing"
schema={{ type: 'string' }}
required
disabled={false}
options={{
widgetClassNames: ['first-class', 'second-class']
}}/>
);
expect(SkinDeep.hasClass(tree, 'first-class')).to.be.true;
expect(SkinDeep.hasClass(tree, 'second-class')).to.be.true;
});
it('should accept an object widgetClassNames prop', () => {
const tree = SkinDeep.shallowRender(
<TextWidget
id="1"
value="testing"
schema={{ type: 'string' }}
required
disabled={false}
options={{
widgetClassNames: {
'first-class': true,
'second-class': true,
'third-class': false
}
}}/>
);
expect(SkinDeep.hasClass(tree, 'first-class')).to.be.true;
expect(SkinDeep.hasClass(tree, 'second-class')).to.be.true;
expect(SkinDeep.hasClass(tree, 'third-class')).to.be.false;
});
});