Skip to content

Commit

Permalink
Refactor #1920 - For MultiSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Mar 30, 2021
1 parent 7a8a290 commit 689b377
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/components/multiselect/MultiSelect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import TooltipOptions from '../tooltip/TooltipOptions';

interface MultiSelectProps {
id?: string;
inputRef?: any;
name?: string;
value?: any;
options?: any[];
Expand Down
28 changes: 23 additions & 5 deletions src/components/multiselect/MultiSelect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { classNames } from '../utils/ClassNames';
import React, { Component, createRef } from 'react';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { classNames } from '../utils/ClassNames';
import { tip } from '../tooltip/Tooltip';
import DomHandler from '../utils/DomHandler';
import FilterUtils from '../utils/FilterUtils';
Expand All @@ -16,6 +16,7 @@ export class MultiSelect extends Component {

static defaultProps = {
id: null,
inputRef: null,
name: null,
value: null,
options: null,
Expand Down Expand Up @@ -64,6 +65,7 @@ export class MultiSelect extends Component {

static propTypes = {
id: PropTypes.string,
inputRef: PropTypes.any,
name: PropTypes.string,
value: PropTypes.any,
options: PropTypes.array,
Expand Down Expand Up @@ -134,7 +136,8 @@ export class MultiSelect extends Component {
this.onPanelClick = this.onPanelClick.bind(this);

this.id = this.props.id || UniqueComponentId();
this.overlayRef = React.createRef();
this.overlayRef = createRef();
this.inputRef = createRef(this.props.inputRef);
}

onPanelClick(event) {
Expand Down Expand Up @@ -509,7 +512,22 @@ export class MultiSelect extends Component {
}
}

updateInputRef() {
let ref = this.props.inputRef;

if (ref) {
if (typeof ref === 'function') {
ref(this.inputRef.current);
}
else {
ref.current = this.inputRef.current;
}
}
}

componentDidMount() {
this.updateInputRef();

if (this.props.tooltip) {
this.renderTooltip();
}
Expand Down Expand Up @@ -646,7 +664,7 @@ export class MultiSelect extends Component {
}

checkValidity() {
return this.nativeSelect.checkValidity();
return this.inputRef.current.checkValidity();
}

removeChip(event, item) {
Expand Down Expand Up @@ -788,7 +806,7 @@ export class MultiSelect extends Component {

return (
<div className="p-hidden-accessible p-multiselect-hidden-select">
<select ref={(el) => this.nativeSelect = el} required={this.props.required} name={this.props.name} tabIndex={-1} aria-hidden="true" multiple>
<select ref={this.inputRef} required={this.props.required} name={this.props.name} tabIndex={-1} aria-hidden="true" multiple>
{selectedOptions}
</select>
</div>
Expand Down

0 comments on commit 689b377

Please sign in to comment.