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

Repair Radio Checking onChange #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
39 changes: 23 additions & 16 deletions src/components/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';

import { connectTheme } from '../../utils';
import Radio from './Type/Radio';

class Input extends PureComponent {
static get propTypes() {
Expand All @@ -14,42 +15,48 @@ class Input extends PureComponent {
style: PropTypes.object,
theme: PropTypes.object,
type: PropTypes.string,
value: PropTypes.string
value: PropTypes.string,
};
}

static get defaultProps() {
return {
onChange: () => {},
type: 'text'
type: 'text',
};
}

render() {
const {
type,
children,
name,
onChange,
placeholder,
theme,
style,
theme,
type,
value,
...otherProps
} = this.props;

return (
<input
className={theme.input[type]}
name={name}
onChange={onChange}
placeholder={placeholder}
style={style}
type={type}
value={value}
{...otherProps}
/>
);
switch (type) {
case 'radio':
return <Radio {...this.props}>{children}</Radio>;
Copy link
Contributor

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 👍


default:
return (
<input
className={theme.input[type]}
name={name}
onChange={onChange}
placeholder={placeholder}
style={style}
type={type}
value={value}
{...otherProps}
/>
);
}
}
}

Expand Down
102 changes: 102 additions & 0 deletions src/components/Input/Type/Radio.js
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is lim-prim?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a subset of primitives -> number and string

Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Radio does not have a placeholder to my knowledge.

Copy link
Contributor

Choose a reason for hiding this comment

The 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}
Copy link
Contributor

Choose a reason for hiding this comment

The 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 <Radio type="number" {...otherProps} /> and use a radio component to render a non radio input.

value={optValue}
{...otherProps}
/>
{optLabel}
</label>
</div>
);
});
}
}

export default connectTheme(Radio);