Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private handleItemCreate = (query: string, evt?: React.SyntheticEvent) => {
// we keep a cached createNewItem in state, but might as well recompute
// the result just to be sure it's perfectly in sync with the query.
const item = Utils.safeInvoke(this.props.createNewItemFromQuery, query);
if (item != null) {
Utils.safeInvoke(this.props.onItemSelect, item, evt);
this.setQuery("", true);
}
};
let nextActiveItem: T | undefined;
const nextQueries = [];
// Find an exising item that exactly matches each pasted value, or
// create a new item if possible. Ignore unmatched values if creating
// items is disabled.
const pastedItemsToEmit = [];
for (const query of queries) {
const equalItem = getMatchingItem(query, this.props);
if (equalItem !== undefined) {
nextActiveItem = equalItem;
pastedItemsToEmit.push(equalItem);
} else if (this.canCreateItems()) {
const newItem = Utils.safeInvoke(createNewItemFromQuery, query);
if (newItem !== undefined) {
pastedItemsToEmit.push(newItem);
}
} else {
nextQueries.push(query);
}
}
// UX nicety: combine all unmatched queries into a single
// comma-separated query in the input, so we don't lose any information.
// And don't reset the active item; we'll do that ourselves below.
this.setQuery(nextQueries.join(", "), false);
// UX nicety: update the active item if we matched with at least one
// existing item.
if (nextActiveItem !== undefined) {
this.input.blur();
}
nextOpenState = false;
}
// the internal state should only change when uncontrolled.
if (this.props.selectedItem === undefined) {
this.setState({
isOpen: nextOpenState,
selectedItem: item,
});
} else {
// otherwise just set the next open state.
this.setState({ isOpen: nextOpenState });
}
Utils.safeInvoke(this.props.onItemSelect, item, event);
};
private handleKeyUp = (event: React.KeyboardEvent) => {
const { onKeyUp } = this.props;
const { activeItem } = this.state;
// using keyup for enter to play nice with Button's keyboard clicking.
// if we were to process enter on keydown, then Button would click itself on keyup
// and the popvoer would re-open out of our control :(.
if (event.keyCode === Keys.ENTER) {
event.preventDefault();
if (activeItem == null || isCreateNewItem(activeItem)) {
this.handleItemCreate(this.state.query, event);
} else {
this.handleItemSelect(activeItem, event);
}
}
Utils.safeInvoke(onKeyUp, event);
};
this.setState(newState);
} else {
// component is controlled, and there's a new value
// so set inputs' text based off of _old_ value and later fire onChange with new value
if (hasNewValue) {
this.setState(this.getFullStateFromValue(this.state.value, this.props.useAmPm));
} else {
// no new value, this means only text has changed (from user typing)
// we want inputs to change, so update state with new text for the inputs
// but don't change actual value
this.setState({ ...newState, value: DateUtils.clone(this.state.value) });
}
}
if (hasNewValue) {
BlueprintUtils.safeInvoke(this.props.onChange, newState.value);
}
}
}
if (which === Keys.ESCAPE || which === Keys.TAB) {
if (this.input != null) {
this.input.blur();
}
this.setState({ isOpen: false });
} else if (
this.props.openOnKeyDown &&
which !== Keys.BACKSPACE &&
which !== Keys.ARROW_LEFT &&
which !== Keys.ARROW_RIGHT
) {
this.setState({ isOpen: true });
}
if (this.state.isOpen) {
Utils.safeInvoke(handleQueryListKeyDown, evt);
}
Utils.safeInvokeMember(this.props.inputProps, "onKeyDown", evt);
};
};
this.handleInputFocus(e, boundary);
break;
case "keydown":
this.handleInputKeyDown(e as React.KeyboardEvent);
break;
case "mousedown":
this.handleInputMouseDown();
break;
default:
break;
}
const inputProps = this.getInputProps(boundary);
const callbackFn = this.getInputGroupCallbackForEvent(e, inputProps);
Utils.safeInvoke(callbackFn, e);
};
private safeInvokeInputProp(name: keyof HTMLInputProps, e: React.SyntheticEvent) {
const { inputProps = {} } = this.props;
Utils.safeInvoke(inputProps[name], e);
}
valueString.length > 0 &&
valueString !== getFormattedDateString(this.state.value, this.props) &&
(!isDateValid(date) || !this.isDateInRange(date))
) {
if (this.props.value === undefined) {
this.setState({ isInputFocused: false, value: date, valueString: null });
} else {
this.setState({ isInputFocused: false });
}
if (isNaN(date.valueOf())) {
Utils.safeInvoke(this.props.onError, new Date(undefined));
} else if (!this.isDateInRange(date)) {
Utils.safeInvoke(this.props.onError, date);
} else {
Utils.safeInvoke(this.props.onChange, date, true);
}
} else {
if (valueString.length === 0) {
this.setState({ isInputFocused: false, value: null, valueString: null });
} else {
this.setState({ isInputFocused: false });
}
}
this.registerPopoverBlurHandler();
this.safeInvokeInputProp("onBlur", e);
};
const date = this.parseDate(valueString);
if (
valueString.length > 0 &&
valueString !== getFormattedDateString(this.state.value, this.props) &&
(!isDateValid(date) || !this.isDateInRange(date))
) {
if (this.props.value === undefined) {
this.setState({ isInputFocused: false, value: date, valueString: null });
} else {
this.setState({ isInputFocused: false });
}
if (isNaN(date.valueOf())) {
Utils.safeInvoke(this.props.onError, new Date(undefined));
} else if (!this.isDateInRange(date)) {
Utils.safeInvoke(this.props.onError, date);
} else {
Utils.safeInvoke(this.props.onChange, date, true);
}
} else {
if (valueString.length === 0) {
this.setState({ isInputFocused: false, value: null, valueString: null });
} else {
this.setState({ isInputFocused: false });
}
}
this.registerPopoverBlurHandler();
this.safeInvokeInputProp("onBlur", e);
};