UNPKG

@atlaskit/editor-plugin-extension

Version:

editor-plugin-extension plugin for @atlaskit/editor-core

196 lines (194 loc) 7.71 kB
import _extends from "@babel/runtime/helpers/extends"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import React, { useEffect, useState } from 'react'; import { injectIntl } from 'react-intl'; import { getCustomFieldResolver, configPanelMessages as messages } from '@atlaskit/editor-common/extensions'; import { Field } from '@atlaskit/form'; import { AsyncCreatableSelect } from '@atlaskit/select'; import FieldMessages from '../FieldMessages'; import { validate as _validate } from '../utils'; import { formatOptionLabel } from './SelectItem'; import UnhandledType from './UnhandledType'; function FieldError(_ref) { var name = _ref.name, field = _ref.field; var type = field.options.resolver.type; return /*#__PURE__*/React.createElement(UnhandledType, { key: name, field: field, errorMessage: "Field \"".concat(name, "\" can't be rendered. Missing resolver for \"").concat(type, "\".") }); } function CustomSelect(_ref2) { var name = _ref2.name, autoFocus = _ref2.autoFocus, extensionManifest = _ref2.extensionManifest, placeholder = _ref2.placeholder, field = _ref2.field, onFieldChange = _ref2.onFieldChange, parameters = _ref2.parameters, intl = _ref2.intl; var fieldDefaultValue = field.defaultValue, description = field.description, isMultiple = field.isMultiple, isRequired = field.isRequired, label = field.label, options = field.options, isDisabled = field.isDisabled; var _useState = useState(true), _useState2 = _slicedToArray(_useState, 2), loading = _useState2[0], setLoading = _useState2[1]; var _useState3 = useState(null), _useState4 = _slicedToArray(_useState3, 2), resolver = _useState4[0], setResolver = _useState4[1]; var _useState5 = useState([]), _useState6 = _slicedToArray(_useState5, 2), defaultOptions = _useState6[0], setDefaultOptions = _useState6[1]; var _useState7 = useState(undefined), _useState8 = _slicedToArray(_useState7, 2), defaultValue = _useState8[0], setDefaultValue = _useState8[1]; useEffect(function () { var cancel = false; function fetchResolver() { return _fetchResolver.apply(this, arguments); } function _fetchResolver() { _fetchResolver = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _resolver, _options; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: setLoading(true); _context.prev = 1; _resolver = getCustomFieldResolver(extensionManifest, field.options.resolver); if (!cancel) { _context.next = 5; break; } return _context.abrupt("return"); case 5: setResolver(function () { return _resolver; }); // fetch the default values _context.next = 8; return _resolver(undefined, fieldDefaultValue, parameters); case 8: _options = _context.sent; setDefaultOptions(_options); if (!cancel) { _context.next = 12; break; } return _context.abrupt("return"); case 12: // filter returned values to match the defaultValue if (fieldDefaultValue && isMultiple) { setDefaultValue(_options.filter(function (option) { return fieldDefaultValue.includes(option.value); })); } if (fieldDefaultValue && !isMultiple) { setDefaultValue(_options.find(function (option) { return fieldDefaultValue === option.value; })); } _context.next = 19; break; case 16: _context.prev = 16; _context.t0 = _context["catch"](1); // eslint-disable-next-line no-console console.error(_context.t0); case 19: setLoading(false); case 20: case "end": return _context.stop(); } }, _callee, null, [[1, 16]]); })); return _fetchResolver.apply(this, arguments); } fetchResolver(); return function () { cancel = true; }; }, [extensionManifest, field.options.resolver, fieldDefaultValue, isMultiple, parameters]); function _formatCreateLabel(value) { if (!value) { return null; } var message = intl.formatMessage(messages.createOption); return "".concat(message, " \"").concat(value, "\""); } var isCreatable = options.isCreatable, customFormatCreateLabel = options.formatCreateLabel; return /*#__PURE__*/React.createElement(Field, { name: name, label: label, isRequired: isRequired, defaultValue: defaultValue // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , validate: function validate(value) { return _validate(field, value); }, testId: "config-panel-custom-select-".concat(name), isDisabled: isDisabled }, function (_ref3) { var fieldProps = _ref3.fieldProps, error = _ref3.error; return /*#__PURE__*/React.createElement(React.Fragment, null, resolver && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(AsyncCreatableSelect // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading , _extends({}, fieldProps, { // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) onChange: function onChange(value) { fieldProps.onChange(value); // We assume onChange is called whenever values actually changed // for isDirty onFieldChange(name, true); } // add type cast to avoid adding a "IsMulti" generic prop (TODO: ED-12072) , isMulti: isMultiple || false, isClearable: true // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , isValidNewOption: function isValidNewOption(value) { return !!(isCreatable && value); }, validationState: error ? 'error' : 'default', defaultOptions: defaultOptions // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , formatCreateLabel: function formatCreateLabel(value) { return customFormatCreateLabel ? customFormatCreateLabel(value) : _formatCreateLabel(value); }, formatOptionLabel: formatOptionLabel // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , loadOptions: function loadOptions(searchTerm) { return resolver(searchTerm, fieldDefaultValue, parameters); }, autoFocus: autoFocus, placeholder: placeholder })), /*#__PURE__*/React.createElement(FieldMessages, { error: error, description: description })), !loading && !resolver && /*#__PURE__*/React.createElement(FieldError, { name: name, field: field })); }); } // eslint-disable-next-line @typescript-eslint/ban-types var _default_1 = injectIntl(CustomSelect); export default _default_1;