Skip to content

Commit

Permalink
Allow MultipleChoice component to accept new options (#538)
Browse files Browse the repository at this point in the history
* Allow multiplechoice comp to switch options

* Fix object comparison

* Revert storybook test

* Bump version

* Clean up

* fix lint

* Fix static method
  • Loading branch information
kaytcat authored Jun 17, 2019
1 parent 59092af commit 01a6fde
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nukleus",
"version": "15.0.0",
"version": "15.0.1",
"description": "Shared components repo for kununu projects",
"main": "dist/components/index.js",
"repository": {
Expand Down
22 changes: 19 additions & 3 deletions src/components/MultipleChoice/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import styles from './index.scss';

export default class MultipleChoice extends React.Component {
static propTypes = {
choices: PropTypes.arrayOf(PropTypes.shape({
choices: PropTypes.arrayOf(PropTypes.shape({ // eslint-disable-line react/no-unused-prop-types
id: PropTypes.string,
isChecked: PropTypes.bool,
label: PropTypes.string,
Expand All @@ -31,7 +31,7 @@ export default class MultipleChoice extends React.Component {
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
options: PropTypes.arrayOf(PropTypes.shape({
options: PropTypes.arrayOf(PropTypes.shape({ // eslint-disable-line react/no-unused-prop-types
id: PropTypes.string,
isChecked: PropTypes.bool,
label: PropTypes.string,
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class MultipleChoice extends React.Component {
};

state = {
choices: (this.props.options.length && this.props.options) || this.props.choices, // eslint-disable-line react/destructuring-assignment
choices: MultipleChoice.getCorrectChoiceValues(this.props),
showError: false,
};

Expand All @@ -76,12 +76,24 @@ export default class MultipleChoice extends React.Component {

componentWillReceiveProps (nextProps) {
if (nextProps.error) this.showError();
const newChoiceValues = MultipleChoice.getCorrectChoiceValues(nextProps);
const oldChoiceValues = MultipleChoice.getCorrectChoiceValues(this.props);

const {query} = this.props;
const {choices} = this.state;

// If new options props are passed to the component
// We replace the current options with the new ones.
if (JSON.stringify(newChoiceValues) !== JSON.stringify(oldChoiceValues)) {
this.setState({
choices: newChoiceValues,
});
}

// We do not yet control the state when the query does not change,
// for instance when updates are triggered by tweaking with other components.
if (nextProps.query === query) return;

if (!nextProps.query[nextProps.name]) {
this.updateValue(choices, 'unchecked');
} else {
Expand Down Expand Up @@ -111,6 +123,10 @@ export default class MultipleChoice extends React.Component {
return classNames;
}

static getCorrectChoiceValues ({options, choices}) {
return (options.length && options) || choices;
}

getChoicesToUpdate (newChoices) {
const {choices} = this.state;

Expand Down

0 comments on commit 01a6fde

Please sign in to comment.