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

In example 9, removed findItemElement function #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
98 changes: 46 additions & 52 deletions demo/src/components/App/components/Example9/Example9.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
import theme from '../theme.less';

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import match from 'autosuggest-highlight/match';
import parse from 'autosuggest-highlight/parse';
import { updateInputValue, hideItems, updateHighlightedItem } from '../../redux';
import Autowhatever from 'Autowhatever';
import SourceCodeLink from 'SourceCodeLink/SourceCodeLink';
import countries from './countries';
import { escapeRegexCharacters } from '../../utils';
import IsolatedScroll from 'react-isolated-scroll';

const exampleId = '9';
import theme from "../theme.less";

import React from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import match from "autosuggest-highlight/match";
import parse from "autosuggest-highlight/parse";
import {
updateInputValue,
hideItems,
updateHighlightedItem
} from "../../redux";
import Autowhatever from "Autowhatever";
import SourceCodeLink from "SourceCodeLink/SourceCodeLink";
import countries from "./countries";
import { escapeRegexCharacters } from "../../utils";
import IsolatedScroll from "react-isolated-scroll";

const exampleId = "9";
const file = `demo/src/components/App/components/Example${exampleId}/Example${exampleId}.js`;

function getMatchingCountries(value) {
const escapedValue = escapeRegexCharacters(value.trim());

if (escapedValue === '') {
if (escapedValue === "") {
return [];
}

const regex = new RegExp('^' + escapedValue, 'i');
const regex = new RegExp("^" + escapedValue, "i");

return countries.filter(country => regex.test(country.name));
}

function findItemElement(startNode) {
let node = startNode;

do {
if (node.getAttribute('data-item-index') !== null) {
return node;
}

node = node.parentNode;
} while (node !== null);

console.error('Clicked item:', startNode); // eslint-disable-line no-console
throw new Error('Couldn\'t find the clicked item element');
}

function mapStateToProps(state) {
return {
value: state[exampleId].value,
Expand All @@ -60,7 +49,7 @@ function mapDispatchToProps(dispatch) {
dispatch(updateInputValue(exampleId, newValue, newItems));
},
onFocus: () => {
dispatch(updateInputValue(exampleId, ''));
dispatch(updateInputValue(exampleId, ""));
},
onBlur: () => {
dispatch(hideItems(exampleId));
Expand Down Expand Up @@ -98,41 +87,46 @@ function renderItem(country, { value }) {

return (
<span>
{
parts.map((part, index) => {
const className = part.highlight ? theme.highlight : null;

return (
<span className={className} key={index}>{part.text}</span>
);
})
}
{parts.map((part, index) => {
const className = part.highlight ? theme.highlight : null;

return (
<span className={className} key={index}>
{part.text}
</span>
);
})}
</span>
);
}

function Example(props) {
const {
value, highlightedSectionIndex, highlightedItemIndex, items,
onChange, onFocus, onBlur, onMouseEnter, onMouseLeave, onMouseDown
value,
highlightedSectionIndex,
highlightedItemIndex,
items,
onChange,
onFocus,
onBlur,
onMouseEnter,
onMouseLeave,
onMouseDown
} = props;
const inputProps = {
placeholder: 'Search and click countries',
placeholder: "Search and click countries",
value,
onChange,
onFocus,
onBlur
};
const itemProps = ({ itemIndex }) => ({
'data-item-index': itemIndex,
"data-item-index": itemIndex,
onMouseEnter,
onMouseLeave,
onMouseDown: event => {
const clickedItem = findItemElement(event.target);
const clickedItemIndex = clickedItem.getAttribute('data-item-index');

onMouseDown(items[clickedItemIndex]);
}
onMouseDown: (index => {
return event => onMouseDown(items[index]);
})(itemIndex)
});

return (
Expand Down