This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathpicker.jsx
167 lines (147 loc) · 4.33 KB
/
picker.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React, { Component } from "react";
import PropTypes from "prop-types";
import emojione from "emojione";
import store from "store";
import each from "lodash/each";
import map from "lodash/map";
import omit from "lodash/omit";
import strategy from "emojione/emoji.json";
import Emoji from "./emoji";
import Categories from "./categories";
import createRowsSelector from "./utils/createRowsSelector";
import createEmojisFromStrategy from "./utils/createEmojisFromStrategy";
import { defaultCategories } from "./constants";
export default class Picker extends Component {
static propTypes = {
emojione: PropTypes.shape({
imageType: PropTypes.string,
sprites: PropTypes.bool,
imagePathSVGSprites: PropTypes.string
}),
search: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
searchPlaceholder: PropTypes.string,
className: PropTypes.string,
onChange: PropTypes.func.isRequired,
categories: PropTypes.object
};
static defaultProps = {
search: "",
searchPlaceholder: "Search…",
categories: defaultCategories
};
state = {
modifier: store.get("emoji-modifier") || "0",
category: false,
term: this.props.search !== true ? this.props.search : ""
};
componentWillMount() {
this.rowsSelector = createRowsSelector();
each(this.props.emojione, (value, key) => {
emojione[key] = value;
});
this.setState({ emojis: createEmojisFromStrategy(strategy) });
}
componentDidMount() {
this.setState({ category: this.categories.getActiveCategory() }); // eslint-disable-line
}
componentWillReceiveProps(nextProps) {
if (this.props.search !== nextProps.search) {
this.setState({ term: this.props.search });
}
}
setFocus = ev => {
if (ev.target.id === "flags") {
this.categories[this.state.category].children[0].focus();
}
};
_renderHeaderCategories = () => {
return map(this.props.categories, (details, key) => (
<li
key={key}
className={this.state.category === key ? "active" : undefined}
>
<Emoji
id={key}
role="menuitem"
ariaLabel={`${key} category`}
shortname={`:${details.emoji}:`}
onSelect={() => {
this.categories.jumpToCategory(key);
}}
/>
</li>
));
};
_renderCategories = () => {
const rows = this.rowsSelector(
this.props.categories,
this.state.emojis,
this.state.modifier,
this.props.search,
this.state.term
);
return (
<div className="emoji-categories-wrapper">
<Categories
nonce={this.props.nonce}
ref={this._setCategoriesRef}
rows={rows}
modifier={this.state.modifier}
onActiveCategoryChange={this._onActiveCategoryChange}
onChange={this.props.onChange}
onModifierChange={this._onModifierChange}
/>
</div>
);
};
_setCategoriesRef = categories => {
this.categories = categories;
};
_setSearchRef = ref => {
this.search = ref;
};
_onActiveCategoryChange = category => {
if (category !== this.state.category) {
this.setState({ category });
}
};
_onModifierChange = modifier => {
this.setState({ modifier });
store.set("emoji-modifier", modifier);
};
_renderSearchInput = () => {
if (this.props.search === true) {
return (
<div className="emoji-search-wrapper">
<input
className="emoji-search"
type="search"
placeholder={this.props.searchPlaceholder}
ref={this._setSearchRef}
onChange={this._updateSearchTerm}
autoFocus
/>
</div>
);
}
return null;
};
_updateSearchTerm = () => {
this.setState({ term: this.search.value });
};
render() {
const props = omit(this.props, Object.keys(Picker.propTypes));
let classes = "emoji-dialog";
if (this.props.search === true) classes += " with-search";
if (this.props.className) classes += ` ${this.props.className}`;
return (
<div className={classes} role="dialog" {...props}>
<header className="emoji-dialog-header" role="menu">
<ul onBlur={this.setFocus}>{this._renderHeaderCategories()}</ul>
</header>
{this._renderSearchInput()}
{this._renderCategories()}
</div>
);
}
}