-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: wyj <wb-wyj631405@antfin.com>
- Loading branch information
Showing
10 changed files
with
100 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,10 @@ | ||
import equal from 'fast-deep-equal'; | ||
import { ChecklistItemDefaultProps } from './props'; | ||
|
||
Component({ | ||
props: ChecklistItemDefaultProps, | ||
|
||
data: { | ||
checked: false | ||
}, | ||
|
||
didMount() { | ||
const checked = this.getChecked() | ||
this.setData({ | ||
checked | ||
}); | ||
}, | ||
|
||
didUpdate(prevProps) { | ||
const { value, item, multiple } = prevProps; | ||
if (equal(this.props.value, value) && | ||
equal(this.props.item, item) && | ||
equal(this.props.multiple, multiple)) return; | ||
const checked = this.getChecked() | ||
this.setData({ | ||
checked | ||
}); | ||
}, | ||
|
||
methods: { | ||
onChecklistItemClick() { | ||
const { checked } = this.data; | ||
const { onChange } = this.props; | ||
this.setData({ | ||
checked: !checked | ||
}); | ||
const value = this.getValues(!checked) | ||
onChange && onChange.call(this.props, value); | ||
}, | ||
|
||
getChecked() { | ||
const { multiple, item, value } = this.props; | ||
if (!multiple) { | ||
return value === item.value | ||
} | ||
let valueArr = [] | ||
if (!Array.isArray(value)) { | ||
valueArr = [value] | ||
} else { | ||
valueArr = value.slice() | ||
} | ||
return valueArr.indexOf(item.value) > -1 | ||
this.props.onChange(this.props.item); | ||
}, | ||
|
||
getValues(checked) { | ||
const { multiple, item, value } = this.props; | ||
if (!multiple) { | ||
return checked ? item.value : ''; | ||
} | ||
let valueArr = value | ||
if (!Array.isArray(value)) { | ||
valueArr = [value] | ||
} else { | ||
valueArr = value.slice() | ||
} | ||
if (checked && valueArr.indexOf(item.value) === -1) { | ||
return valueArr.concat([item.value]); | ||
} else { | ||
return valueArr.filter(v => v !== item.value); | ||
} | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,17 @@ | ||
import { IBaseProps } from '../../_base'; | ||
import { ChecklistItem } from '../props' | ||
import { ChecklistItem } from '../props'; | ||
|
||
/** | ||
* @description 可勾选列表单项 | ||
*/ | ||
|
||
interface IChecklistItemProps extends IBaseProps { | ||
/** | ||
* @description 是否支持多选 | ||
* @default false | ||
*/ | ||
multiple: boolean; | ||
|
||
|
||
item: ChecklistItem | ||
|
||
/** | ||
* @description 可勾选列表的值 | ||
*/ | ||
value: Array<string | number>| string| number; | ||
|
||
item: ChecklistItem; | ||
checked: boolean; | ||
/** | ||
* @description 当项选项值改变时触发 | ||
*/ | ||
onChange: (v: string | number) => any | ||
onChange: (item: ChecklistItem) => void; | ||
} | ||
|
||
export declare const ChecklistItemDefaultProps: Partial<IChecklistItemProps>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
export const ChecklistItemDefaultProps = { | ||
multiple: false, | ||
item: {} | ||
}; | ||
|
||
export const ChecklistItemDefaultProps = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function getChecked(value, values, multiple) { | ||
if (!multiple) { | ||
return value === values; | ||
} | ||
return (values || []).indexOf(value) > -1; | ||
} | ||
|
||
export default { getChecked }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,43 @@ | ||
import { ChecklistDefaultProps } from './props'; | ||
import controlled from '../mixins/controlled' | ||
import mixinValue from '../mixins/value'; | ||
import fmtEvent from '../_util/fmtEvent'; | ||
|
||
Component({ | ||
props: ChecklistDefaultProps, | ||
mixins: [controlled()], | ||
didMount() { | ||
const { multiple, value } = this.props | ||
if (multiple && !Array.isArray(value)) { | ||
this.setData({ | ||
cValue: [] | ||
}) | ||
} | ||
}, | ||
mixins: [mixinValue()], | ||
methods: { | ||
onChange(value) { | ||
const { multiple, options } = this.props | ||
let items = null | ||
if (multiple && Array.isArray(value)) { | ||
items = value.map(v => { | ||
return options.filter(o => o.value === v)?.[0] | ||
}) | ||
onChange(item) { | ||
const { multiple, options, onChange } = this.props; | ||
const value = item.value; | ||
if (multiple) { | ||
let currentValue = this.getValue() || []; | ||
if (currentValue.indexOf(value) > -1) { | ||
currentValue = currentValue.filter((v) => v !== value); | ||
} else { | ||
currentValue = [...currentValue, value]; | ||
} | ||
if (!this.isControlled()) { | ||
this.update(currentValue); | ||
} | ||
if (onChange) { | ||
onChange( | ||
currentValue, | ||
options.filter((v) => currentValue.indexOf(v.value) > -1) as any, | ||
fmtEvent(this.props) | ||
); | ||
} | ||
} else { | ||
items = options.filter(o => o.value === value)?.[0]; | ||
if (!this.isControlled()) { | ||
this.update(value); | ||
} | ||
if (onChange) { | ||
onChange( | ||
value, | ||
options.find((v) => v.value === value) as any, | ||
fmtEvent(this.props) | ||
); | ||
} | ||
} | ||
this.cOnChange(value, items, fmtEvent(this.props)); | ||
} | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export const ChecklistDefaultProps = { | ||
value: '', | ||
multiple: false, | ||
options: [] | ||
}; | ||
|