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 slider component! #5280

Merged
merged 4 commits into from
Oct 25, 2019
Merged
Changes from all commits
Commits
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
133 changes: 133 additions & 0 deletions jsx/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,137 @@ class LorisElement extends Component {
}
}

/**
* Slider Component
* React wrapper for a <input type='range'> element.
*/
class SliderElement extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}

handleChange(e) {
// Handles empty, min & max cases.
const inputValue = e.target.value
? parseFloat(e.target.value)
: this.props.minValue;
let value = inputValue > this.props.maxValue
? this.props.maxValue
: inputValue;
value = value < this.props.minValue
? this.props.minValue
: value;
this.props.onUserInput(this.props.name, value);
}

render() {
let errorMessage = null;
let requiredHTML = null;
let elementClass = this.props.elementClass;
let disabled = this.props.disabled ? 'disabled' : null;
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';
}

return (
<div className={elementClass}>
<label className={'col-sm-3 control-label'}
htmlFor={this.props.id}>
{this.props.label}
{errorMessage}
{requiredHTML}
</label>
<div className={'col-sm-9'}>
<div style={{
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
width: '100%',
}}>
<div style={{
flexGrow: 1000,
display: 'flex',
flexDirection: 'column',
flexBasis: '100%',
maxWidth: this.props.maxWidth,
marginRight: '5px',
flex: 2,
}}>
<input
maltheism marked this conversation as resolved.
Show resolved Hide resolved
type='range'
name={this.props.name}
id={this.props.id}
value={this.props.value}
min={this.props.minValue}
max={this.props.maxValue}
required={required}
disabled={disabled}
onChange={this.handleChange}
style={{width: '100%'}}
/>
</div>
<div style={{
display: 'flex',
flexDirection: 'column',
flexBasis: '100%',
maxWidth: '50px',
flex: 1,
}}>
<input
type='number'
name={'input_' + this.props.name}
value={this.props.value}
min={this.props.minValue}
max={this.props.maxValue}
required={required}
disabled={disabled}
onChange={this.handleChange}
style={{
width: '50px',
textAlign: 'center',
}}
/>
</div>
</div>
</div>
</div>
);
}
}
SliderElement.propTypes = {
id: PropTypes.string,
name: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
value: PropTypes.number.isRequired,
minValue: PropTypes.number.isRequired,
maxValue: PropTypes.number.isRequired,
maxWidth: PropTypes.string,
disabled: PropTypes.bool,
required: PropTypes.bool,
errorMessage: PropTypes.string,
elementClass: PropTypes.string,
onUserInput: PropTypes.func,
};
SliderElement.defaultProps = {
id: null,
maxWidth: 'auto',
disabled: false,
required: false,
errorMessage: '',
elementClass: 'row form-group',
onUserInput: function() {
console.warn('onUserInput() callback is not set');
},
};

window.FormElement = FormElement;
window.FieldsetElement = FieldsetElement;
window.SelectElement = SelectElement;
Expand All @@ -1750,6 +1881,7 @@ window.FileElement = FileElement;
window.StaticElement = StaticElement;
window.HeaderElement = HeaderElement;
window.LinkElement = LinkElement;
window.SliderElement = SliderElement;
window.CheckboxElement = CheckboxElement;
window.ButtonElement = ButtonElement;
window.CTA = CTA;
Expand All @@ -1771,6 +1903,7 @@ export default {
HeaderElement,
LinkElement,
CheckboxElement,
SliderElement,
ButtonElement,
CTA,
LorisElement,
Expand Down