Refactor LabelInputContainer

stable/1.0.x
Alex Gleason 2020-04-22 23:01:36 -05:00
rodzic 924486984e
commit 252b3fbf51
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
1 zmienionych plików z 46 dodań i 73 usunięć

Wyświetl plik

@ -28,18 +28,33 @@ InputContainer.propTypes = {
extraClass: PropTypes.string, extraClass: PropTypes.string,
}; };
export const LabelInput = ({ label, ...props }) => { export const LabelInputContainer = ({ label, children, ...props }) => {
const id = uuidv4(); const id = uuidv4();
const childrenWithProps = React.Children.map(children, child => (
React.cloneElement(child, { id: id })
));
return ( return (
<div className='label_input'> <div className='label_input'>
<label htmlFor={id}>{label}</label> <label htmlFor={id}>{label}</label>
<div className='label_input__wrapper'> <div className='label_input__wrapper'>
<input id={id} {...props} /> {childrenWithProps}
</div> </div>
</div> </div>
); );
}; };
LabelInputContainer.propTypes = {
label: PropTypes.string.isRequired,
children: PropTypes.node,
};
export const LabelInput = ({ label, ...props }) => (
<LabelInputContainer label={label}>
<input {...props} />
</LabelInputContainer>
);
LabelInput.propTypes = { LabelInput.propTypes = {
label: PropTypes.string.isRequired, label: PropTypes.string.isRequired,
}; };
@ -64,46 +79,26 @@ export class SimpleInput extends ImmutablePureComponent {
} }
export class SimpleForm extends ImmutablePureComponent { export const SimpleForm = ({ children, onSubmit }) => (
<form className='simple_form' onSubmit={onSubmit}>{children}</form>
);
static propTypes = { SimpleForm.propTypes = {
children: PropTypes.node, children: PropTypes.node,
onSubmit: PropTypes.func, onSubmit: PropTypes.func,
} };
static defaultProps = { SimpleForm.defaultProps = {
onSubmit: e => e.preventDefault(), onSubmit: e => e.preventDefault(),
} };
render() { export const FieldsGroup = ({ children }) => (
const { children, onSubmit } = this.props; <div className='fields-group'>{children}</div>
);
return ( FieldsGroup.propTypes = {
<form className='simple_form' onSubmit={onSubmit}> children: PropTypes.node,
{children} };
</form>
);
}
}
export class FieldsGroup extends ImmutablePureComponent {
static propTypes = {
children: PropTypes.node,
}
render() {
const { children } = this.props;
return (
<div className='fields-group'>
{children}
</div>
);
}
}
export const Checkbox = props => ( export const Checkbox = props => (
<SimpleInput type='checkbox' {...props} /> <SimpleInput type='checkbox' {...props} />
@ -126,7 +121,7 @@ export class RadioGroup extends ImmutablePureComponent {
return ( return (
<div className='input with_floating_label radio_buttons'> <div className='input with_floating_label radio_buttons'>
<div className='label_input'> <div className='label_input'>
<label className='radio_buttons optional'>{label}</label> <label>{label}</label>
<ul>{childrenWithProps}</ul> <ul>{childrenWithProps}</ul>
</div> </div>
</div> </div>
@ -150,20 +145,13 @@ export class RadioItem extends ImmutablePureComponent {
} }
render() { render() {
const { label, hint, value, checked, onChange } = this.props; const { label, hint, ...props } = this.props;
const id = uuidv4(); const id = uuidv4();
return ( return (
<li className='radio'> <li className='radio'>
<label htmlFor={id}> <label htmlFor={id}>
<input <input id={id} type='radio' {...props} /> {label}
id={id}
className='radio_buttons optional'
type='radio'
checked={checked}
onChange={onChange}
value={value}
/> {label}
{hint && <span className='hint'>{hint}</span>} {hint && <span className='hint'>{hint}</span>}
</label> </label>
</li> </li>
@ -177,35 +165,20 @@ export class SelectDropdown extends ImmutablePureComponent {
static propTypes = { static propTypes = {
label: PropTypes.string, label: PropTypes.string,
items: PropTypes.object.isRequired, items: PropTypes.object.isRequired,
defaultValue: PropTypes.string,
onChange: PropTypes.func,
} }
render() { render() {
const { label, items, defaultValue, onChange } = this.props; const { label, items, ...props } = this.props;
const id = uuidv4();
return ( const optionElems = Object.keys(items).map(item => (
<div className='input with_label select optional'> <option key={item} value={item}>{items[item]}</option>
<div className='label_input'> ));
<label className='select optional' htmlFor={id}>{label}</label>
<div className='label_input__wrapper'> const selectElem = <select {...props}>{optionElems}</select>;
<select
id={id} return label ? (
className='select optional' <LabelInputContainer label={label}>{selectElem}</LabelInputContainer>
onChange={onChange} ) : selectElem;
defaultValue={defaultValue}
>
{Object.keys(items).map(item => (
<option key={item} value={item}>
{items[item]}
</option>
))}
</select>
</div>
</div>
</div>
);
} }
} }