fleet/frontend/components/forms/fields/InputField/InputField.jsx
Gabriel Hernandez 6555d8def4
Feat UI windows automatic enrollment (#12988)
relates to #12606

Implementation of the Windows automatic enrollment Fleet UI pages. This
includes implementation of card for windows automatic enrollment, the
setup page for windows automatic enrollment, and terms and conditions
page for windows (This is currently still being worked on as our current
solution is not working).

**windows mdm auto enrollment card**


![image](https://github.com/fleetdm/fleet/assets/1153709/d4dc1813-dc28-4a63-bacd-cb7e43e18170)

**windows auto enrollment setup page**


![image](https://github.com/fleetdm/fleet/assets/1153709/92da4b05-0d5d-4404-867f-6d315957bdc3)

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Marcos Oviedo <marcos@fleetdm.com>
2023-08-08 15:57:55 +01:00

191 lines
4.6 KiB
JavaScript

import React, { Component } from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import { noop, pick } from "lodash";
import { stringToClipboard } from "utilities/copy_text";
import FormField from "components/forms/FormField";
import Button from "components/buttons/Button";
import Icon from "components/Icon";
const baseClass = "input-field";
class InputField extends Component {
static propTypes = {
autofocus: PropTypes.bool,
disabled: 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,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
placeholder: PropTypes.string,
type: PropTypes.string,
blockAutoComplete: PropTypes.bool,
value: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
PropTypes.number,
]).isRequired,
parseTarget: PropTypes.bool,
tooltip: PropTypes.string,
hint: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
PropTypes.object,
]),
enableCopy: PropTypes.bool,
};
static defaultProps = {
autofocus: false,
inputWrapperClass: "",
inputOptions: {},
label: null,
labelClassName: "",
onFocus: noop,
onBlur: noop,
type: "text",
blockAutoComplete: false,
value: "",
parseTarget: false,
tooltip: "",
hint: "",
enableCopy: false,
};
componentDidMount() {
const { autofocus } = this.props;
const { input } = this;
if (autofocus) {
input.focus();
}
return false;
}
onInputChange = (evt) => {
evt.preventDefault();
const { value, name } = evt.target;
const { onChange, parseTarget } = this.props;
if (parseTarget) {
// Returns both name and value
return onChange({ value, name });
}
return onChange(value);
};
render() {
const {
disabled,
error,
inputClassName,
inputOptions,
inputWrapperClass,
name,
onFocus,
onBlur,
placeholder,
type,
blockAutoComplete,
value,
} = this.props;
const { onInputChange } = this;
const shouldShowPasswordClass = type === "password";
const inputClasses = classnames(baseClass, inputClassName, {
[`${baseClass}--password`]: shouldShowPasswordClass,
[`${baseClass}--disabled`]: disabled,
[`${baseClass}--error`]: error,
[`${baseClass}__textarea`]: type === "textarea",
});
const formFieldProps = pick(this.props, [
"hint",
"label",
"error",
"name",
"tooltip",
]);
const copyValue = (e) => {
e.preventDefault();
stringToClipboard(value);
};
if (type === "textarea") {
return (
<FormField
{...formFieldProps}
type="textarea"
className={inputWrapperClass}
>
<textarea
name={name}
id={name}
onChange={onInputChange}
className={inputClasses}
disabled={disabled}
placeholder={placeholder}
ref={(r) => {
this.input = r;
}}
type={type}
{...inputOptions}
value={value}
/>
</FormField>
);
}
const inputContainerClasses = classnames(`${baseClass}__input-container`, {
"copy-enabled": this.props.enableCopy,
});
return (
<FormField {...formFieldProps} type="input" className={inputWrapperClass}>
<div className={inputContainerClasses}>
<input
disabled={disabled}
name={name}
id={name}
onChange={onInputChange}
onFocus={onFocus}
onBlur={onBlur}
className={inputClasses}
placeholder={placeholder}
ref={(r) => {
this.input = r;
}}
type={type}
{...inputOptions}
value={value}
autoComplete={blockAutoComplete ? "new-password" : ""}
/>
<div className={`${baseClass}__copy-wrapper`}>
{this.props.enableCopy && (
<Button
variant="text-icon"
onClick={copyValue}
className={`${baseClass}__copy-value-button`}
>
<Icon name="copy" /> Copy
</Button>
)}
</div>
</div>
</FormField>
);
}
}
export default InputField;