Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TagsControl; fix TagsEditorModal animation #3399

Merged
merged 5 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 13 additions & 28 deletions client/app/components/tags-control/TagsControl.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject, map, filter, trim, extend } from 'lodash';
import { map, trim, extend } from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { react2angular } from 'react2angular';
Expand All @@ -24,23 +24,12 @@ export class TagsControl extends React.Component {
children: null,
};

static Prepend({ children }) {
return <React.Fragment>{children}</React.Fragment>;
}

static Append({ children }) {
return <React.Fragment>{children}</React.Fragment>;
}

state = {
showModal: false,
availableTags: [],
};

openEditModal = () => {
// load available tags every time modal is shown; show modal only when tags loaded
this.props.getAvailableTags()
.then(availableTags => this.setState({ showModal: true, availableTags }));
this.setState({ showModal: true });
};

closeEditModal = () => {
Expand All @@ -64,7 +53,7 @@ export class TagsControl extends React.Component {
{this.state.showModal && (
<TagsEditorModal
tags={tags}
availableTags={this.state.availableTags}
getAvailableTags={this.props.getAvailableTags}
onConfirm={this.onTagsChanged}
onCancel={this.closeEditModal}
/>
Expand All @@ -74,14 +63,12 @@ export class TagsControl extends React.Component {
}

render() {
const children = filter(React.Children.toArray(this.props.children), isObject);
return (
<div className={'tags-control ' + this.props.className}>
{filter(children, child => child.type === this.constructor.Prepend)}
{this.props.children}
{map(this.props.tags, tag => (
<span className="label label-tag" key={tag} title={tag}>{tag}</span>
))}
{filter(children, child => child.type === this.constructor.Append)}
{this.props.canEdit && this.renderEditButton()}
</div>
);
Expand All @@ -94,21 +81,19 @@ function modelTagsControl({ archivedTooltip }) {
function ModelTagsControl({ isDraft, isArchived, ...props }) {
return (
<TagsControl {...props}>
<TagsControl.Prepend>
{!isArchived && isDraft && (
<span className="label label-tag-unpublished">Unpublished</span>
)}
{isArchived && (
<Tooltip placement="right" title={archivedTooltip}>
<span className="label label-tag-archived">Archived</span>
</Tooltip>
)}
</TagsControl.Prepend>
{!isArchived && isDraft && (
<span className="label label-tag-unpublished">Unpublished</span>
)}
{isArchived && (
<Tooltip placement="right" title={archivedTooltip}>
<span className="label label-tag-archived">Archived</span>
</Tooltip>
)}
</TagsControl>
);
}

// `extend` needed just for `react2angular`, so remove it when `react2angular` no longer needed
// ANGULAR_REMOVE_ME `extend` needed just for `react2angular`, so remove it when `react2angular` no longer needed
ModelTagsControl.propTypes = extend({
isDraft: PropTypes.bool,
isArchived: PropTypes.bool,
Expand Down
27 changes: 16 additions & 11 deletions client/app/components/tags-control/TagsEditorModal.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map, trim, chain } from 'lodash';
import { map, trim, uniq } from 'lodash';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chains

import React from 'react';
import PropTypes from 'prop-types';
import Select from 'antd/lib/select';
Expand All @@ -7,14 +7,13 @@ import Modal from 'antd/lib/modal';
export default class TagsEditorModal extends React.Component {
static propTypes = {
tags: PropTypes.arrayOf(PropTypes.string),
availableTags: PropTypes.arrayOf(PropTypes.string),
getAvailableTags: PropTypes.func.isRequired,
onConfirm: PropTypes.func,
onCancel: PropTypes.func,
};

static defaultProps = {
tags: [],
availableTags: [],
onConfirm: () => {},
onCancel: () => {},
};
Expand All @@ -24,16 +23,20 @@ export default class TagsEditorModal extends React.Component {

this.state = {
isVisible: true,
result: chain(this.props.tags).map(trim).uniq().value(),
loading: true,
availableTags: [],
result: uniq(map(this.props.tags, trim)),
onAfterClose: () => {},
};
}

this.selectOptions =
chain(this.props.availableTags)
.map(trim)
.uniq()
.map(tag => <Select.Option key={tag}>{tag}</Select.Option>)
.value();
componentDidMount() {
this.props.getAvailableTags().then((availableTags) => {
this.setState({
loading: false,
availableTags: uniq(map(availableTags, trim)),
});
});
}

onConfirm(result) {
Expand Down Expand Up @@ -66,8 +69,10 @@ export default class TagsEditorModal extends React.Component {
defaultValue={this.state.result}
onChange={values => this.setState({ result: map(values, trim) })}
autoFocus
disabled={this.state.loading}
loading={this.state.loading}
>
{this.selectOptions}
{map(this.state.availableTags, tag => <Select.Option key={tag}>{tag}</Select.Option>)}
</Select>
</Modal>
);
Expand Down