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

246 lines
5.1 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,
}, 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,
children: PropTypes.node,
extraClass: PropTypes.string,
};
2020-04-22 17:09:40 -07:00
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,
};
2020-04-20 17:33:19 -07:00
export class SimpleForm extends ImmutablePureComponent {
static propTypes = {
children: PropTypes.node,
2020-04-20 18:20:07 -07:00
onSubmit: PropTypes.func,
}
static defaultProps = {
onSubmit: e => e.preventDefault(),
2020-04-20 17:33:19 -07:00
}
render() {
const { children, onSubmit } = this.props;
return (
<form className='simple_form' onSubmit={onSubmit}>
{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 class Checkbox extends ImmutablePureComponent {
static propTypes = {
label: PropTypes.string,
}
render() {
2020-04-22 17:36:27 -07:00
const Input = this.props.label ? LabelInput : 'input';
2020-04-20 17:33:19 -07:00
return (
2020-04-22 17:36:27 -07:00
<InputContainer {...this.props} extraClass='boolean'>
2020-04-22 17:09:40 -07:00
<Input type='checkbox' {...this.props} />
2020-04-22 17:36:27 -07:00
</InputContainer>
2020-04-20 17:33:19 -07:00
);
}
}
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'>
<label className='radio_buttons optional'>{label}</label>
<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() {
const { label, hint, value, checked, onChange } = this.props;
const id = uuidv4();
return (
<li className='radio'>
<label htmlFor={id}>
<input
id={id}
className='radio_buttons optional'
type='radio'
checked={checked}
onChange={onChange}
value={value}
/> {label}
{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,
defaultValue: PropTypes.string,
onChange: PropTypes.func,
}
render() {
const { label, items, defaultValue, onChange } = this.props;
const id = uuidv4();
return (
<div className='input with_label select optional'>
<div className='label_input'>
<label className='select optional' htmlFor={id}>{label}</label>
<div className='label_input__wrapper'>
<select
id={id}
className='select optional'
onChange={onChange}
defaultValue={defaultValue}
>
{Object.keys(items).map(item => (
<option key={item} value={item}>
{items[item]}
</option>
))}
</select>
</div>
</div>
</div>
);
}
}
2020-04-21 16:00:05 -07:00
export class TextInput extends ImmutablePureComponent {
static propTypes = {
label: PropTypes.string,
}
render() {
2020-04-22 17:36:27 -07:00
const Input = this.props.label ? LabelInput : 'input';
2020-04-22 16:11:12 -07:00
return (
2020-04-22 17:36:27 -07:00
<InputContainer {...this.props}>
2020-04-22 17:09:40 -07:00
<Input type='text' {...this.props} />
2020-04-22 17:36:27 -07:00
</InputContainer>
2020-04-22 16:11:12 -07:00
);
2020-04-21 16:00:05 -07:00
}
2020-04-21 17:46:29 -07:00
2020-04-21 16:00:05 -07:00
}
2020-04-22 14:26:44 -07:00
export class FileChooser extends ImmutablePureComponent {
static propTypes = {
label: PropTypes.string,
hint: PropTypes.string,
}
static defaultProps = {
2020-04-22 16:15:50 -07:00
accept: ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
2020-04-22 14:26:44 -07:00
}
render() {
2020-04-22 17:36:27 -07:00
const { hint, ...props } = this.props;
const Input = this.props.label ? LabelInput : 'input';
2020-04-22 14:26:44 -07:00
return (
2020-04-22 17:36:27 -07:00
<InputContainer {...this.props}>
2020-04-22 17:09:40 -07:00
<Input type='file' {...props} />
2020-04-22 17:36:27 -07:00
</InputContainer>
2020-04-22 14:26:44 -07:00
);
}
}