@talend/react-containers
Version:
Provide connected components aka containers for @talend/react-cmf based on @talend/react-components.
121 lines (120 loc) • 3.61 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Component as RComponent } from 'react';
import Immutable from 'immutable';
import PropTypes from 'prop-types';
import { cmfConnect, componentState } from '@talend/react-cmf';
import { Typeahead as Component } from "@talend/react-components";
import { omit } from "lodash";
import { jsx as _jsx } from "react/jsx-runtime";
export const DISPLAY_NAME = 'Container(Typeahead)';
export const DEFAULT_STATE = new Immutable.Map({
docked: true,
searching: false,
focusedSectionIndex: null,
focusedItemIndex: null,
items: null
});
/**
* The Typeahead React container
*/
export default class Typeahead extends RComponent {
constructor(props) {
super(props);
this.onToggle = this.onToggle.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onBlur = this.onBlur.bind(this);
this.onSelect = this.onSelect.bind(this);
}
onToggle() {
this.props.setState(() => ({
docked: !this.props.state.get('docked', true),
focusedSectionIndex: null,
focusedItemIndex: null,
items: null
}));
}
onBlur(event) {
const {
onBlur
} = this.props;
this.onToggle();
if (onBlur) {
onBlur(event);
}
}
onSelect(event, value) {
const {
onSelect
} = this.props;
if (onSelect) {
onSelect(event, value);
}
}
onKeyDown(event, data) {
const {
onKeyDown
} = this.props;
const {
highlightedItemIndex,
newHighlightedItemIndex,
highlightedSectionIndex,
newHighlightedSectionIndex
} = data;
if (onKeyDown) {
onKeyDown(event, data);
}
switch (event.key) {
case 'Down':
case 'ArrowDown':
case 'Up':
case 'ArrowUp':
event.preventDefault();
this.props.setState(() => ({
focusedSectionIndex: newHighlightedSectionIndex,
focusedItemIndex: newHighlightedItemIndex
}));
break;
case 'Enter':
event.preventDefault();
if (highlightedItemIndex !== null && highlightedItemIndex !== null) {
this.onSelect(event, {
sectionIndex: highlightedSectionIndex,
itemIndex: highlightedItemIndex
});
}
break;
case 'Esc':
case 'Escape':
event.preventDefault();
this.onBlur(event);
break;
default:
}
}
render() {
const {
items
} = this.props;
const props = {
...omit(this.props, cmfConnect.INJECTED_PROPS),
...this.props.state.toJS(),
onToggle: this.onToggle,
onBlur: this.onBlur,
onSelect: this.onSelect,
onKeyDown: this.onKeyDown,
items: items && items.toJS ? items.toJS() : items
};
return /*#__PURE__*/_jsx(Component, {
...props
});
}
}
_defineProperty(Typeahead, "displayName", DISPLAY_NAME);
_defineProperty(Typeahead, "propTypes", {
...componentState.propTypes,
onSelect: PropTypes.func,
onBlur: PropTypes.func
});
//# sourceMappingURL=Typeahead.container.js.map