Skip to content

Commit

Permalink
FIX Added the state changed checks and bubble up event - pending build
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlewisnz committed Oct 5, 2023
1 parent 635d13c commit c931056
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion client/src/components/TagField.js
Expand Up @@ -10,8 +10,16 @@ class TagField extends Component {
constructor(props) {
super(props);

this.selectComponentRef = React.createRef();

this.state = {
initalState: props.value ? props.value : [],
hasChanges: false,
};

if (!this.isControlled()) {
this.state = {
...this.state,
value: props.value,
};
}
Expand All @@ -22,6 +30,14 @@ class TagField extends Component {
this.fetchOptions = debounce(this.fetchOptions, 500);
}

componentDidUpdate(previousProps, previousState) {
if (previousState.hasChanges !== this.state.hasChanges) {
const element = this.selectComponentRef.current.select.wrapper;
const event = new Event('change', { bubbles: true });
element.dispatchEvent(event);
}
}

/**
* Get the options that should be shown to the user for this tagfield, optionally filtering by the
* given string input
Expand Down Expand Up @@ -50,6 +66,16 @@ class TagField extends Component {
* @param {string} value
*/
handleChange(value) {
this.setState({
hasChanges: false
});

if (JSON.stringify(this.state.initalState) !== JSON.stringify(value)) {
this.setState({
hasChanges: true
});
}

if (this.isControlled()) {
this.props.onChange(value);
return;
Expand Down Expand Up @@ -137,13 +163,16 @@ class TagField extends Component {
}
}

const changedClassName = this.state.hasChanges ? '' : 'no-change-track';

return (
<SelectComponent
{...passThroughAttributes}
onChange={this.handleChange}
onBlur={this.handleOnBlur}
inputProps={{ className: 'no-change-track' }}
{...optionAttributes}
className={changedClassName}
ref={this.selectComponentRef}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/legacy/entwine/TagField.js
Expand Up @@ -44,7 +44,7 @@ window.jQuery.entwine('ss', ($) => {
// clone the object (so we don't modify the original),
opts = $.extend({}, opts);
// modify it,
opts.ignoreFieldSelector += ', .ss-tag-field .Select :input';
opts.ignoreFieldSelector += ', .ss-tag-field .no-change-track :input';
// then set the clone as the value on this element
// (so next call to this method gets this same clone)
this.setChangeTrackerOptions(opts);
Expand Down

0 comments on commit c931056

Please sign in to comment.