From e7e862c06e93f940f20723458acbfe5933424b2f Mon Sep 17 00:00:00 2001 From: Ran Byron Date: Tue, 29 Jan 2019 22:59:58 +0200 Subject: [PATCH] Fix: Escape button in tag edit modal --- .../components/tags-control/TagsControl.jsx | 82 +++++++++------ .../tags-control/TagsEditorModal.jsx | 99 ++++++------------- 2 files changed, 81 insertions(+), 100 deletions(-) diff --git a/client/app/components/tags-control/TagsControl.jsx b/client/app/components/tags-control/TagsControl.jsx index 0a28c0f98d..f514577261 100644 --- a/client/app/components/tags-control/TagsControl.jsx +++ b/client/app/components/tags-control/TagsControl.jsx @@ -1,7 +1,7 @@ import { map, trim } from 'lodash'; -import React from 'react'; +import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; -import { $uibModal } from '@/services/ng'; +import TagsEditorModal from './TagsEditorModal'; export default class TagsControl extends React.Component { static propTypes = { @@ -15,27 +15,36 @@ export default class TagsControl extends React.Component { static defaultProps = { tags: [], canEdit: false, - getAvailableTags: () => {}, + getAvailableTags: () => Promise.resolve([]), onEdit: () => {}, className: '', }; - editTags() { - const { getAvailableTags, onEdit } = this.props; // eslint-disable-line react/prop-types - const tags = map(this.props.tags, trim); + constructor(props) { + super(props); + + this.state = { + showModal: false, + }; - getAvailableTags().then((availableTags) => { - $uibModal - .open({ - component: 'tagsEditorModal', - resolve: { - tags: () => tags, - availableTags: () => availableTags, - }, - }).result.then((newTags) => { - onEdit(newTags); - }); - }); + // get available tags + this.props.getAvailableTags() + .then((tags) => { + this.availableTags = tags; + }); + } + + onTagsChanged = (newTags) => { + this.props.onEdit(newTags); + this.closeEditModal(); + } + + openEditModal = () => { + this.setState({ showModal: true }); + } + + closeEditModal = () => { + this.setState({ showModal: false }); } // eslint-disable-next-line class-methods-use-this @@ -55,19 +64,32 @@ export default class TagsControl extends React.Component { } renderEditButton() { - if (this.props.canEdit) { - return (this.props.tags.length > 0) ? ( - this.editTags()}> - - - ) : ( - this.editTags()}> - - Add tag - - ); + if (!this.props.canEdit) { + return null; } - return null; + + const tags = map(this.props.tags, trim); + + const buttonLabel = tags.length > 0 + ? + : Add tag; + + return ( + + + {buttonLabel} + + {this.state.showModal + ? + : null + } + + ); } render() { diff --git a/client/app/components/tags-control/TagsEditorModal.jsx b/client/app/components/tags-control/TagsEditorModal.jsx index 0b24ad8494..1f31219c0a 100644 --- a/client/app/components/tags-control/TagsEditorModal.jsx +++ b/client/app/components/tags-control/TagsEditorModal.jsx @@ -1,12 +1,12 @@ -import { map, trim, uniq } from 'lodash'; +import { map, trim, chain } from 'lodash'; import React from 'react'; import PropTypes from 'prop-types'; -import { react2angular } from 'react2angular'; import Select from 'antd/lib/select'; +import Modal from 'antd/lib/modal'; const { Option } = Select; -class TagsEditorModal extends React.Component { +export default class TagsEditorModal extends React.Component { static propTypes = { tags: PropTypes.arrayOf(PropTypes.string), availableTags: PropTypes.arrayOf(PropTypes.string), @@ -23,81 +23,40 @@ class TagsEditorModal extends React.Component { constructor(props) { super(props); + this.state = { - result: uniq(map(this.props.tags, trim)), + result: chain(this.props.tags).map(trim).uniq().value(), }; - this.selectRef = React.createRef(); - } - componentDidMount() { - // `autoFocus` does not work on Select because its `componentDidMount` is fired before the component - // is actually visible, so it cannot get focus. This hack should be replaced with `autoFocus` prop - // when Angular will finally gone - setTimeout(() => { - if ( - this.selectRef.current && - this.selectRef.current.rcSelect && - this.selectRef.current.rcSelect.inputRef - ) { - this.selectRef.current.rcSelect.inputRef.focus(); - } - }); + this.selectOptions = + chain(this.props.availableTags) + .map(trim) + .uniq() + .map(tag => ) + .value(); } render() { - const { - availableTags, - close, - dismiss, - } = this.props; - - const uniqueAvailableTags = uniq(map(availableTags, trim)); + const { close, dismiss } = this.props; + const { result } = this.state; return ( -
-
- -

Add/Edit Tags

-
-
- -
-
- - -
-
+ close(result)} + onCancel={dismiss} + > + + ); } } - -export default function init(ngModule) { - ngModule.component('tagsEditorModal', { - template: ` - - `, - bindings: { - resolve: '<', - close: '&', - dismiss: '&', - }, - }); - ngModule.component('tagsEditorModalImpl', react2angular(TagsEditorModal)); -} - -init.init = true;