Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
fix(AvCheckbox): get required attribute from group
Browse files Browse the repository at this point in the history
  • Loading branch information
Reese Schultz committed Dec 18, 2018
1 parent f9b1870 commit 908de00
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion __test__/AvCheckboxGroup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('AvCheckboxGroup', () => {
it('should give default fallback when no one set up their stuff', () => {
const wrapper = shallow(<AvCheckboxGroup name="yo" />, options);
const component = wrapper.instance();
expect(component.getDefaultValue()).to.eql({key: 'defaultValue', value: ''});
expect(component.getDefaultValue()).to.eql({key: 'defaultValue', value: []});
});

it('should reset properly', () => {
Expand Down
30 changes: 17 additions & 13 deletions src/AvCheckbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ export default class AvCheckbox extends Component {
customInput,
...attributes} = this.props;

const listProps = this.context.Group.getProps();
const groupProps = this.context.Group.getProps();

const touched = this.context.FormCtrl.isTouched(listProps.name);
const hasError = this.context.FormCtrl.hasError(listProps.name);
const touched = this.context.FormCtrl.isTouched(groupProps.name);
const hasError = this.context.FormCtrl.hasError(groupProps.name);

const classes = classNames(
className,
touched ? 'is-touched' : 'is-untouched',
this.context.FormCtrl.isDirty(listProps.name) ? 'is-dirty' : 'is-pristine',
this.context.FormCtrl.isBad(listProps.name) ? 'is-bad-input' : null,
this.context.FormCtrl.isDirty(groupProps.name) ? 'is-dirty' : 'is-pristine',
this.context.FormCtrl.isBad(groupProps.name) ? 'is-bad-input' : null,
hasError ? 'av-invalid' : 'av-valid',
touched && hasError && 'is-invalid'
touched && hasError && 'is-invalid',
);

if (this.props.disabled === undefined && this.context.FormCtrl.isDisabled() !== undefined) {
Expand All @@ -66,31 +66,35 @@ export default class AvCheckbox extends Component {
if (customInput) {
return (
<CustomInput
name={groupProps.name}
type='checkbox'
{...attributes}
inline={listProps.inline}
id={id || `checkbox-${listProps.name}-${this.props.value}`}
inline={groupProps.inline}
id={id || `checkbox-${groupProps.name}-${this.props.value}`}
className={classes}
onChange={this.onChangeHandler}
value={this.props.value && this.props.value.toString()}
defaultChecked={this.isDefaultChecked(listProps.value)}
defaultChecked={this.isDefaultChecked(groupProps.value)}
required={groupProps.required}
label={this.props.label}
/>
);
}

return (
<FormGroup check inline={listProps.inline} disabled={attributes.disabled || attributes.readOnly}>
<FormGroup check inline={groupProps.inline} disabled={attributes.disabled || attributes.readOnly}>
<Input
name={groupProps.name}
type='checkbox'
{...attributes}
id={id || `checkbox-${listProps.name}-${this.props.value}`}
id={id || `checkbox-${groupProps.name}-${this.props.value}`}
className={classes}
onChange={this.onChangeHandler}
value={this.props.value && this.props.value.toString()}
defaultChecked={this.isDefaultChecked(listProps.value)}
defaultChecked={this.isDefaultChecked(groupProps.value)}
required={groupProps.required}
/>
<Label check for={id || `checkbox-${listProps.name}-${this.props.value}`}>{this.props.label}</Label>
<Label check for={id || `checkbox-${groupProps.name}-${this.props.value}`}>{this.props.label}</Label>
</FormGroup>
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/AvCheckboxGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class AvCheckboxGroup extends Component {
}

componentWillMount() {
this.value = this.props.value || this.getDefaultValue().value || [];
this.value = this.props.value || this.getDefaultValue().value;
this.setState({ value: this.value });
this.updateValidations();
}
Expand Down Expand Up @@ -110,7 +110,7 @@ export default class AvCheckboxGroup extends Component {
getDefaultValue() {
const key = 'defaultValue';

let value = '';
let value = [];
if (!isUndefined(this.props[key])) {
value = this.props[key];
} else if (!isUndefined(this.context.FormCtrl.getDefaultValue(this.props.name))) {
Expand All @@ -126,7 +126,7 @@ export default class AvCheckboxGroup extends Component {
}

update() {
this.setState([]);
this.setState({});
this.updateInputs();
}

Expand Down Expand Up @@ -160,10 +160,10 @@ export default class AvCheckboxGroup extends Component {

reset() {
this.value = this.getDefaultValue().value;
this.setState({ value: this.value });
this.context.FormCtrl.setDirty(this.props.name, false);
this.context.FormCtrl.setTouched(this.props.name, false);
this.context.FormCtrl.setBad(this.props.name, false);
this.setState({ value: this.value });
this.validate();
this.props.onReset && this.props.onReset(this.value);
}
Expand Down

0 comments on commit 908de00

Please sign in to comment.