Skip to content

Commit dab36ce

Browse files
dhruvduttwhilelucky
authored andcommitted
fix(Select/web): check select all if all options are checked by default (#194)
1 parent 23938f9 commit dab36ce

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

Diff for: src/Select/web/Select.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Select extends React.Component {
107107
...selectedOptions,
108108
selectedOption,
109109
];
110-
if (selectedOptions.length === options.length) {
110+
if (newSelectedOptions.length === options.length) {
111111
newSelectedOptions = [{
112112
label: 'Select all',
113113
value: 'selectAll',
@@ -128,7 +128,7 @@ class Select extends React.Component {
128128
}
129129

130130
getDefaultSelected = (props, context) => {
131-
const { name, defaultSelected } = props;
131+
const { name, defaultSelected, options } = props;
132132
const { formik } = context;
133133
let defaultSelectedOptions = [];
134134

@@ -137,14 +137,32 @@ class Select extends React.Component {
137137
} else if (defaultSelected === 'selectAll') {
138138
defaultSelectedOptions = this.makeOptions();
139139
} else if (defaultSelected != null) {
140-
defaultSelectedOptions = Array.isArray(defaultSelected)
141-
? defaultSelectedOptions.concat(defaultSelected.map(this.remakeOption))
142-
: defaultSelectedOptions.concat(this.remakeOption(defaultSelected));
140+
if (Array.isArray(defaultSelected)) {
141+
defaultSelectedOptions = defaultSelectedOptions
142+
.concat(defaultSelected.map(this.remakeOption));
143+
if (defaultSelectedOptions.length === options.length) {
144+
defaultSelectedOptions = [{
145+
label: 'Select all',
146+
value: 'selectAll',
147+
}].concat(defaultSelectedOptions);
148+
}
149+
} else {
150+
defaultSelectedOptions = defaultSelectedOptions.concat(this.remakeOption(defaultSelected));
151+
}
143152
} else if (formik && name && getIn(formik.values, name) != null) {
144153
const formikValues = getIn(formik.values, name);
145-
defaultSelectedOptions = Array.isArray(formikValues)
146-
? defaultSelectedOptions.concat(formikValues.map(this.remakeOption))
147-
: defaultSelectedOptions.concat(this.remakeOption(formikValues));
154+
if (Array.isArray(formikValues)) {
155+
defaultSelectedOptions = defaultSelectedOptions
156+
.concat(formikValues.map(this.remakeOption));
157+
if (defaultSelectedOptions.length === options.length) {
158+
defaultSelectedOptions = [{
159+
label: 'Select all',
160+
value: 'selectAll',
161+
}].concat(defaultSelectedOptions);
162+
}
163+
} else {
164+
defaultSelectedOptions = defaultSelectedOptions.concat(this.remakeOption(formikValues));
165+
}
148166
}
149167

150168
return defaultSelectedOptions;

0 commit comments

Comments
 (0)