alaska-field-select
Advanced tools
Comparing version 0.11.22 to 0.12.0
49
index.js
@@ -1,18 +0,17 @@ | ||
// @flow | ||
'use strict'; | ||
import { Field } from 'alaska'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
export default class SelectField extends Field { | ||
static plain = String; | ||
static viewOptions = ['options', 'translate', 'checkbox', 'switch', 'multi']; | ||
static defaultOptions = { | ||
cell: 'SelectFieldCell', | ||
view: 'SelectFieldView', | ||
filter: 'SelectFieldFilter' | ||
}; | ||
var _lodash = require('lodash'); | ||
number: boolean; | ||
boolean: boolean; | ||
dataType: Function; | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _alaska = require('alaska'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
class SelectField extends _alaska.Field { | ||
initSchema() { | ||
@@ -32,13 +31,3 @@ let schema = this._schema; | ||
}; | ||
[ | ||
'get', | ||
'set', | ||
'default', | ||
'index', | ||
'unique', | ||
'text', | ||
'sparse', | ||
'required', | ||
'select' | ||
].forEach((key) => { | ||
['get', 'set', 'default', 'index', 'unique', 'text', 'sparse', 'required', 'select'].forEach(key => { | ||
if (this[key] !== undefined) { | ||
@@ -53,3 +42,3 @@ // $Flow | ||
createFilter(filter: Object): any|void { | ||
createFilter(filter) { | ||
let value = filter; | ||
@@ -68,3 +57,3 @@ let inverse = false; | ||
value = parseInt(value); | ||
if (isNaN(value)) return null; | ||
if (_lodash2.default.isNaN(value)) return null; | ||
return inverse ? { $ne: value } : value; | ||
@@ -85,1 +74,9 @@ } | ||
} | ||
exports.default = SelectField; | ||
SelectField.plain = String; | ||
SelectField.viewOptions = ['options', 'translate', 'checkbox', 'switch', 'multi']; | ||
SelectField.defaultOptions = { | ||
cell: 'SelectFieldCell', | ||
view: 'SelectFieldView', | ||
filter: 'SelectFieldFilter' | ||
}; |
{ | ||
"name": "alaska-field-select", | ||
"version": "0.11.22", | ||
"version": "0.12.0", | ||
"description": "Alaska select field", | ||
@@ -9,10 +9,11 @@ "keywords": [ | ||
], | ||
"alaska": "field", | ||
"main": "index.js", | ||
"repository": "https://github.com/maichong/alaska/tree/master/packages/alaska-field-select", | ||
"repository": "https://github.com/maichong/alaska/tree/master/src/alaska-field-select", | ||
"author": "Liang <liang@maichong.it> (https://github.com/liangxingchen)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"alaska-field-checkbox": "^0.11.22", | ||
"react-select": "^1.0.0-rc.5" | ||
"alaska-field-checkbox": "^0.12.0", | ||
"react-select": "^1.0.1" | ||
} | ||
} |
@@ -31,4 +31,22 @@ // @flow | ||
export default class Select extends React.Component { | ||
type Props = { | ||
allowCreate?: boolean, | ||
disabled?: boolean, | ||
multi?: boolean, | ||
renderValue?: Function, | ||
onChange: Function, | ||
loadOptions?: Function, | ||
value: any, | ||
options: Object[] | ||
}; | ||
type State = { | ||
options: Alaska$SelectField$option[]; | ||
optionsMap: { | ||
[value: string]: Alaska$SelectField$option; | ||
}; | ||
value?: Alaska$SelectField$option | Alaska$SelectField$option[]; | ||
}; | ||
export default class Select extends React.Component<Props, State> { | ||
static contextTypes = { | ||
@@ -38,24 +56,5 @@ t: PropTypes.func | ||
props: { | ||
allowCreate?: boolean, | ||
disabled?: boolean, | ||
multi?: boolean, | ||
renderValue?: Function, | ||
onChange: Function, | ||
loadOptions?: Function, | ||
value: any, | ||
options: Object[] | ||
}; | ||
state: { | ||
options: Alaska$SelectField$option[]; | ||
optionsMap: { | ||
[value:string]:Alaska$SelectField$option; | ||
}; | ||
value?:Alaska$SelectField$option | Alaska$SelectField$option[]; | ||
}; | ||
_cache: Object; | ||
constructor(props: Object) { | ||
constructor(props: Props) { | ||
super(props); | ||
@@ -82,3 +81,3 @@ this.state = { | ||
componentWillReceiveProps(props: Object) { | ||
componentWillReceiveProps(props: Props) { | ||
let state = {}; | ||
@@ -136,3 +135,3 @@ if (props.options !== this.props.options) { | ||
handleChange = (value: Alaska$SelectField$option|Alaska$SelectField$option[]) => { | ||
handleChange = (value: Alaska$SelectField$option | Alaska$SelectField$option[]) => { | ||
let optionsMap = this.state.optionsMap; | ||
@@ -160,32 +159,2 @@ if (value) { | ||
renderValueWithRemove = (item: Alaska$SelectField$option) => { | ||
let me = this; | ||
function handleRemove() { | ||
let values = []; | ||
let value = me.state.value; | ||
if (value instanceof Array) { | ||
value.forEach((v) => { | ||
if (v.value != item.value) { | ||
values.push(v); | ||
} | ||
}); | ||
} | ||
me.setState({ value: values }); | ||
if (me.props.onChange) { | ||
me.props.onChange(values); | ||
} | ||
} | ||
return ( | ||
<div className="simple-value"> | ||
<span | ||
className="simple-value-remove" | ||
onClick={handleRemove} | ||
>x</span> | ||
<span>{item.label}</span> | ||
</div> | ||
); | ||
}; | ||
handleSearchChange = (search: string) => { | ||
@@ -222,2 +191,33 @@ if (this._cache[search]) { | ||
renderValueWithRemove = (item: Alaska$SelectField$option) => { | ||
let me = this; | ||
function handleRemove() { | ||
let values = []; | ||
let value = me.state.value; | ||
if (value instanceof Array) { | ||
value.forEach((v) => { | ||
if (v.value != item.value) { | ||
values.push(v); | ||
} | ||
}); | ||
} | ||
me.setState({ value: values }); | ||
if (me.props.onChange) { | ||
me.props.onChange(values); | ||
} | ||
} | ||
return ( | ||
<div className="simple-value"> | ||
<span | ||
className="simple-value-remove" | ||
onClick={handleRemove} | ||
>x | ||
</span> | ||
<span>{item.label}</span> | ||
</div> | ||
); | ||
}; | ||
render() { | ||
@@ -224,0 +224,0 @@ const { t } = this.context; |
@@ -10,17 +10,17 @@ // @flow | ||
export default class SelectCheckbox extends React.Component { | ||
props: { | ||
disabled?: boolean, | ||
multi?: boolean, | ||
onChange: Function, | ||
loadOptions?: Function, | ||
value: any, | ||
options: Object[] | ||
}; | ||
type Props = { | ||
disabled?: boolean, | ||
multi?: boolean, | ||
onChange: Function, | ||
loadOptions?: Function, | ||
value: any, | ||
options: Object[] | ||
}; | ||
state: { | ||
options:Alaska$SelectField$option[] | ||
}; | ||
type State = { | ||
options: Alaska$SelectField$option[] | ||
}; | ||
constructor(props: Object) { | ||
export default class SelectCheckbox extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
@@ -43,3 +43,3 @@ this.state = { | ||
componentWillReceiveProps(props: Object) { | ||
componentWillReceiveProps(props: Props) { | ||
this.setState({ | ||
@@ -55,3 +55,5 @@ options: props.options | ||
let optionsMap: Indexed = {}; | ||
_.forEach(options, (o) => (optionsMap[getOptionValue(o)] = o)); | ||
_.forEach(options, (o) => { | ||
optionsMap[getOptionValue(o)] = o; | ||
}); | ||
@@ -118,7 +120,8 @@ if (!multi) { | ||
onCheck={() => this.handleCheck(vid)} | ||
/>) | ||
/>); | ||
}) | ||
}</div> | ||
} | ||
</div> | ||
); | ||
} | ||
} |
@@ -8,4 +8,3 @@ // @flow | ||
export default class SelectFieldCell extends React.Component { | ||
export default class SelectFieldCell extends React.Component<Alaska$view$Field$Cell$Props> { | ||
static contextTypes = { | ||
@@ -15,9 +14,3 @@ t: PropTypes.func | ||
props: { | ||
value: any, | ||
field: Object, | ||
model: Object, | ||
}; | ||
shouldComponentUpdate(props: Object) { | ||
shouldComponentUpdate(props: Alaska$view$Field$Cell$Props) { | ||
/* eslint eqeqeq:0 */ | ||
@@ -28,3 +21,3 @@ return props.value != this.props.value; | ||
render() { | ||
const t = this.context.t; | ||
const { t } = this.context; | ||
let { field, value, model } = this.props; | ||
@@ -35,5 +28,7 @@ let el; | ||
if (field.multi) { | ||
el = []; | ||
let arr = []; | ||
let valueMap = {}; | ||
_.forEach(value, (v) => (valueMap[getOptionValue(v)] = true)); | ||
_.forEach(value, (v) => { | ||
valueMap[getOptionValue(v)] = true; | ||
}); | ||
_.forEach(field.options, (opt) => { | ||
@@ -45,4 +40,4 @@ if (valueMap[opt.value]) { | ||
} | ||
if (el.length) { | ||
el.push(' , '); | ||
if (arr.length) { | ||
arr.push(' , '); | ||
} | ||
@@ -53,5 +48,6 @@ let c; | ||
} | ||
el.push(<span className={c} key={opt.value}>{label}</span>); | ||
arr.push(<span className={c} key={opt.value}>{label}</span>); | ||
} | ||
}); | ||
el = arr; | ||
} else { | ||
@@ -58,0 +54,0 @@ let option = _.find(field.options, (opt) => opt.value === value); |
@@ -11,4 +11,9 @@ // @flow | ||
export default class SelectFieldFilter extends React.Component { | ||
type State = { | ||
value: any, | ||
inverse: boolean, | ||
error: boolean | ||
}; | ||
export default class SelectFieldFilter extends React.Component<Alaska$view$Field$Filter$Props, State> { | ||
static contextTypes = { | ||
@@ -18,17 +23,3 @@ t: PropTypes.func, | ||
props: { | ||
className: string, | ||
value: any, | ||
field: Object, | ||
onChange: Function, | ||
onClose: Function, | ||
}; | ||
state: { | ||
value: any, | ||
inverse: boolean, | ||
error: boolean | ||
}; | ||
constructor(props: Object) { | ||
constructor(props: Alaska$view$Field$Filter$Props) { | ||
super(props); | ||
@@ -48,3 +39,3 @@ let u = props.value || {}; | ||
t(opt: Alaska$SelectField$option) { | ||
const t = this.context.t; | ||
const { t } = this.context; | ||
if (this.props.field.translate === false || !t) { | ||
@@ -80,3 +71,3 @@ return opt; | ||
render() { | ||
const t = this.context.t; | ||
const { t } = this.context; | ||
let { className, field, onClose } = this.props; | ||
@@ -107,3 +98,4 @@ const { value, inverse, error } = this.state; | ||
onClick={this.handleInverse} | ||
>{t('inverse')}</a> | ||
>{t('inverse')} | ||
</a> | ||
</div> | ||
@@ -110,0 +102,0 @@ <a className="btn field-filter-close" onClick={onClose}><i className="fa fa-close" /></a> |
@@ -13,4 +13,7 @@ // @flow | ||
export default class SelectFieldView extends React.Component { | ||
type State = { | ||
options: Alaska$SelectField$option[] | ||
}; | ||
export default class SelectFieldView extends React.Component<Alaska$view$Field$View$Props, State> { | ||
static contextTypes = { | ||
@@ -20,25 +23,12 @@ t: PropTypes.func, | ||
props: { | ||
className: string, | ||
model: Object, | ||
field: Object, | ||
data: Object, | ||
errorText: string, | ||
disabled: boolean, | ||
value: any, | ||
onChange: Function, | ||
}; | ||
state = {}; | ||
componentWillMount() { | ||
this.setState({ | ||
options: this.filter(this.props.field.options, this.props.data) | ||
options: this.filter(this.props.field.options, this.props.record) | ||
}); | ||
} | ||
componentWillReceiveProps(nextProps: Object) { | ||
if (nextProps.field !== this.props.field || nextProps.data !== this.props.data) { | ||
componentWillReceiveProps(nextProps: Alaska$view$Field$View$Props) { | ||
if (nextProps.field !== this.props.field || nextProps.record !== this.props.record) { | ||
this.setState({ | ||
options: this.filter(nextProps.field.options, nextProps.data) | ||
options: this.filter(nextProps.field.options, nextProps.record) | ||
}); | ||
@@ -48,8 +38,8 @@ } | ||
shouldComponentUpdate(props: Object, state: Object) { | ||
return !shallowEqualWithout(props, this.props, 'data', 'onChange', 'model') || state !== this.state; | ||
shouldComponentUpdate(props: Alaska$view$Field$View$Props, state: State) { | ||
return !shallowEqualWithout(props, this.props, 'record', 'onChange', 'model') || state !== this.state; | ||
} | ||
t(opt: Alaska$SelectField$option) { | ||
const t = this.context.t; | ||
const { t } = this.context; | ||
if (this.props.field.translate === false || !t) { | ||
@@ -65,9 +55,9 @@ return opt; | ||
filter(options: Alaska$SelectField$option[], data: Object) { | ||
if (!options || !data || !options.length) { | ||
return options; | ||
filter(options?: Alaska$SelectField$option[], record: Object): Alaska$SelectField$option[] { | ||
if (!options || !record || !options.length) { | ||
return []; | ||
} | ||
let res = []; | ||
_.forEach(options, (opt) => { | ||
if (checkDepends(opt.depends, data)) { | ||
if (checkDepends(opt.depends, record)) { | ||
res.push(this.t(opt)); | ||
@@ -80,3 +70,5 @@ } | ||
render() { | ||
let { className, field, value, disabled, errorText, onChange } = this.props; | ||
let { | ||
className, field, value, disabled, errorText, onChange | ||
} = this.props; | ||
let View = Select; | ||
@@ -88,3 +80,4 @@ if (field.checkbox) { | ||
} | ||
if (field.multi) { | ||
let { help, multi } = field; | ||
if (multi) { | ||
if (!_.isArray(value)) { | ||
@@ -95,3 +88,2 @@ value = [value]; | ||
} | ||
let help = field.help; | ||
className += ' select-field'; | ||
@@ -108,3 +100,5 @@ if (errorText) { | ||
let valueMap = {}; | ||
_.forEach(value, (v) => (valueMap[getOptionValue(v)] = true)); | ||
_.forEach(value, (v) => { | ||
valueMap[getOptionValue(v)] = true; | ||
}); | ||
_.forEach(this.state.options, (opt) => { | ||
@@ -111,0 +105,0 @@ if (valueMap[String(opt.value)]) { |
@@ -7,18 +7,17 @@ // @flow | ||
export default class Switch extends React.Component { | ||
type Props = { | ||
disabled?: boolean, | ||
multi?: boolean, | ||
onChange: Function, | ||
loadOptions?: Function, | ||
value: any, | ||
options: Object[] | ||
}; | ||
props: { | ||
disabled?: boolean, | ||
multi?: boolean, | ||
onChange: Function, | ||
loadOptions?: Function, | ||
value: any, | ||
options: Object[] | ||
}; | ||
type State = { | ||
options: Alaska$SelectField$option[] | ||
}; | ||
state: { | ||
options:Alaska$SelectField$option[] | ||
}; | ||
constructor(props: Object) { | ||
export default class Switch extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
@@ -41,3 +40,3 @@ this.state = { | ||
componentWillReceiveProps(props: Object) { | ||
componentWillReceiveProps(props: Props) { | ||
this.setState({ | ||
@@ -53,3 +52,5 @@ options: props.options | ||
let optionsMap: Indexed = {}; | ||
_.forEach(options, (o) => (optionsMap[getOptionValue(o)] = o)); | ||
_.forEach(options, (o) => { | ||
optionsMap[getOptionValue(o)] = o; | ||
}); | ||
@@ -90,3 +91,5 @@ if (!multi) { | ||
if (multi) { | ||
_.forEach(value, (v) => (valueMap[getOptionValue(v)] = true)); | ||
_.forEach(value, (v) => { | ||
valueMap[getOptionValue(v)] = true; | ||
}); | ||
} else if (value !== undefined) { | ||
@@ -108,3 +111,3 @@ valueMap[getOptionValue(value)] = true; | ||
} | ||
return <div key={vid} className={cls} onClick={disabled?null:() => this.handleClick(vid)}>{o.label}</div>; | ||
return <div key={vid} className={cls} onClick={disabled ? null : () => this.handleClick(vid)}>{o.label}</div>; | ||
})} | ||
@@ -111,0 +114,0 @@ </div> |
Sorry, the diff of this file is not supported yet
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
23299
782
+ Addedalaska-field-checkbox@0.12.12(transitive)
- Removedalaska-field-checkbox@0.11.22(transitive)
Updatedreact-select@^1.0.1