alaska-field-select
Advanced tools
Comparing version 0.13.1 to 0.13.2
{ | ||
"name": "alaska-field-select", | ||
"version": "0.13.1", | ||
"version": "0.13.2", | ||
"description": "Alaska select field", | ||
@@ -15,5 +15,5 @@ "keywords": [ | ||
"dependencies": { | ||
"alaska-field-checkbox": "^0.13.1", | ||
"alaska-field-checkbox": "^0.13.2", | ||
"react-select": "^1.2.1" | ||
} | ||
} |
@@ -21,2 +21,38 @@ // @flow | ||
function processOptions( | ||
options?: Alaska$SelectField$option[], | ||
optionsMap: { | ||
[value: string]: Alaska$SelectField$option, | ||
}, | ||
value: Alaska$SelectField$value | ||
): Alaska$SelectField$option[] { | ||
let valueMap = {}; | ||
if (Array.isArray(value)) { | ||
_.forEach(value, (v) => { | ||
valueMap[String(v)] = true; | ||
}); | ||
} else { | ||
valueMap[String(value)] = true; | ||
} | ||
let res = _.map(options || [], (opt) => { | ||
if (typeof opt.style === 'string') { | ||
opt = _.omit(opt, 'style'); | ||
} | ||
let vKey = String(opt.value); | ||
optionsMap[vKey] = opt; | ||
if (valueMap[vKey]) { | ||
delete valueMap[vKey]; | ||
} | ||
return opt; | ||
}); | ||
if (_.size(valueMap)) { | ||
_.keys(valueMap).forEach((v) => { | ||
if (optionsMap[v]) { | ||
res.push(optionsMap[v]); | ||
} | ||
}); | ||
} | ||
return res; | ||
} | ||
export default class Select extends React.Component<Props, State> { | ||
@@ -31,11 +67,8 @@ static contextTypes = { | ||
super(props); | ||
let optionsMap = {}; | ||
let options = processOptions(props.options, optionsMap, props.value); | ||
this.state = { | ||
options: this.processOptions(props.options), | ||
optionsMap: {}, | ||
options, | ||
optionsMap | ||
}; | ||
if (props.options) { | ||
for (let o of props.options) { | ||
this.state.optionsMap[String(o.value)] = o; | ||
} | ||
} | ||
this.state.value = this.processValue(props.value); | ||
@@ -54,19 +87,12 @@ this._cache = {}; | ||
let state = {}; | ||
if (props.options !== this.props.options) { | ||
state.options = this.processOptions(props.options); | ||
state.optionsMap = {}; | ||
if (props.options) { | ||
for (let o of props.options) { | ||
state.optionsMap[String(o.value)] = o; | ||
} | ||
if (props.options !== this.props.options || props.value !== this.props.value) { | ||
state.optionsMap = this.state.optionsMap; | ||
let options = props.options; | ||
if (props.multi && props.loadOptions) { | ||
options = this.state.options; | ||
} | ||
} | ||
if (props.value !== undefined) { | ||
state.options = processOptions(options, state.optionsMap, props.value); | ||
state.value = this.processValue(props.value); | ||
} | ||
this.setState(state, () => { | ||
if (state.optionsMap) { | ||
this.setState({ value: this.processValue(props.value) }); | ||
} | ||
}); | ||
this.setState(state); | ||
} | ||
@@ -78,11 +104,2 @@ | ||
processOptions(options?: Alaska$SelectField$option[]): Alaska$SelectField$option[] { | ||
return _.map(options || [], (opt) => { | ||
if (typeof opt.style === 'string') { | ||
opt = _.omit(opt, 'style'); | ||
} | ||
return opt; | ||
}); | ||
} | ||
processValue = (value: any) => { | ||
@@ -139,4 +156,10 @@ let optionsMap = this.state.optionsMap; | ||
handleSearchChange = (search: string) => { | ||
if (this._cache[search]) { | ||
this.setState({ options: this._cache[search] }); | ||
const { value } = this.props; | ||
let { optionsMap } = this.state; | ||
let cacheKey = 'c_' + (search || JSON.stringify(value)); | ||
if (this._cache[cacheKey]) { | ||
this.setState({ | ||
options: processOptions(this._cache[cacheKey], optionsMap, value), | ||
optionsMap | ||
}); | ||
return; | ||
@@ -147,9 +170,7 @@ } | ||
if (!error && res.options) { | ||
this._cache[search] = res.options; | ||
let options = this._cache[search]; | ||
let optionsMap = {}; | ||
options.forEach((o) => { | ||
optionsMap[o.value] = o; | ||
this._cache[cacheKey] = res.options; | ||
this.setState({ | ||
options: processOptions(this._cache[cacheKey], optionsMap, value), | ||
optionsMap | ||
}); | ||
this.setState({ options, optionsMap }); | ||
} | ||
@@ -156,0 +177,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23853
779