Skip to content

Commit

Permalink
fix(checkbox): set true/false value
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSharpieOne committed Oct 10, 2016
1 parent 24f7801 commit 17a2673
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
30 changes: 30 additions & 0 deletions __test__/AvBaseInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,36 @@ describe('BaseInput', function() {
});
});

describe('get field value', () => {
it('should give the value of "checked" for a checkbox', () => {
this.props.type = 'checkbox';
const event = {target: {checked: {}}};
const result = this.component.getFieldValue(event);
expect(result).to.equal(event.target.checked);
});

it('should give the value of "value" for non checkboxs which have it defined', () => {
this.props.type = 'text';
const event = {target: {value: {}}};
const result = this.component.getFieldValue(event);
expect(result).to.equal(event.target.value);
});

it('should give the event for non checkboxs which do not have a target', () => {
this.props.type = 'text';
const event = {noTarget: {value: {}}};
const result = this.component.getFieldValue(event);
expect(result).to.equal(event);
});

it('should give the event for non checkboxs which do not have a value defined', () => {
this.props.type = 'text';
const event = {target: {noValue: {}}};
const result = this.component.getFieldValue(event);
expect(result).to.equal(event);
});
});

describe('validate event', () => {
it('should use getValidationEvent to get the validation event', () => {
const spy = sinon.spy(this.component, 'getValidationEvent');
Expand Down
17 changes: 11 additions & 6 deletions src/AvBaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const htmlValidationTypes = [
/*'range', 'month', 'week', 'time'*/ // These do not currently have validation
];

const getFieldValue = event => event && event.target && !isUndefined(event.target.value) ? event.target.value : event;

export default class AvBaseInput extends Component {
static propTypes = {
name: PropTypes.string.isRequired,
Expand Down Expand Up @@ -82,24 +80,24 @@ export default class AvBaseInput extends Component {
}

onInputHandler(_value) {
this.value = getFieldValue(_value);
this.value = this.getFieldValue(_value);
this.validateEvent('onInput');
!this.context.FormCtrl.isTouched[this.props.name] && this.context.FormCtrl.setTouched(this.props.name);
}

onBlurHandler(_value) {
this.value = getFieldValue(_value);
this.value = this.getFieldValue(_value);
this.validateEvent('onBlur');
!this.context.FormCtrl.isTouched[this.props.name] && this.context.FormCtrl.setTouched(this.props.name);
}

onFocusHandler(_value) {
this.value = getFieldValue(_value);
this.value = this.getFieldValue(_value);
this.validateEvent('onFocus');
}

onChangeHandler(_value) {
this.value = getFieldValue(_value);
this.value = this.getFieldValue(_value);
this.validateEvent('onChange');
!this.context.FormCtrl.isDirty[this.props.name] && this.context.FormCtrl.setDirty(this.props.name);
}
Expand All @@ -116,6 +114,13 @@ export default class AvBaseInput extends Component {
return {key, value};
}

getFieldValue(event){
if (this.props.type === 'checkbox') {
return event.target.checked;
}
return event && event.target && !isUndefined(event.target.value) ? event.target.value : event;
}

getValidationEvent() {
return (this.props.validationEvent)
? this.props.validationEvent
Expand Down

0 comments on commit 17a2673

Please sign in to comment.