fleet/frontend/components/forms/fields/InputField/InputField.jsx
Mike Stone f099b2ae22 Create packs (#516)
* Creates new PackComposerPage at /packs/new

* Creates PackForm component

* Adds PackForm to PackComposerPage

* Creates QueriesListItem

* Creates QueriesList

* Creates QueriesListWrapper

* Get all queries when the Packs Composer Page loads

* Form HOC handles updates to formData prop

* Creates form to configure scheduled queries

* QueriesListWrapper renders ConfigurePackQueryForm

* search queries input filters queries list

* Empty state text

* create pack when user submits the new pack form

* Adds Edit pack page to /packs/:pack_id/edit

* API client - get scheduled queries for a pack

* API client - create scheduled query

* Redux config for scheduled queries

* Remove scheduled queries from packs

* Add labels to pack on create

* Add disabled state to the select targets dropdown

* Adds edit route and pushes to new route on edit click

* Adds cancel button to edit pack form

* Adds Checkbox that selects all scheduled queries in table
2016-12-21 12:25:54 -05:00

113 lines
2.8 KiB
JavaScript

import React, { Component, PropTypes } from 'react';
import classnames from 'classnames';
import { noop, pick } from 'lodash';
import FormField from 'components/forms/FormField';
const baseClass = 'input-field';
class InputField extends Component {
static propTypes = {
autofocus: PropTypes.bool,
error: PropTypes.string,
inputClassName: PropTypes.string, // eslint-disable-line react/forbid-prop-types
inputWrapperClass: PropTypes.string,
inputOptions: PropTypes.object, // eslint-disable-line react/forbid-prop-types
name: PropTypes.string,
onChange: PropTypes.func,
onFocus: PropTypes.func,
placeholder: PropTypes.string,
type: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
};
static defaultProps = {
autofocus: false,
inputWrapperClass: '',
inputOptions: {},
label: null,
labelClassName: '',
onFocus: noop,
type: 'text',
value: '',
};
componentDidMount () {
const { autofocus } = this.props;
const { input } = this;
if (autofocus) {
input.focus();
}
return false;
}
onInputChange = (evt) => {
evt.preventDefault();
const { value } = evt.target;
const { onChange } = this.props;
return onChange(value);
}
render () {
const {
error,
inputClassName,
inputOptions,
inputWrapperClass,
name,
onFocus,
placeholder,
type,
value,
} = this.props;
const { onInputChange } = this;
const shouldShowPasswordClass = type === 'password';
const inputClasses = classnames(baseClass, inputClassName, {
[`${baseClass}--password`]: shouldShowPasswordClass,
[`${baseClass}--error`]: error,
[`${baseClass}__textarea`]: type === 'textarea',
});
const formFieldProps = pick(this.props, ['hint', 'label', 'error', 'name']);
if (type === 'textarea') {
return (
<FormField {...formFieldProps} type="textarea" className={inputWrapperClass}>
<textarea
name={name}
onChange={onInputChange}
className={inputClasses}
placeholder={placeholder}
ref={(r) => { this.input = r; }}
type={type}
{...inputOptions}
value={value}
/>
</FormField>
);
}
return (
<FormField {...formFieldProps} type="input" className={inputWrapperClass}>
<input
name={name}
onChange={onInputChange}
onFocus={onFocus}
className={inputClasses}
placeholder={placeholder}
ref={(r) => { this.input = r; }}
type={type}
{...inputOptions}
value={value}
/>
</FormField>
);
}
}
export default InputField;