bigbuffet-rw/app/gabsocial/features/forms/index.js

197 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-04-20 17:33:19 -07:00
import React from 'react';
import ImmutablePureComponent from 'react-immutable-pure-component';
import PropTypes from 'prop-types';
2020-04-22 16:11:12 -07:00
import classNames from 'classnames';
2020-04-20 17:33:19 -07:00
import { v4 as uuidv4 } from 'uuid';
2020-04-22 17:36:27 -07:00
export const InputContainer = (props) => {
const containerClass = classNames('input', {
'with_label': props.label,
'required': props.required,
2020-04-22 20:02:56 -07:00
'boolean': props.type === 'checkbox',
2020-04-22 17:36:27 -07:00
}, props.extraClass);
return (
<div className={containerClass}>
{props.children}
{props.hint && <span className='hint'>{props.hint}</span>}
</div>
);
};
InputContainer.propTypes = {
label: PropTypes.string,
hint: PropTypes.string,
required: PropTypes.bool,
2020-04-22 20:02:56 -07:00
type: PropTypes.string,
2020-04-22 17:36:27 -07:00
children: PropTypes.node,
extraClass: PropTypes.string,
};
2020-04-22 21:01:36 -07:00
export const LabelInputContainer = ({ label, children, ...props }) => {
2020-04-22 17:09:40 -07:00
const id = uuidv4();
2020-04-22 21:01:36 -07:00
const childrenWithProps = React.Children.map(children, child => (
React.cloneElement(child, { id: id })
));
2020-04-22 17:09:40 -07:00
return (
<div className='label_input'>
<label htmlFor={id}>{label}</label>
<div className='label_input__wrapper'>
2020-04-22 21:01:36 -07:00
{childrenWithProps}
2020-04-22 17:09:40 -07:00
</div>
</div>
);
};
2020-04-22 21:01:36 -07:00
LabelInputContainer.propTypes = {
label: PropTypes.string.isRequired,
children: PropTypes.node,
};
export const LabelInput = ({ label, ...props }) => (
<LabelInputContainer label={label}>
<input {...props} />
</LabelInputContainer>
);
2020-04-22 17:09:40 -07:00
LabelInput.propTypes = {
label: PropTypes.string.isRequired,
};
2020-04-22 20:02:56 -07:00
export class SimpleInput extends ImmutablePureComponent {
static propTypes = {
label: PropTypes.string,
hint: PropTypes.string,
}
render() {
const { hint, ...props } = this.props;
const Input = this.props.label ? LabelInput : 'input';
return (
<InputContainer {...this.props}>
<Input {...props} />
</InputContainer>
);
}
}
export const SimpleForm = ({ children, ...props }) => (
<form className='simple_form' {...props}>{children}</form>
2020-04-22 21:01:36 -07:00
);
2020-04-20 17:33:19 -07:00
2020-04-22 21:01:36 -07:00
SimpleForm.propTypes = {
children: PropTypes.node,
};
2020-04-20 17:33:19 -07:00
2020-04-22 21:01:36 -07:00
SimpleForm.defaultProps = {
acceptCharset: 'UTF-8',
2020-04-22 21:01:36 -07:00
onSubmit: e => e.preventDefault(),
};
2020-04-20 17:33:19 -07:00
2020-04-22 21:01:36 -07:00
export const FieldsGroup = ({ children }) => (
<div className='fields-group'>{children}</div>
);
2020-04-20 17:33:19 -07:00
2020-04-22 21:01:36 -07:00
FieldsGroup.propTypes = {
children: PropTypes.node,
};
2020-04-20 17:33:19 -07:00
2020-04-22 20:02:56 -07:00
export const Checkbox = props => (
<SimpleInput type='checkbox' {...props} />
);
2020-04-20 18:20:07 -07:00
export class RadioGroup extends ImmutablePureComponent {
static propTypes = {
label: PropTypes.string,
children: PropTypes.node,
}
render() {
const { label, children, onChange } = this.props;
const childrenWithProps = React.Children.map(children, child =>
React.cloneElement(child, { onChange })
);
return (
2020-04-22 17:09:40 -07:00
<div className='input with_floating_label radio_buttons'>
2020-04-20 18:20:07 -07:00
<div className='label_input'>
2020-04-22 21:01:36 -07:00
<label>{label}</label>
2020-04-20 18:20:07 -07:00
<ul>{childrenWithProps}</ul>
</div>
</div>
);
}
}
export class RadioItem extends ImmutablePureComponent {
static propTypes = {
label: PropTypes.string,
hint: PropTypes.string,
value: PropTypes.string.isRequired,
checked: PropTypes.bool.isRequired,
onChange: PropTypes.func,
}
static defaultProps = {
checked: false,
}
render() {
2020-04-22 21:01:36 -07:00
const { label, hint, ...props } = this.props;
2020-04-20 18:20:07 -07:00
const id = uuidv4();
return (
<li className='radio'>
<label htmlFor={id}>
2020-04-22 21:01:36 -07:00
<input id={id} type='radio' {...props} /> {label}
2020-04-20 18:20:07 -07:00
{hint && <span className='hint'>{hint}</span>}
</label>
</li>
);
}
}
2020-04-20 18:33:27 -07:00
export class SelectDropdown extends ImmutablePureComponent {
static propTypes = {
label: PropTypes.string,
items: PropTypes.object.isRequired,
}
render() {
2020-04-22 21:01:36 -07:00
const { label, items, ...props } = this.props;
2020-04-20 18:33:27 -07:00
2020-04-22 21:01:36 -07:00
const optionElems = Object.keys(items).map(item => (
<option key={item} value={item}>{items[item]}</option>
));
const selectElem = <select {...props}>{optionElems}</select>;
return label ? (
<LabelInputContainer label={label}>{selectElem}</LabelInputContainer>
) : selectElem;
2020-04-20 18:33:27 -07:00
}
}
2020-04-21 16:00:05 -07:00
2020-04-22 20:02:56 -07:00
export const TextInput = props => (
<SimpleInput type='text' {...props} />
);
2020-04-21 16:00:05 -07:00
2020-04-22 20:02:56 -07:00
export const FileChooser = props => (
<SimpleInput type='file' {...props} />
);
2020-04-21 16:00:05 -07:00
2020-04-22 20:02:56 -07:00
FileChooser.defaultProps = {
accept: ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
};