-
Notifications
You must be signed in to change notification settings - Fork 3
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
Repair Radio Checking onChange #15
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React, { PureComponent } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { connectTheme } from '../../../utils'; | ||
|
||
const isLimPrim = function(input) { | ||
return { string: '', number: '' }.hasOwnProperty(typeof input); | ||
}; | ||
|
||
const optTypeCheck = function(option) { | ||
if (Array.isArray(option)) return isLimPrim(option[0]) ? 'array' : false; | ||
else return isLimPrim(option) ? 'lim-prim' : false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a subset of primitives -> number and string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the output polymorphic? |
||
}; | ||
|
||
const radioPropCheck = function(options, idx) { | ||
if (!optTypeCheck(options[idx])) { | ||
throw new Error( | ||
`Invalid type [ ${typeof options[idx]} ] of option | ||
Label in Radio. Validation failed.` | ||
); | ||
} | ||
}; | ||
|
||
class Radio extends PureComponent { | ||
constructor(props) { | ||
super(props); | ||
this.state = { checked: this.props.checked || 0 }; | ||
this.handleChecked = this.handleChecked.bind(this); | ||
} | ||
|
||
handleChecked(i) { | ||
this.setState({ checked: i }); | ||
} | ||
|
||
static get propTypes() { | ||
return { | ||
children: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.string), | ||
PropTypes.arrayOf((children, key) => radioPropCheck(children, key)), | ||
]), | ||
name: PropTypes.string.isRequired, | ||
onChange: PropTypes.func, | ||
placeholder: PropTypes.string, | ||
style: PropTypes.object, | ||
theme: PropTypes.object, | ||
type: PropTypes.string, | ||
value: PropTypes.string, | ||
}; | ||
} | ||
|
||
static get defaultProps() { | ||
return { | ||
onChange: function() {}, | ||
type: 'text', | ||
}; | ||
} | ||
|
||
render() { | ||
const { | ||
children, | ||
className, | ||
name, | ||
onChange, | ||
placeholder, | ||
style, | ||
theme, | ||
type, | ||
value, | ||
...otherProps | ||
} = this.props; | ||
|
||
return children.map((option, idx) => { | ||
const radioType = optTypeCheck(option); | ||
const optLabel = radioType === 'lim-prim' ? option : option[0]; | ||
const optValue = radioType === 'lim-prim' ? option : option[1]; | ||
|
||
return ( | ||
<div key={`${optLabel}-${idx}`} className={className}> | ||
<label className={`form-label ${theme.input.capitalize}`}> | ||
<input | ||
checked={idx === this.state.checked} | ||
className={theme.input[type]} | ||
name={`${name}`} | ||
onChange={e => { | ||
this.handleChecked(idx); | ||
onChange(e); | ||
}} | ||
placeholder={placeholder} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be reading this wrong but wouldn't this mean that all radios will have the same placeholder? Also, do radio inputs have placeholders? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Radio does not have a placeholder to my knowledge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this component is going to be exclusively for Radio then this should be removed imo. |
||
style={style} | ||
type={type} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. following on the previous comment, if this is specifically a Radio input, shouldn't type be hard coded? We should harden on either having flexible, general inputs or having the specific type. My guess is this could allow for someone to do |
||
value={optValue} | ||
{...otherProps} | ||
/> | ||
{optLabel} | ||
</label> | ||
</div> | ||
); | ||
}); | ||
} | ||
} | ||
|
||
export default connectTheme(Radio); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I like this a lot. Also adds a lot more flexibility for more custom input types in the future 👍