From 7aab8b0ae3d6816cf861c09193c9e2e166e2d18a Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Wed, 4 Jan 2017 15:46:52 -0800 Subject: [PATCH] Simplifying the Fields (Controls) interface (#1868) * Simplifying the Field interface / logic * Moving ControlLabelWithTooltip where it belongs * Progress * Rename FieldClass->FieldType --- .../explorev2/components/CheckboxField.jsx | 6 +- .../explorev2/components/FieldSet.jsx | 75 +++-------- .../explorev2/components/SelectField.jsx | 14 +- .../explorev2/components/TextAreaField.jsx | 2 - .../explorev2/components/TextField.jsx | 5 - .../assets/javascripts/explorev2/main.css | 6 + .../javascripts/explorev2/stores/fields.js | 122 ++++++++++++------ 7 files changed, 111 insertions(+), 119 deletions(-) diff --git a/superset/assets/javascripts/explorev2/components/CheckboxField.jsx b/superset/assets/javascripts/explorev2/components/CheckboxField.jsx index fb9ce69db9797..98a3013bb0d4e 100644 --- a/superset/assets/javascripts/explorev2/components/CheckboxField.jsx +++ b/superset/assets/javascripts/explorev2/components/CheckboxField.jsx @@ -1,6 +1,5 @@ import React, { PropTypes } from 'react'; import { Checkbox } from 'react-bootstrap'; -import ControlLabelWithTooltip from './ControlLabelWithTooltip'; const propTypes = { name: PropTypes.string.isRequired, @@ -24,12 +23,9 @@ export default class CheckboxField extends React.Component { render() { return ( - - + /> ); } } diff --git a/superset/assets/javascripts/explorev2/components/FieldSet.jsx b/superset/assets/javascripts/explorev2/components/FieldSet.jsx index b155b6e8bd068..b2a11823aa2c2 100644 --- a/superset/assets/javascripts/explorev2/components/FieldSet.jsx +++ b/superset/assets/javascripts/explorev2/components/FieldSet.jsx @@ -3,7 +3,16 @@ import TextField from './TextField'; import CheckboxField from './CheckboxField'; import TextAreaField from './TextAreaField'; import SelectField from './SelectField'; -import { fieldTypes } from '../stores/fields'; + +import ControlLabelWithTooltip from './ControlLabelWithTooltip'; + +const fieldMap = { + TextField, + CheckboxField, + TextAreaField, + SelectField, +}; +const fieldTypes = Object.keys(fieldMap); const propTypes = { name: PropTypes.string.isRequired, @@ -18,68 +27,22 @@ const propTypes = { PropTypes.string, PropTypes.number, PropTypes.bool, - PropTypes.array]).isRequired, + PropTypes.array]), }; const defaultProps = { - choices: null, - description: null, - places: null, - validators: null, onChange: () => {}, }; -export default class FieldSet extends React.Component { - renderCheckBoxField() { - return ( - ); - } - - renderTextAreaField() { - return ( - ); - } - - renderSelectField(selectProps) { - return ( - ); - } - - renderTextField() { - return ( - ); - } - +export default class FieldSet extends React.PureComponent { render() { - const type = this.props.type; - const selectProps = { - SelectCustomMultiField: { multi: true, freeForm: true }, - SelectMultipleSortableField: { multi: true, freeForm: false }, - SelectField: { multi: false, freeForm: false }, - FreeFormSelectField: { multi: false, freeForm: true }, - }; - let field; - - if (type === 'CheckboxField') { - field = this.renderCheckBoxField(); - } else if (Object.keys(selectProps).includes(type)) { - field = this.renderSelectField(selectProps[type]); - } else if (['TextField', 'IntegerField'].includes(type)) { - field = this.renderTextField(); - } else if (type === 'TextAreaField') { - field = this.renderTextAreaField(); - } - - return field; + const FieldType = fieldMap[this.props.type]; + return ( +
+ + +
+ ); } } diff --git a/superset/assets/javascripts/explorev2/components/SelectField.jsx b/superset/assets/javascripts/explorev2/components/SelectField.jsx index b3514449ee563..f9686e06bd687 100644 --- a/superset/assets/javascripts/explorev2/components/SelectField.jsx +++ b/superset/assets/javascripts/explorev2/components/SelectField.jsx @@ -1,6 +1,4 @@ import React, { PropTypes } from 'react'; -import ControlLabelWithTooltip from './ControlLabelWithTooltip'; -import { slugify } from '../../modules/utils'; import Select, { Creatable } from 'react-select'; @@ -87,17 +85,7 @@ export default class SelectField extends React.Component { // Tab, comma or Enter will trigger a new option created for FreeFormSelect const selectWrap = this.props.freeForm ? () : (