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 1 commit
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
75 changes: 75 additions & 0 deletions src/components/Input/Type/Radio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';

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

class Radio extends PureComponent {
constructor(props) {
super(props);
this.state = { checked: 0 };
this.handleChecked = this.handleChecked.bind(this);
}

handleChecked(i) {
this.setState({ checked: i });
}

static get propTypes() {
return {
children: PropTypes.arrayOf(PropTypes.string).isRequired,
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: this.handleChange,
type: 'text',
};
}

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

return children.map((option, idx) => (
<div key={option} className={className}>
Copy link
Contributor

Choose a reason for hiding this comment

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

having everything set to option kind of limits the use of this. For example, we might want a descriptive label but a value that maps to an int.

An ideal for this would be something like what semantic-ui does (http://react.semantic-ui.com/addons/radio/#types-radio-group). Unfortunately we don't have anything like Form.Field right now though...

<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}
style={style}
type={type}
value={option}
{...otherProps}
/>
{option}
</label>
</div>
));
}
}

export default connectTheme(Radio);