From bb3bdee0c127ffbfb181a79d8f7333107a0a4721 Mon Sep 17 00:00:00 2001 From: Loic Teixeira Date: Sun, 30 Apr 2017 18:22:59 +1200 Subject: [PATCH] Linter 2 spaces --- .../client/components/AutoComplete.js | 124 +-- .../client/components/BaseChooser.js | 326 ++++---- .../client/components/Buttons.js | 58 +- .../client/components/ModelChooser.js | 74 +- .../client/components/ModelPicker.js | 773 +++++++++--------- .../client/components/RemoteModelChooser.js | 96 +-- .../client/sources/ModelSource.js | 146 ++-- .../client/sources/RemoteModelSource.js | 170 ++-- wagtailmodelchoosers/client/utils.js | 20 +- .../client/wagtailmodelchoosers.js | 24 +- .../client/wagtailmodelchoosers.test.js | 26 +- 11 files changed, 918 insertions(+), 919 deletions(-) diff --git a/wagtailmodelchoosers/client/components/AutoComplete.js b/wagtailmodelchoosers/client/components/AutoComplete.js index cb5f972..693c2aa 100644 --- a/wagtailmodelchoosers/client/components/AutoComplete.js +++ b/wagtailmodelchoosers/client/components/AutoComplete.js @@ -5,84 +5,84 @@ const getSuggestionValue = suggestion => suggestion.name; const renderSuggestion = () => null; const defaultProps = { - filter: '', + filter: '', }; const propTypes = { - onLoadSuggestions: PropTypes.func.isRequired, - onLoadStart: PropTypes.func.isRequired, - onChange: PropTypes.func.isRequired, - endpoint: PropTypes.string.isRequired, - filter: PropTypes.string, + onLoadSuggestions: PropTypes.func.isRequired, + onLoadStart: PropTypes.func.isRequired, + onChange: PropTypes.func.isRequired, + endpoint: PropTypes.string.isRequired, + filter: PropTypes.string, }; class AutoComplete extends Component { - constructor(props) { - super(props); + constructor(props) { + super(props); - this.state = { - value: '', - suggestions: [], - }; + this.state = { + value: '', + suggestions: [], + }; - this.loadSuggestions = this.loadSuggestions.bind(this); - this.onSuggestionsUpdateRequested = this.onSuggestionsUpdateRequested.bind(this); - this.onChange = this.onChange.bind(this); - } + this.loadSuggestions = this.loadSuggestions.bind(this); + this.onSuggestionsUpdateRequested = this.onSuggestionsUpdateRequested.bind(this); + this.onChange = this.onChange.bind(this); + } - loadSuggestions(suggestionValue) { - const { filter, endpoint, onLoadSuggestions } = this.props; - const url = `${endpoint}/?search=${suggestionValue}${filter}`; + loadSuggestions(suggestionValue) { + const { filter, endpoint, onLoadSuggestions } = this.props; + const url = `${endpoint}/?search=${suggestionValue}${filter}`; - fetch(url, { - credentials: 'same-origin', - }) - .then(res => res.json()) - .then(json => { - this.setState({ - suggestions: json.results, - loading: false, - }, () => { - onLoadSuggestions(json.results); - }); + fetch(url, { + credentials: 'same-origin', + }) + .then(res => res.json()) + .then(json => { + this.setState({ + suggestions: json.results, + loading: false, + }, () => { + onLoadSuggestions(json.results); }); - } + }); + } - onSuggestionsUpdateRequested({ value }) { - const { onLoadStart } = this.props; - onLoadStart(); - this.loadSuggestions(value); - } + onSuggestionsUpdateRequested({ value }) { + const { onLoadStart } = this.props; + onLoadStart(); + this.loadSuggestions(value); + } - onChange(event, { newValue }) { - const { onChange } = this.props; + onChange(event, { newValue }) { + const { onChange } = this.props; - this.setState({ - value: newValue, - }, () => { - onChange(newValue); - }); - } + this.setState({ + value: newValue, + }, () => { + onChange(newValue); + }); + } - render() { - const { value, suggestions } = this.state; + render() { + const { value, suggestions } = this.state; - return ( -
- -
- ); - } + return ( +
+ +
+ ); + } } AutoComplete.defaultProps = defaultProps; diff --git a/wagtailmodelchoosers/client/components/BaseChooser.js b/wagtailmodelchoosers/client/components/BaseChooser.js index 27225d2..ceddf67 100644 --- a/wagtailmodelchoosers/client/components/BaseChooser.js +++ b/wagtailmodelchoosers/client/components/BaseChooser.js @@ -5,193 +5,193 @@ import ModelPicker from './ModelPicker'; import { tr } from '../utils'; const STR = { - choose: 'Choose', - choose_again: 'Choose another', - clear: 'Clear choice', + choose: 'Choose', + choose_again: 'Choose another', + clear: 'Clear choice', }; const defaultProps = { - display: 'title', - filters: [], - pk_name: 'uuid', - translations: {}, + display: 'title', + filters: [], + pk_name: 'uuid', + translations: {}, }; const propTypes = { - initialValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired, - updateInputValue: PropTypes.func.isRequired, - label: PropTypes.string.isRequired, - initial_display_value: PropTypes.string.isRequired, - endpoint: PropTypes.string.isRequired, - value: PropTypes.any, - required: PropTypes.bool.isRequired, - display: PropTypes.oneOfType([PropTypes.string, PropTypes.array]).isRequired, - list_display: PropTypes.array.isRequired, - filters: PropTypes.array, - pk_name: PropTypes.string, - translations: PropTypes.object, + initialValue: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired, + updateInputValue: PropTypes.func.isRequired, + label: PropTypes.string.isRequired, + initial_display_value: PropTypes.string.isRequired, + endpoint: PropTypes.string.isRequired, + value: PropTypes.any, + required: PropTypes.bool.isRequired, + display: PropTypes.oneOfType([PropTypes.string, PropTypes.array]).isRequired, + list_display: PropTypes.array.isRequired, + filters: PropTypes.array, + pk_name: PropTypes.string, + translations: PropTypes.object, }; class BaseChooser extends React.Component { - constructor(props) { - super(props); + constructor(props) { + super(props); - const { display, initialValue, initial_display_value: initialDisplayValue } = this.props; + const { display, initialValue, initial_display_value: initialDisplayValue } = this.props; - // If `initialValue` is an object (i.e. the item), use it directly, - // otherwise create a new object and use the `initialValue` for the ID. - const hasInitialObject = initialValue !== null && typeof initialValue === 'object'; - const selectedItem = hasInitialObject ? initialValue : {}; - const selectedId = hasInitialObject ? this.getItemPk(selectedItem) : initialValue; + // If `initialValue` is an object (i.e. the item), use it directly, + // otherwise create a new object and use the `initialValue` for the ID. + const hasInitialObject = initialValue !== null && typeof initialValue === 'object'; + const selectedItem = hasInitialObject ? initialValue : {}; + const selectedId = hasInitialObject ? this.getItemPk(selectedItem) : initialValue; - // Ensure the item has the required key for `display`. - const displayKey = Array.isArray(display) ? display[0] : display; - if (!(displayKey in selectedItem)) { - selectedItem[displayKey] = initialDisplayValue; - } - - this.state = { - pickerVisible: false, - selectedId: selectedId, - selectedItem: selectedItem, - initialUrl: null, - }; - - this.showPicker = this.showPicker.bind(this); - this.onClose = this.onClose.bind(this); - this.onSelect = this.onSelect.bind(this); - this.getItemPk = this.getItemPk.bind(this); - this.getItemPreview = this.getItemPreview.bind(this); - this.isOptional = this.isOptional.bind(this); - this.getChooseButtons = this.getChooseButtons.bind(this); - this.clearPicker = this.clearPicker.bind(this); - } - - showPicker() { - this.setState({ - pickerVisible: true, - }); - } - - onClose() { - this.setState({ - pickerVisible: false, - }); + // Ensure the item has the required key for `display`. + const displayKey = Array.isArray(display) ? display[0] : display; + if (!(displayKey in selectedItem)) { + selectedItem[displayKey] = initialDisplayValue; } - onSelect(id, item, url) { - this.setState({ - selectedId: id, - selectedItem: item, - pickerVisible: false, - initialUrl: url, - }, () => { - this.props.updateInputValue(item); - }); + this.state = { + pickerVisible: false, + selectedId: selectedId, + selectedItem: selectedItem, + initialUrl: null, + }; + + this.showPicker = this.showPicker.bind(this); + this.onClose = this.onClose.bind(this); + this.onSelect = this.onSelect.bind(this); + this.getItemPk = this.getItemPk.bind(this); + this.getItemPreview = this.getItemPreview.bind(this); + this.isOptional = this.isOptional.bind(this); + this.getChooseButtons = this.getChooseButtons.bind(this); + this.clearPicker = this.clearPicker.bind(this); + } + + showPicker() { + this.setState({ + pickerVisible: true, + }); + } + + onClose() { + this.setState({ + pickerVisible: false, + }); + } + + onSelect(id, item, url) { + this.setState({ + selectedId: id, + selectedItem: item, + pickerVisible: false, + initialUrl: url, + }, () => { + this.props.updateInputValue(item); + }); + } + + getItemPk(item) { + const { pk_name: pkName } = this.props; + + return !!item ? item[pkName] : null; + } + + getItemPreview() { + const { display } = this.props; + const { selectedItem } = this.state; + + if (!selectedItem) { + return ''; } - getItemPk(item) { - const { pk_name: pkName } = this.props; - - return !!item ? item[pkName] : null; - } - - getItemPreview() { - const { display } = this.props; - const { selectedItem } = this.state; - - if (!selectedItem) { - return ''; + // Return first non-empty field if `display` is an Array. + if (Array.isArray(display)) { + for (const fieldName of display) { + if (fieldName in selectedItem && selectedItem[fieldName]) { + return selectedItem[fieldName]; } - - // Return first non-empty field if `display` is an Array. - if (Array.isArray(display)) { - for (const fieldName of display) { - if (fieldName in selectedItem && selectedItem[fieldName]) { - return selectedItem[fieldName]; - } - } - } - - // Return the `display` field if available. - if (display in selectedItem && selectedItem[display]) { - return selectedItem[display]; - } - - // Return the object PK as default. - return this.getItemPk(selectedItem); + } } - isOptional() { - const { required } = this.props; - return !required; + // Return the `display` field if available. + if (display in selectedItem && selectedItem[display]) { + return selectedItem[display]; } - getChooseButtons() { - const { translations } = this.props; - const { selectedId } = this.state; - - if (!selectedId) { - return ( - - ); - } - - return ( - - - {this.isOptional() ? ( - - ) : null} - - ); + // Return the object PK as default. + return this.getItemPk(selectedItem); + } + + isOptional() { + const { required } = this.props; + return !required; + } + + getChooseButtons() { + const { translations } = this.props; + const { selectedId } = this.state; + + if (!selectedId) { + return ( + + ); } - clearPicker(e) { - e.preventDefault(); - - this.setState({ - selectedId: null, - selectedItem: null, - pickerVisible: false, - initialUrl: null, - }, () => { - this.props.updateInputValue(null); - }); - } - - render() { - const { pickerVisible, initialUrl } = this.state; - - return ( -
-
- - {this.getItemPreview()} - - {this.getChooseButtons()} -
- {pickerVisible ? ( - - ) : null} -
- ); - } + return ( + + + {this.isOptional() ? ( + + ) : null} + + ); + } + + clearPicker(e) { + e.preventDefault(); + + this.setState({ + selectedId: null, + selectedItem: null, + pickerVisible: false, + initialUrl: null, + }, () => { + this.props.updateInputValue(null); + }); + } + + render() { + const { pickerVisible, initialUrl } = this.state; + + return ( +
+
+ + {this.getItemPreview()} + + {this.getChooseButtons()} +
+ {pickerVisible ? ( + + ) : null} +
+ ); + } } BaseChooser.defaultProps = defaultProps; diff --git a/wagtailmodelchoosers/client/components/Buttons.js b/wagtailmodelchoosers/client/components/Buttons.js index b550d90..ca8fb5b 100644 --- a/wagtailmodelchoosers/client/components/Buttons.js +++ b/wagtailmodelchoosers/client/components/Buttons.js @@ -2,59 +2,59 @@ import PropTypes from 'prop-types'; import React from 'react'; const Button = ({ isActive, classes, label, onClick }) => { - const buttonClasses = classes.slice(); + const buttonClasses = classes.slice(); - buttonClasses.push('button'); - if (!isActive) { - buttonClasses.push('button--disabled'); - } + buttonClasses.push('button'); + if (!isActive) { + buttonClasses.push('button--disabled'); + } - return ( - - ); + return ( + + ); }; Button.defaultProps = { - isActive: true, - classes: [], + isActive: true, + classes: [], }; Button.propTypes = { - isActive: PropTypes.bool, - classes: PropTypes.array, - label: PropTypes.string.isRequired, - onClick: PropTypes.func.isRequired, + isActive: PropTypes.bool, + classes: PropTypes.array, + label: PropTypes.string.isRequired, + onClick: PropTypes.func.isRequired, }; const CloseButton = ({ onClick }) => { - const classes = ['close', 'icon', 'text-replace', 'icon-cross']; + const classes = ['close', 'icon', 'text-replace', 'icon-cross']; - return