-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: CheckboxGroup and CheckboxSingle components
- Loading branch information
Showing
11 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React from 'react'; | ||
import Example from '../components/Example'; | ||
import { CheckboxSingle } from '../../src'; | ||
import { CheckboxGroup } from '../../src'; | ||
|
||
const Konsole = window.console; | ||
|
||
const onChange = function(value, event, name) { | ||
Konsole.log({checkboxGroupExample_CustomOnChange_value: value}); | ||
Konsole.log({checkboxGroupExample_CustomOnChange_event: event}); | ||
Konsole.log({checkboxGroupExample_CustomOnChange_name: name}); | ||
} | ||
|
||
class CheckboxGroupExample extends React.PureComponent { | ||
render() { | ||
return( | ||
<CheckboxGroup name="movies" value={['terminator', 'predator']} onChange={onChange}> | ||
<CheckboxSingle label="The Terminator" value="terminator" /> | ||
<CheckboxSingle label="Predator" value="predator"/> | ||
<CheckboxSingle label="The Sound of Music" value="soundofmusic"/> | ||
</CheckboxGroup> | ||
); | ||
} | ||
} | ||
|
||
const exampleProps = { | ||
componentName: 'CheckboxGroup', | ||
notes: 'Contains individual checkboxes. The state of child checkboxes is held in an array.', | ||
exampleCodeSnippet: ` | ||
<CheckboxGroup name="movies" value={['terminator', 'predator']} onChange={onChange}> | ||
<CheckboxSingle label="The Terminator" value="terminator" /> | ||
<CheckboxSingle label="Predator" value="predator"/> | ||
<CheckboxSingle label="The Sound of Music" value="soundofmusic"/> | ||
</CheckboxGroup> | ||
`, | ||
propTypeSectionArray: [ | ||
{ | ||
label: '', | ||
propTypes: [ | ||
{ | ||
propType: 'name', | ||
type: 'string', | ||
}, | ||
{ | ||
propType: 'value', | ||
type: 'Array of strings', | ||
note: 'The strings must be the values of the group\'s child Checkboxes', | ||
}, | ||
{ | ||
propType: 'children', | ||
type: '<Checkbox /> elements', | ||
}, | ||
{ | ||
propType: 'onChange', | ||
type: 'Function', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
export default () => ( | ||
<Example {...exampleProps}> | ||
<CheckboxGroupExample /> | ||
</Example> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React from 'react'; | ||
import Example from '../components/Example'; | ||
import { CheckboxSingle } from '../../src'; | ||
|
||
const Konsole = window.console; | ||
|
||
const onChange = function(value, event, name) { | ||
Konsole.log({checkboxSingleExample_CustomOnChange_value: value}); | ||
Konsole.log({checkboxSingleExample_CustomOnChange_event: event}); | ||
Konsole.log({checkboxSingleExample_CustomOnChange_name: name}); | ||
} | ||
|
||
class CheckboxSingleExample extends React.PureComponent { | ||
render() { | ||
return <CheckboxSingle | ||
name="Name goes here" | ||
label="Label goes here" | ||
value="Value goes here" | ||
dts="data-test-selector-goes-here" | ||
checked="" | ||
onChangeCallback={onChange} | ||
/>; | ||
} | ||
} | ||
|
||
const exampleProps = { | ||
componentName: 'CheckboxSingle', | ||
notes: '', | ||
exampleCodeSnippet: ` | ||
<CheckboxSingle | ||
name="Name goes here" | ||
label="Label goes here" | ||
value="Value goes here" | ||
disabled="disabled" | ||
dts="data-test-selector-goes-here" | ||
/> | ||
`, | ||
propTypeSectionArray: [ | ||
{ | ||
label: '', | ||
propTypes: [ | ||
{ | ||
propType: 'name', | ||
type: 'string', | ||
}, | ||
{ | ||
propType: 'label', | ||
type: 'string', | ||
}, | ||
{ | ||
propType: 'value', | ||
type: 'string', | ||
}, | ||
{ | ||
propType: 'checked', | ||
type: 'string', | ||
note: 'Can be any string, not necessarily "checked" or "true."' | ||
}, | ||
{ | ||
propType: 'disabled', | ||
type: 'string', | ||
note: 'Can be any string, not necessarily "disabled" or "true."' | ||
}, | ||
{ | ||
propType: 'dts', | ||
type: 'string', | ||
}, | ||
{ | ||
propType: 'onChange', | ||
type: 'function', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
export default () => ( | ||
<Example {...exampleProps}> | ||
<CheckboxSingleExample /> | ||
</Example> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import './styles.scss'; | ||
|
||
const Konsole = window.console; | ||
|
||
class CheckboxGroup extends React.Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
value: this.props.value || [], | ||
}; | ||
this.renderChildren = this.renderChildren.bind(this); | ||
this.onChangeCallback = this.onChangeCallback.bind(this); | ||
} | ||
|
||
onChangeCallback(checkboxParams, event) { | ||
let newValue; | ||
if(checkboxParams.checked){ | ||
newValue = this.state.value.concat([checkboxParams.value]); | ||
} else { | ||
newValue = this.state.value.filter(currentValue => currentValue !== checkboxParams.value); | ||
} | ||
|
||
Konsole.log(newValue); | ||
this.setState({value: newValue}); | ||
|
||
if (this.props.onChange) { | ||
this.props.onChange(newValue, event, this.props.name); | ||
} | ||
} | ||
|
||
renderChildren() { | ||
return React.Children.map(this.props.children, (child) => | ||
React.cloneElement(child, { | ||
name: this.props.name, | ||
checked: (this.state.value.indexOf(child.props.value) >= 0) ? "checked" : "", | ||
onChangeCallback: this.onChangeCallback, | ||
})); | ||
} | ||
|
||
render() { | ||
const classes = `checkbox-group ${this.props.customClasses}`; | ||
return ( | ||
<div className={classes}> | ||
{this.renderChildren()} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
CheckboxGroup.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
value: PropTypes.arrayOf(PropTypes.string), | ||
children: PropTypes.arrayOf(PropTypes.element).isRequired, | ||
customClasses: PropTypes.string, | ||
onChange: PropTypes.func, | ||
}; | ||
|
||
CheckboxGroup.defaultProps = {}; | ||
|
||
export default CheckboxGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import CheckboxGroup from '.'; | ||
|
||
describe('CheckboxGroup', () => { | ||
it('should render with defaults', () => { | ||
const component = shallow(<CheckboxGroup />); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.checkbox-group label { | ||
line-height: 20px; | ||
} | ||
|
||
.checkbox-group input { | ||
margin: 2px 5px 0 0; | ||
line-height: 20px; | ||
display: inline-block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import './styles.scss'; | ||
|
||
class CheckboxSingle extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
checked: props.checked || false, | ||
disabled: props.disabled || false, | ||
name: props.name, | ||
value: props.value, | ||
dts: props.dts || "", | ||
label: props.label || "", | ||
}; | ||
this.onChange = this.onChange.bind(this); | ||
if(this.props.onChangeCallback) { | ||
this.onChangeCallback = this.props.onChangeCallback.bind(this); | ||
} | ||
} | ||
|
||
onChange(event) { | ||
const isChecked = Boolean(event.target.checked); | ||
const isDisabled = Boolean(event.target.disabled); | ||
this.setState({ | ||
checked: isChecked, | ||
disabled: isDisabled, | ||
}); | ||
if(this.onChangeCallback) { | ||
this.onChangeCallback({value: this.state.value, checked: isChecked}, event, this.props.name); | ||
} | ||
} | ||
|
||
render() { | ||
const onChangeHandler = this.onChange; | ||
const booleanAttributes = { disabled: "", checked: "" }; | ||
if(this.state.disabled) { | ||
booleanAttributes.disabled = "disabled"; | ||
} | ||
if(this.state.checked) { | ||
booleanAttributes.checked = "checked"; | ||
} | ||
|
||
return( | ||
<div className="checkbox-single"> | ||
<label> | ||
<input | ||
type="checkbox" | ||
name={this.state.name} | ||
value={this.state.value} | ||
onChange={onChangeHandler} | ||
data-test-selector={this.state.dts} | ||
{...booleanAttributes} | ||
/> | ||
{this.state.label} | ||
</label> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
CheckboxSingle.propTypes = { | ||
name: PropTypes.string, | ||
label: PropTypes.string, | ||
value: PropTypes.string.isRequired, | ||
dts: PropTypes.string, | ||
disabled: PropTypes.string, | ||
checked: PropTypes.string, | ||
onChangeCallback: PropTypes.func, | ||
}; | ||
|
||
export default CheckboxSingle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import CheckboxSingle from '.'; | ||
|
||
describe('CheckboxSingle', () => { | ||
it('should render with defaults', () => { | ||
const component = shallow(<CheckboxSingle />); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.checkbox-single label { | ||
line-height: 20px; | ||
} | ||
|
||
.checkbox-single input { | ||
margin: 2px 5px 0 0; | ||
line-height: 20px; | ||
display: inline-block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters