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

Feature - autofill symbol search #1064

Merged
84 changes: 62 additions & 22 deletions src/components/Board/SymbolSearch/SymbolSearch.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import Symbol from '../Symbol';
import { LABEL_POSITION_BELOW } from '../../Settings/Display/Display.constants';
import messages from './SymbolSearch.messages';
import './SymbolSearch.css';
import { IconButton, Tooltip } from '@material-ui/core';
import BackspaceIcon from '@material-ui/icons/Backspace';

const SymbolSets = {
mulberry: '0',
Expand Down Expand Up @@ -47,6 +49,7 @@ export class SymbolSearch extends PureComponent {
static propTypes = {
intl: intlShape.isRequired,
open: PropTypes.bool,
autoFill: PropTypes.string,
maxSuggestions: PropTypes.number,
onChange: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired
Expand All @@ -60,6 +63,7 @@ export class SymbolSearch extends PureComponent {
constructor(props) {
super(props);
this.state = {
openMirror: false,
value: '',
suggestions: [],
skin: 'white',
Expand All @@ -78,6 +82,16 @@ export class SymbolSearch extends PureComponent {
);
}

static getDerivedStateFromProps(nextProps, prevState) {
const { open, autoFill } = nextProps;
const { openMirror: wasOpen } = prevState;

if (open === true && wasOpen === false)
return { value: autoFill, openMirror: true };
if (open === false) return { openMirror: false };
return null;
}

translateSymbols(symbols = []) {
return symbols.map(symbol => {
const translatedId = this.props.intl
Expand Down Expand Up @@ -252,12 +266,14 @@ export class SymbolSearch extends PureComponent {
handleSuggestionsClearRequested = () => {};

handleSuggestionSelected = (event, { suggestion }) => {
const { onChange, onClose } = this.props;
const { onChange, onClose, autoFill } = this.props;
this.setState({ value: '' });

const label = autoFill.length ? autoFill : suggestion.translatedId;

onChange({
image: suggestion.src,
label: suggestion.translatedId,
label: label,
labelKey: undefined
}).then(() => onClose());
};
Expand Down Expand Up @@ -302,30 +318,54 @@ export class SymbolSearch extends PureComponent {
this.getSuggestions(this.state.value);
};

handleClearSuggest() {
this.setState({ value: '' });
}

render() {
const { intl, open, onClose } = this.props;

const clearButton =
this.state.value.length > 0 ? (
<div className="react-autosuggest__clear">
<Tooltip
title={intl.formatMessage(messages.clearText)}
aria-label={intl.formatMessage(messages.clearText)}
>
<IconButton
label={intl.formatMessage(messages.clearText)}
onClick={this.handleClearSuggest.bind(this)}
>
<BackspaceIcon style={{ color: 'white' }} />
</IconButton>
</Tooltip>
</div>
) : null;

const autoSuggest = (
<Autosuggest
aria-label="Search auto-suggest"
alwaysRenderSuggestions={true}
suggestions={this.state.suggestions}
focusInputOnSuggestionClick={!isMobile.any}
onSuggestionsFetchRequested={this.handleSuggestionsFetchRequested}
onSuggestionsClearRequested={this.handleSuggestionsClearRequested}
onSuggestionSelected={this.handleSuggestionSelected}
renderSuggestionsContainer={this.renderSuggestionsContainer}
getSuggestionValue={this.getSuggestionValue}
renderSuggestion={this.renderSuggestion}
highlightFirstSuggestion={true}
inputProps={{
autoFocus: true,
placeholder: intl.formatMessage(messages.searchSymbolLibrary),
label: intl.formatMessage(messages.searchSymbolLibrary),
value: this.state.value,
onChange: this.handleChange
}}
/>
<div className="react-autosuggest__container">
<Autosuggest
aria-label="Search auto-suggest"
alwaysRenderSuggestions={true}
suggestions={this.state.suggestions}
focusInputOnSuggestionClick={!isMobile.any}
onSuggestionsFetchRequested={this.handleSuggestionsFetchRequested}
onSuggestionsClearRequested={this.handleSuggestionsClearRequested}
onSuggestionSelected={this.handleSuggestionSelected}
renderSuggestionsContainer={this.renderSuggestionsContainer}
getSuggestionValue={this.getSuggestionValue}
renderSuggestion={this.renderSuggestion}
highlightFirstSuggestion={true}
inputProps={{
autoFocus: true,
placeholder: intl.formatMessage(messages.searchSymbolLibrary),
label: intl.formatMessage(messages.searchSymbolLibrary),
value: this.state.value,
onChange: this.handleChange
}}
/>
{clearButton}
</div>
);

return (
Expand Down
4 changes: 4 additions & 0 deletions src/components/Board/SymbolSearch/SymbolSearch.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
color: rgba(255, 255, 255, 0.26);
}

.react-autosuggest__clear {
margin-right: 1em;
}

.react-autosuggest__suggestions-container {
position: absolute;
top: 120px;
Expand Down
4 changes: 4 additions & 0 deletions src/components/Board/SymbolSearch/SymbolSearch.messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default defineMessages({
id: 'cboard.components.SymbolSearch.searchSymbolLibrary',
defaultMessage: 'Search symbol library'
},
clearText: {
id: 'cboard.components.SymbolSearch.clearText',
defaultMessage: 'Clear text'
},
uploadAnImage: {
id: 'cboard.components.InputImage.uploadImage',
defaultMessage: 'Upload an image'
Expand Down
10 changes: 6 additions & 4 deletions src/components/Board/TileEditor/TileEditor.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class TileEditor extends Component {
activeStep: 0,
editingTiles: props.editingTiles,
isSymbolSearchOpen: false,
autoFill: '',
selectedBackgroundColor: '',
tile: this.defaultTile,
linkedBoard: '',
Expand Down Expand Up @@ -359,8 +360,8 @@ export class TileEditor extends Component {
this.setState({ isEditImageBtnActive: false });
};

handleSearchClick = event => {
this.setState({ isSymbolSearchOpen: true });
handleSearchClick = (event, currentLabel) => {
this.setState({ isSymbolSearchOpen: true, autoFill: currentLabel || '' });
this.setState({ isEditImageBtnActive: false });
};

Expand Down Expand Up @@ -422,7 +423,7 @@ export class TileEditor extends Component {
const buttons = (
<IconButton
label={intl.formatMessage(messages.symbolSearch)}
onClick={this.handleSearchClick}
onClick={e => this.handleSearchClick(e, currentLabel)}
>
<SearchIcon />
</IconButton>
Expand Down Expand Up @@ -520,7 +521,7 @@ export class TileEditor extends Component {
variant="contained"
color="primary"
startIcon={<SearchIcon />}
onClick={this.handleSearchClick}
onClick={e => this.handleSearchClick(e, currentLabel)}
>
{intl.formatMessage(messages.symbols)}
</Button>
Expand Down Expand Up @@ -651,6 +652,7 @@ export class TileEditor extends Component {

<SymbolSearch
open={this.state.isSymbolSearchOpen}
autoFill={this.state.autoFill}
onChange={this.handleSymbolSearchChange}
onClose={this.handleSymbolSearchClose}
/>
Expand Down
1 change: 1 addition & 0 deletions src/translations/src/cboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"cboard.components.FullScreenButton.fullscreen": "Full screen",
"cboard.components.FullScreenButton.exitFullscreen": "Exit full screen",
"cboard.components.SymbolSearch.searchSymbolLibrary": "Search symbol library",
"cboard.components.SymbolSearch.clearText": "Clear text",
"cboard.components.InputImage.uploadImage": "Upload image",
"cboard.components.Board.TileEditor.voiceRecorder": "Voice recorder",
"cboard.components.Board.TileEditor.createTile": "Create tile",
Expand Down