@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
141 lines (140 loc) • 6.88 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
var _excluded = ["open", "closeIconDescription", "removeType", "cancelText", "deleteText", "onClose", "onDelete", "deleteContent", "postDeleteContent", "highImpactItemName", "highImpactTextFieldLabel"];
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
//
// Copyright IBM Corp. 2020, 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//
import React from 'react';
import PropTypes from 'prop-types';
import { Modal, TextInput } from 'carbon-components-react';
import { idAttribute } from '../../component_helpers/IDHelper';
import { idePrefix } from '../../globals/js/settings';
export var MEDIUM_IMPACT = 'medium';
export var HIGH_IMPACT = 'high';
var IdeRemove = /*#__PURE__*/function (_React$Component) {
function IdeRemove(props) {
var _this;
_classCallCheck(this, IdeRemove);
_this = _callSuper(this, IdeRemove, [props]);
_this.state = {
itemName: '',
didDelete: false
};
_this.textFieldChange = _this.textFieldChange.bind(_this);
return _this;
}
_inherits(IdeRemove, _React$Component);
return _createClass(IdeRemove, [{
key: "textFieldChange",
value: function textFieldChange(evt) {
if (evt && evt.target && evt.target.value) {
this.setState({
itemName: evt.target.value
});
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props,
open = _ref.open,
_unusedCloseIconDescription = _ref.closeIconDescription,
removeType = _ref.removeType,
cancelText = _ref.cancelText,
deleteText = _ref.deleteText,
onClose = _ref.onClose,
onDelete = _ref.onDelete,
deleteContent = _ref.deleteContent,
postDeleteContent = _ref.postDeleteContent,
highImpactItemName = _ref.highImpactItemName,
highImpactTextFieldLabel = _ref.highImpactTextFieldLabel,
others = _objectWithoutProperties(_ref, _excluded);
var didDelete = postDeleteContent ? this.state.didDelete : false;
var content = this.state.didDelete && postDeleteContent ? postDeleteContent : deleteContent;
return /*#__PURE__*/React.createElement("div", _extends({}, others, idAttribute('IdeRemove-root')), /*#__PURE__*/React.createElement(Modal, _extends({
open: open,
danger: true,
passiveModal: postDeleteContent && didDelete
}, idAttribute('IdeRemove-modal'), {
modalLabel: content.label,
modalHeading: content.heading,
"aria-label": content.label,
primaryButtonText: deleteText,
secondaryButtonText: cancelText,
onRequestClose: onClose,
onRequestSubmit: function onRequestSubmit() {
_this2.setState({
didDelete: true
});
onDelete();
},
primaryButtonDisabled: removeType === HIGH_IMPACT && highImpactItemName !== this.state.itemName
}), /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("p", idAttribute('IdeRemove-modal-body'), content.body), removeType === HIGH_IMPACT && !didDelete ? /*#__PURE__*/React.createElement("div", {
className: "".concat(idePrefix, "-remove--modal-body-textfield-container")
}, /*#__PURE__*/React.createElement(TextInput, _extends({}, idAttribute('IdeRemove-modal-body-high-impact-textfield'), {
onChange: this.textFieldChange,
id: "IdeRemove-high-impact-".concat(highImpactItemName),
labelText: highImpactTextFieldLabel
}))) : null)));
}
}]);
}(React.Component);
IdeRemove.propTypes = {
/** Text which appears in the cancel button. Must be a pre translated string. */
cancelText: PropTypes.string.isRequired,
/** closeIconDescription is no longer used, as Carbon Modal no longer uses iconDescription prop */
closeIconDescription: PropTypes.string,
/** deleteContent - the text displayed before a user clicks delete or remove. Required. */
deleteContent: PropTypes.shape({
label: PropTypes.string.isRequired,
heading: PropTypes.string.isRequired,
body: PropTypes.string.isRequired
}).isRequired,
/** Text which appears in the delete button before being clicked. Must be a pre translated string. */
deleteText: PropTypes.string.isRequired,
/** if a high impact remove, this is the name of the item being removed. This must be provided. */
highImpactItemName: PropTypes.string,
/** if a high impact remove, this is the label for the text field. Must be a pre translated string */
highImpactTextFieldLabel: PropTypes.string,
/** Callback which is invoked when the user selects cancel/closes the modal */
onClose: PropTypes.func,
/** Callback which is invoked when the user confirms the delete. Only invoked after the configured delay occurs */
onDelete: PropTypes.func,
/** Modal open/closed state. */
open: PropTypes.bool,
/** postDeleteContent - the text displayed after a user clicks delete. Optional. */
postDeleteContent: PropTypes.shape({
label: PropTypes.string.isRequired,
heading: PropTypes.string.isRequired,
body: PropTypes.string.isRequired
}),
/** Tye type of delete/remove which is being performed. See the readme for when to use which mode */
removeType: PropTypes.oneOf([MEDIUM_IMPACT, HIGH_IMPACT])
};
IdeRemove.defaultProps = {
open: false,
removeType: MEDIUM_IMPACT,
cancelText: 'Cancel - provide a translated string',
deleteText: 'Delete - provide a translated string',
onDelete: null,
onClose: null,
deleteContent: {
label: 'Remove Label - provide a translated string',
heading: 'Remove Heading - provide a translated string',
body: 'Remove Body - provide a translated string'
},
highImpactItemName: 'my entity name',
highImpactTextFieldLabel: 'Confrim remove of [name] - provide a translated string'
};
export default IdeRemove;