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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jsx/Form.js] new radio component! #5846

Merged
merged 26 commits into from
Jan 6, 2020
Merged
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4d9f450
update
Misotheism May 30, 2019
ee7e49a
Merge branch 'master' of https://github.com/maltheism/Loris
Misotheism Oct 30, 2019
8e89775
Merge remote-tracking branch 'aces/master'
Misotheism Oct 30, 2019
d2c05a1
fix
Misotheism Oct 30, 2019
f1a5dab
Merge remote-tracking branch 'aces/master'
Misotheism Dec 2, 2019
3552366
Merge remote-tracking branch 'aces/master'
Misotheism Dec 4, 2019
1f0a3df
Merge remote-tracking branch 'aces/master'
Misotheism Dec 4, 2019
024f328
Merge remote-tracking branch 'aces/master'
Misotheism Dec 4, 2019
0843c50
Merge remote-tracking branch 'aces/master'
Misotheism Dec 6, 2019
4b21090
Merge remote-tracking branch 'aces/master'
Misotheism Dec 9, 2019
c87b432
master new radio component
Misotheism Dec 9, 2019
f9dbcb4
Update jsx/Form.js
maltheism Dec 9, 2019
9d72211
Update jsx/Form.js
maltheism Dec 9, 2019
42f7c28
update from zaliqa suggestions
Misotheism Dec 9, 2019
5a51907
thanks zaliqa
Misotheism Dec 9, 2019
782538a
Update jsx/Form.js
maltheism Dec 9, 2019
c4c04c1
Update jsx/Form.js
maltheism Dec 9, 2019
7c6b5c5
Update jsx/Form.js
maltheism Dec 9, 2019
1bca532
add checked functionality
Misotheism Dec 10, 2019
4c3b13f
update
Misotheism Dec 10, 2019
dacb364
update raidobutton to work
Misotheism Dec 10, 2019
7080acd
update
Misotheism Dec 13, 2019
375f273
Update jsx/Form.js
maltheism Dec 16, 2019
a148852
Update jsx/Form.js
maltheism Dec 16, 2019
8f5b7f3
Update jsx/Form.js
maltheism Dec 16, 2019
74e8e9d
Update jsx/Form.js
maltheism Dec 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions jsx/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,150 @@ class LorisElement extends Component {
}
}

/**
* Radio Component
* React wrapper for a <input type='radio'> element.
*
* Example `options` prop:
* {
* female: 'Female',
* male: 'Male',
* }
*/
maltheism marked this conversation as resolved.
Show resolved Hide resolved
class RadioElement extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.generateLayout = this.generateLayout.bind(this);
}

handleChange(e) {
this.props.onUserInput(this.props.name, e.target.value);
}

generateLayout() {
let layout = [];
let disabled = this.props.disabled ? 'disabled' : null;
let required = this.props.required ? 'required' : null;

const styleRow = {
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
width: '100%',
};
const styleColumn = {
display: 'flex',
flexDirection: 'column',
alignSelf: 'flex-start',
marginRight: '10px',
};
const styleContainer = {
paddingTop: '7px',
cursor: 'pointer',
};
const styleLabel = {
margin: 0,
color: '#064785',
cursor: 'pointer',
};
const styleInput = {
display: 'inline-block',
margin: '0 5px 0 5px',
cursor: 'pointer',
};

let content = [];
for (const key in this.props.items) {
maltheism marked this conversation as resolved.
Show resolved Hide resolved
if (this.props.items.hasOwnProperty(key)) {
maltheism marked this conversation as resolved.
Show resolved Hide resolved
content.push(
<div key={key}
style={styleColumn}>
<div style={styleContainer}>
<input
type='radio'
name={this.props.name}
value={this.props.items[key]}
maltheism marked this conversation as resolved.
Show resolved Hide resolved
id={key}
required={required}
disabled={disabled}
onChange={this.handleChange}
style={styleInput}
/>
maltheism marked this conversation as resolved.
Show resolved Hide resolved
<label htmlFor={key}
style={styleLabel}
>
{key}
maltheism marked this conversation as resolved.
Show resolved Hide resolved
</label>
</div>
</div>
);
}
}

layout.push(
<div key={this.props.name + '_key'}
style={styleRow}>
{content}
</div>
);

return layout;
}

render() {
let errorMessage = null;
let requiredHTML = null;
let elementClass = this.props.elementClass;
let required = this.props.required ? 'required' : null;

// Add required asterix
if (required) {
requiredHTML = <span className='text-danger'>*</span>;
}
// Add error message
if (this.props.errorMessage) {
errorMessage = <span>{this.props.errorMessage}</span>;
elementClass = this.props.elementClass + ' has-error';
}
// Generate layout
const layout = this.generateLayout();

return (
<div className={elementClass}>
<label className={'col-sm-3 control-label'}>
{this.props.label}
{errorMessage}
{requiredHTML}
</label>
<div className={'col-sm-9'}>
{layout}
</div>
</div>
);
}
}
RadioElement.propTypes = {
name: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
options: PropTypes.object.isRequired,
disabled: PropTypes.bool,
required: PropTypes.bool,
errorMessage: PropTypes.string,
elementClass: PropTypes.string,
onUserInput: PropTypes.func,
};
RadioElement.defaultProps = {
disabled: false,
required: false,
errorMessage: '',
elementClass: 'row form-group',
onUserInput: function() {
console.warn('onUserInput() callback is not set');
},
};

/**
* Slider Component
* React wrapper for a <input type='range'> element.
Expand Down Expand Up @@ -1886,6 +2030,7 @@ window.HeaderElement = HeaderElement;
window.LinkElement = LinkElement;
window.SliderElement = SliderElement;
window.CheckboxElement = CheckboxElement;
window.RadioElement = RadioElement;
window.ButtonElement = ButtonElement;
window.CTA = CTA;
window.LorisElement = LorisElement;
Expand All @@ -1906,6 +2051,7 @@ export default {
HeaderElement,
LinkElement,
CheckboxElement,
RadioElement,
SliderElement,
ButtonElement,
CTA,
Expand Down