react-toolbox/components/dropdown/Dropdown.js

234 lines
6.8 KiB
JavaScript
Raw Normal View History

2017-01-26 20:05:32 +03:00
/* eslint-disable */
2016-05-29 20:37:40 +03:00
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
2016-05-22 22:28:48 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { DROPDOWN } from '../identifiers';
import InjectInput from '../input/Input';
import events from '../utils/events';
2016-05-29 20:37:40 +03:00
const factory = (Input) => {
class Dropdown extends Component {
static propTypes = {
allowBlank: PropTypes.bool,
auto: PropTypes.bool,
className: PropTypes.string,
disabled: PropTypes.bool,
error: PropTypes.string,
label: PropTypes.string,
name: PropTypes.string,
2016-05-29 20:37:40 +03:00
onBlur: PropTypes.func,
onChange: PropTypes.func,
2016-08-27 18:42:29 +03:00
onClick: PropTypes.func,
2016-05-29 20:37:40 +03:00
onFocus: PropTypes.func,
2016-10-11 23:12:12 +03:00
required: PropTypes.bool,
2017-01-26 20:05:32 +03:00
source: PropTypes.arrayOf(PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
])).isRequired,
2016-05-29 20:37:40 +03:00
template: PropTypes.func,
theme: PropTypes.shape({
active: PropTypes.string,
disabled: PropTypes.string,
dropdown: PropTypes.string,
error: PropTypes.string,
errored: PropTypes.string,
field: PropTypes.string,
label: PropTypes.string,
required: PropTypes.string,
selected: PropTypes.string,
templateValue: PropTypes.string,
up: PropTypes.string,
value: PropTypes.string,
2017-01-26 20:05:32 +03:00
values: PropTypes.string,
2016-05-29 20:37:40 +03:00
}),
value: PropTypes.oneOfType([
PropTypes.string,
2017-01-26 20:05:32 +03:00
PropTypes.number,
]),
2016-05-29 20:37:40 +03:00
};
static defaultProps = {
auto: true,
className: '',
allowBlank: true,
2016-10-11 23:12:12 +03:00
disabled: false,
2017-01-26 20:05:32 +03:00
required: false,
2016-05-29 20:37:40 +03:00
};
state = {
active: false,
2017-01-26 20:05:32 +03:00
up: false,
2016-05-29 20:37:40 +03:00
};
2017-01-26 20:05:32 +03:00
componentWillUpdate(nextProps, nextState) {
2016-05-29 20:37:40 +03:00
if (!this.state.active && nextState.active) {
2016-11-05 14:22:44 +03:00
events.addEventsToDocument(this.getDocumentEvents());
2016-05-29 20:37:40 +03:00
}
}
2017-01-26 20:05:32 +03:00
componentDidUpdate(prevProps, prevState) {
2016-05-29 20:37:40 +03:00
if (prevState.active && !this.state.active) {
2016-11-05 14:22:44 +03:00
events.removeEventsFromDocument(this.getDocumentEvents());
2016-05-29 20:37:40 +03:00
}
}
2017-01-26 20:05:32 +03:00
componentWillUnmount() {
2016-05-29 20:37:40 +03:00
if (this.state.active) {
2016-11-05 14:22:44 +03:00
events.removeEventsFromDocument(this.getDocumentEvents());
2016-05-29 20:37:40 +03:00
}
}
2016-11-05 14:22:44 +03:00
getDocumentEvents = () => ({
click: this.handleDocumentClick,
2017-01-26 20:05:32 +03:00
touchend: this.handleDocumentClick,
2016-11-05 14:22:44 +03:00
});
2017-01-26 20:05:32 +03:00
getSelectedItem = () => {
for (const item of this.props.source) {
if (item.value === this.props.value) return item;
2016-05-29 20:37:40 +03:00
}
2017-01-26 20:05:32 +03:00
return !this.props.allowBlank
? this.props.source[0]
: undefined;
};
2016-03-23 14:35:24 +03:00
2017-01-26 20:05:32 +03:00
handleSelect = (item, event) => {
if (this.props.onBlur) this.props.onBlur(event);
if (!this.props.disabled && this.props.onChange) {
if (this.props.name) event.target.name = this.props.name;
this.props.onChange(item, event);
this.close();
2016-05-29 20:37:40 +03:00
}
};
handleClick = (event) => {
this.open(event);
2016-05-29 20:37:40 +03:00
events.pauseEvent(event);
if (this.props.onClick) this.props.onClick(event);
2016-05-29 20:37:40 +03:00
};
2017-01-26 20:05:32 +03:00
handleDocumentClick = (event) => {
if (this.state.active && !events.targetIsDescendant(event, ReactDOM.findDOMNode(this))) {
this.setState({ active: false });
2016-05-29 20:37:40 +03:00
}
};
2017-01-26 20:05:32 +03:00
close = () => {
if (this.state.active) {
this.setState({ active: false });
2016-05-29 20:37:40 +03:00
}
2017-01-26 20:05:32 +03:00
}
open = (event) => {
const client = event.target.getBoundingClientRect();
const screenHeight = window.innerHeight || document.documentElement.offsetHeight;
const up = this.props.auto ? client.top > ((screenHeight / 2) + client.height) : false;
if (this.inputNode) this.inputNode.blur();
this.setState({ active: true, up });
};
handleFocus = (event) => {
event.stopPropagation();
if (!this.props.disabled) this.open(event);
if (this.props.onFocus) this.props.onFocus(event);
2016-05-29 20:37:40 +03:00
};
2017-01-26 20:05:32 +03:00
handleBlur = (event) => {
event.stopPropagation();
if (this.state.active) this.close();
if (this.props.onBlur) this.props.onBlur(event);
}
renderTemplateValue(selected) {
2016-05-29 20:37:40 +03:00
const { theme } = this.props;
const className = classnames(theme.field, {
[theme.errored]: this.props.error,
2016-10-11 23:12:12 +03:00
[theme.disabled]: this.props.disabled,
2017-01-26 20:05:32 +03:00
[theme.required]: this.props.required,
2016-05-29 20:37:40 +03:00
});
return (
<div className={className} onClick={this.handleClick}>
2016-05-29 20:37:40 +03:00
<div className={`${theme.templateValue} ${theme.value}`}>
{this.props.template(selected)}
</div>
2016-10-11 23:12:12 +03:00
{this.props.label
2017-01-26 20:05:32 +03:00
? (
<label htmlFor={this.props.name} className={theme.label}>
2016-10-11 23:12:12 +03:00
{this.props.label}
{this.props.required ? <span className={theme.required}> * </span> : null}
</label>
2017-01-26 20:05:32 +03:00
) : null}
2016-05-29 20:37:40 +03:00
{this.props.error ? <span className={theme.error}>{this.props.error}</span> : null}
</div>
);
2015-09-19 18:42:57 +03:00
}
renderValue = (item, idx) => {
2016-05-29 20:37:40 +03:00
const { theme } = this.props;
const className = classnames({
[theme.selected]: item.value === this.props.value,
2017-01-26 20:05:32 +03:00
[theme.disabled]: item.disabled,
});
2016-05-29 20:37:40 +03:00
return (
2017-01-26 20:05:32 +03:00
<li
key={idx}
className={className}
onClick={!item.disabled && this.handleSelect.bind(this, item.value)}
>
2016-05-29 20:37:40 +03:00
{this.props.template ? this.props.template(item) : item.label}
</li>
);
};
2017-01-26 20:05:32 +03:00
render() {
2016-11-20 00:49:47 +03:00
const {
2017-01-26 20:05:32 +03:00
allowBlank, auto, required, onChange, onFocus, onBlur, // eslint-disable-line no-unused-vars
2016-11-20 00:49:47 +03:00
source, template, theme, ...others
} = this.props;
2016-05-29 20:37:40 +03:00
const selected = this.getSelectedItem();
const className = classnames(theme.dropdown, {
[theme.up]: this.state.up,
[theme.active]: this.state.active,
2016-10-11 23:12:12 +03:00
[theme.disabled]: this.props.disabled,
2017-01-26 20:05:32 +03:00
[theme.required]: this.props.required,
2016-05-29 20:37:40 +03:00
}, this.props.className);
return (
2016-11-20 00:49:47 +03:00
<div
className={className}
2017-01-26 20:05:32 +03:00
data-react-toolbox="dropdown"
2016-11-20 00:49:47 +03:00
onBlur={this.handleBlur}
onFocus={this.handleFocus}
>
2016-05-29 20:37:40 +03:00
<Input
{...others}
Migrate styles to PostCSS (#666) * Add postcss-next postcss-include and reporter * Add stylelint * Add CSS colors * Add CSS custom media queries * Use dashes for CSS colors * Add base CSS variables * Remove AppBar SASS dependency from spec page * Migrate AppBar style to PostCSS * Migrate Avatar style to PostCSS * Migrate Ripple style to PostCSS * Remove unneeded media CSS import in Avatar * Add shadows to CSS variables * Migrate Button style to PostCSS * Update webpack test config and linting from npm * Migrate Input style to PostCSS * Add missing input config variables for Dropdown and Autocomplete * Migrate Chip style to PostCSS * Migrate Autocomplete style to PostCSS * Migrate Dropdown style to PostCSS * Migrate animations to PostCSS * Migrate Card style to PostCSS * Migrate Checkbox style to PostCSS * Migrate DataPicker style to PostCSS * Migrate Dialog style to PostCSS * Migrate Drawer style to PostCSS * Add postcss-mixins and postcss-each * Migrate Layout style to PostCSS * Fix bug in button theme * Bugfix in avatar css * Add some missing nesting notations * Migrate Link style to PostCSS * Migrate List style to PostCSS * Migrate Menu style to PostCSS * Migrate Navigation style to PostCSS * Migrate Overlay style to PostCSS * Migrate ProgressBar style to PostCSS * Migrate Radio style to PostCSS * Migrate Slider style to PostCSS * Migrate Snackbar style to PostCSS * Migrate Switch style to PostCSS * Migrate Table style to PostCSS * Migrate Tabs style to PostCSS * Migrate TimePicker to PostCSS * Migrate Tooltip styles to PostCSS * Update webpack config for testing and tests * Migrate commons to PostCSS * Remove sass from main project * Bye from docs to sass * Build with CSS * Remove unneded deps for sass in docs subproject * Fix tests * use 4p shadow in AppBar as spec indicates * Fixed typo in list/config.css * Fix tests * Fix linter errors * Latest build * Release 2.0.0-beta.0 * Remove sass lint * fixes old sass var in css config * Update linter * New Table implementation * Fix old sass var in list/config.css See da0c47041ecf2d8b118b06fba9f53cc4d7e1e79f. * Remove normalize.css from commons.css * Update dependencies * Latest build * Input ready to accept visible hint * Prepare slider and progress to be disabled * Render Snackbar using Portal * Refactor Dialog, Drawer and Overlay to be used in Layout * Add inner layer to AppBar * New layout * Use Layout in spec * Latest build * remove layout playground example * add smTablet and lgTablet to NavDrawer in Layout readme * add default prop className to Layout, NavDrawer and Sidebar * fix css linter errors in card and slider * Typings for Table component * Add missing Drawer identifier * Update lib build * Adds onQueryChange callback property to Autocomplete The onQueryChange callback is called when the value of the query changes in Autocomplete. It is called with the new query value. * Fix #966 * Fix #965 * Fixes #976 * Updated css-related dependencies Removed usages of `addDependencyTo` since it's deprecated and not needed anymore. See https://github.com/postcss/postcss-import#adddependencyto * Enabled HMR for styles by disabling extracting them to a CSS file * Remove lib from repo * Remove lib * Fixes #1021 * Build using Gulp * Export ThemeProvider from react-css-themr * Add CHANGELOG to release command * Release 2.0.0-beta.1 * Remove immutability helper * Update dependencies * Fix Tooltip trying to render after it's been unmounted * Fixes #1038 * Release 2.0.0-beta.2 * Add ramda, refactor utils and remove slide animation modules * Remove separate slide animations modules * Remove box-sizing reset and body rule * Remove commons.css * Release 2.0.0-beta.4 * Update dependencies * Fixes #1061 * More aggresive guard condition for multiple autocomplete * Fix typeof check in isBrowser function * Fixes an issue when specs are opened with browsers that do not support Object.entries(). * Import from ramda using the 'import XXX from "ramda/src/XXX"' pattern so that bundle sizes will be smaller for not including the whole ramda package. * Fix #1032 * Fix tests * Update yarn.lock * Fixes #1064
2017-01-05 04:42:18 +03:00
tabIndex="0"
2016-05-29 20:37:40 +03:00
className={theme.value}
onClick={this.handleClick}
2016-10-11 23:12:12 +03:00
required={this.props.required}
2016-05-29 20:37:40 +03:00
readOnly
2017-01-26 20:05:32 +03:00
ref={(node) => { this.inputNode = node && node.getWrappedInstance(); }}
2016-05-29 20:37:40 +03:00
type={template && selected ? 'hidden' : null}
theme={theme}
themeNamespace="input"
value={selected && selected.label ? selected.label : ''}
2016-05-29 20:37:40 +03:00
/>
2017-01-26 20:05:32 +03:00
{template && selected ? this.renderTemplateValue(selected) : null}
<ul className={theme.values}>
{source.map(this.renderValue)}
2016-05-29 20:37:40 +03:00
</ul>
</div>
);
}
}
2015-10-09 16:55:00 +03:00
2016-05-29 20:37:40 +03:00
return Dropdown;
};
2015-10-22 02:31:17 +03:00
2016-05-29 20:37:40 +03:00
const Dropdown = factory(InjectInput);
export default themr(DROPDOWN)(Dropdown);
export { factory as dropdownFactory };
export { Dropdown };