sforkowany z mirror/soapbox
Refactor LabelInput
rodzic
4a675be598
commit
c367aef6b4
|
@ -4,6 +4,22 @@ import PropTypes from 'prop-types';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
export const LabelInput = ({ label, ...props }) => {
|
||||||
|
const id = uuidv4();
|
||||||
|
return (
|
||||||
|
<div className='label_input'>
|
||||||
|
<label htmlFor={id}>{label}</label>
|
||||||
|
<div className='label_input__wrapper'>
|
||||||
|
<input id={id} {...props} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
LabelInput.propTypes = {
|
||||||
|
label: PropTypes.string.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
export class SimpleForm extends ImmutablePureComponent {
|
export class SimpleForm extends ImmutablePureComponent {
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
|
@ -49,38 +65,21 @@ export class Checkbox extends ImmutablePureComponent {
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
label: PropTypes.string,
|
label: PropTypes.string,
|
||||||
name: PropTypes.string,
|
|
||||||
checked: PropTypes.bool,
|
|
||||||
onChange: PropTypes.func,
|
|
||||||
}
|
|
||||||
|
|
||||||
static defaultProps = {
|
|
||||||
checked: false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { label, name, checked, onChange } = this.props;
|
const { label, required } = this.props;
|
||||||
const id = uuidv4();
|
|
||||||
|
const containerClassNames = classNames('input', 'boolean', {
|
||||||
|
'with_label': label,
|
||||||
|
'required': required,
|
||||||
|
});
|
||||||
|
|
||||||
|
const Input = label ? LabelInput : 'input';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='input with_label boolean optional'>
|
<div className={containerClassNames}>
|
||||||
<div className='label_input'>
|
<Input type='checkbox' {...this.props} />
|
||||||
<label className='boolean optional' htmlFor={id}>
|
|
||||||
{label}
|
|
||||||
</label>
|
|
||||||
<div className='label_input__wrapper'>
|
|
||||||
<label className='checkbox'>
|
|
||||||
<input
|
|
||||||
id={id}
|
|
||||||
name={name}
|
|
||||||
className='boolean optional'
|
|
||||||
type='checkbox'
|
|
||||||
checked={checked}
|
|
||||||
onChange={onChange}
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -102,7 +101,7 @@ export class RadioGroup extends ImmutablePureComponent {
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='input with_floating_label radio_buttons optional user_setting_default_privacy'>
|
<div className='input with_floating_label radio_buttons'>
|
||||||
<div className='label_input'>
|
<div className='label_input'>
|
||||||
<label className='radio_buttons optional'>{label}</label>
|
<label className='radio_buttons optional'>{label}</label>
|
||||||
<ul>{childrenWithProps}</ul>
|
<ul>{childrenWithProps}</ul>
|
||||||
|
@ -195,30 +194,18 @@ export class TextInput extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { label, ...props } = this.props;
|
const { label, required } = this.props;
|
||||||
const id = uuidv4();
|
|
||||||
|
|
||||||
const containerClassNames = classNames('input', {
|
const containerClassNames = classNames('input', {
|
||||||
'with_label': label,
|
'with_label': label,
|
||||||
'required': this.props.required,
|
'required': required,
|
||||||
});
|
});
|
||||||
|
|
||||||
const InputWithLabel = () => (
|
const Input = label ? LabelInput : 'input';
|
||||||
<div className='label_input'>
|
|
||||||
<label htmlFor={id}>{label}</label>
|
|
||||||
<div className='label_input__wrapper'>
|
|
||||||
<Input />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
|
|
||||||
const Input = () => (
|
|
||||||
<input id={id} type='text' {...props} />
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={containerClassNames}>
|
<div className={containerClassNames}>
|
||||||
{label ? <InputWithLabel /> : <Input />}
|
<Input type='text' {...this.props} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -238,22 +225,18 @@ export class FileChooser extends ImmutablePureComponent {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { label, hint, ...props } = this.props;
|
const { label, hint, ...props } = this.props;
|
||||||
const id = uuidv4();
|
|
||||||
|
const containerClassNames = classNames('input', {
|
||||||
|
'with_label': label,
|
||||||
|
'required': this.props.required,
|
||||||
|
});
|
||||||
|
|
||||||
|
const Input = label ? LabelInput : 'input';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='input with_label file optional field_with_hint'>
|
<div className={containerClassNames}>
|
||||||
<div className='label_input'>
|
<Input type='file' {...props} />
|
||||||
<label className='file optional' htmlFor={id}>{label}</label>
|
{hint && <span className='hint'>{hint}</span>}
|
||||||
<div className='label_input__wrapper'>
|
|
||||||
<input
|
|
||||||
id={id}
|
|
||||||
className='file optional'
|
|
||||||
type='file'
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<span className='hint'>{hint}</span>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,9 +63,9 @@ code {
|
||||||
position: static;
|
position: static;
|
||||||
}
|
}
|
||||||
|
|
||||||
label.checkbox {
|
input[type="checkbox"] {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 2px;
|
top: 3px;
|
||||||
left: 0;
|
left: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue