Skip to content

Commit

Permalink
#7 add disabled prop to submit button, need that.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Apr 9, 2019
1 parent 4acbc96 commit c2d6923
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {FormTokenField} from '@wordpress/components';
import { BaseControl } from '@wordpress/components';
import React from "react";
import {fieldClassNames} from "../util";
import {fieldClassNames, parseAttributes} from "../util";
export const SubmitButton = (props) => {
const {
label,
Expand All @@ -12,8 +12,15 @@ export const SubmitButton = (props) => {
value,
onChange,
onBlur,
options
options,
attributes
} = props;
const _attributes = {
disabled: 'object' === typeof attributes
&& attributes.hasOwnProperty('disabled')
&& true == attributes.disabled,
}
const {disabled} = _attributes;
return (
<BaseControl
id={fieldId}
Expand All @@ -23,6 +30,7 @@ export const SubmitButton = (props) => {
type={'submit'}
id={fieldId}
value={label}
disabled={disabled}
/>
</BaseControl>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import renderer from 'react-test-renderer';

import {mount} from 'enzyme';
import { SubmitButton } from './SubmitButton';

describe('Select Field component', () => {
Expand All @@ -19,5 +19,43 @@ describe('Select Field component', () => {
expect(component.toJSON()).toMatchSnapshot();
});

it('Passes disabled attribute when set to true', () => {
const component = mount(
<SubmitButton
label={'Send Message'}
description={'Click to sendu'}
attributes={{
disabled: true
}}
fieldId={'button'}
/>
);
expect(component.find('input').props().disabled ).toBe(true);
});

it('Does not disabled attribute when set to false', () => {
const component = mount(
<SubmitButton
label={'Send Message'}
description={'Click to sendu'}
attributes={{
disabled: false
}}
fieldId={'button'}
/>
);
expect(component.find('input').props().disabled ).toBe(false);
});

it('Does not disabled attribute when not supplied', () => {
const component = mount(
<SubmitButton
label={'Send Message'}
description={'Click to sendu'}
fieldId={'button'}
/>
);
expect(component.find('input').props().disabled ).toBe(false);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports[`Select Field component matches snapshot 1`] = `
className="components-base-control__field"
>
<input
disabled={false}
id="button"
type="submit"
value="Send Message"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export const parseAttributes = (attributes, allowed = null) => {
case 'textarea':
allowed = textAreaAttrs;
break;
case 'submit':
allowed = [ ...['disabled'], allowed ];
break;
case 'text':
case 'default':
case null:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ exports[`fieldFactory Creates a submit button 1`] = `
className="components-base-control__field"
>
<input
disabled={false}
id="clickButton"
type="submit"
value="Click Me"
Expand Down
4 changes: 2 additions & 2 deletions packages/forms/src/CalderaForm/CalderaForm.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ const form = {
},
{
type: 'disable',
rule: createFieldRule('is', emailField.fieldId, '' ),
rule: createFieldRule('empty', emailField.fieldId, null ),
fields: [
submitButton.fieldId
]
},
{
type: 'disable',
rule: createFieldRule('is', textField.fieldId, '' ),
rule: createFieldRule('empty', textField.fieldId,null ),
fields: [
submitButton.fieldId
]
Expand Down

0 comments on commit c2d6923

Please sign in to comment.