-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DiamondYuan
committed
Jan 18, 2024
1 parent
07034fd
commit 1b5dd14
Showing
11 changed files
with
170 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,5 @@ Component({ | |
onChange(value, e) { | ||
this.emit('onChange', 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,10 +1,13 @@ | ||
import { ChecklistItemDefaultProps } from './props'; | ||
import { useEvent } from 'functional-mini/component'; | ||
import { mountComponent } from '../../_util/component'; | ||
import { useComponentEvent } from '../../_util/hooks/useComponentEvent'; | ||
import { ChecklistItemDefaultProps, IChecklistItemProps } from './props'; | ||
|
||
Component({ | ||
props: ChecklistItemDefaultProps, | ||
methods: { | ||
onChecklistItemClick() { | ||
this.props.onChange(this.props.item); | ||
}, | ||
}, | ||
}); | ||
const CheckListItem = (props: IChecklistItemProps) => { | ||
const { triggerEvent } = useComponentEvent(props); | ||
useEvent('onChecklistItemClick', () => { | ||
triggerEvent('change', props.item); | ||
}); | ||
}; | ||
|
||
mountComponent(CheckListItem, 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 |
---|---|---|
@@ -1,54 +1,61 @@ | ||
import { ChecklistDefaultProps } from './props'; | ||
import mixinValue from '../mixins/value'; | ||
import fmtEvent from '../_util/fmtEvent'; | ||
import '../_util/assert-component2'; | ||
import { mountComponent } from '../_util/component'; | ||
import { useComponentEvent } from '../_util/hooks/useComponentEvent'; | ||
import { useHandleCustomEvent } from '../_util/hooks/useHandleCustomEvent'; | ||
import { useMixState } from '../_util/hooks/useMixState'; | ||
import { | ||
ChecklistFunctionalProps, | ||
ChecklistItem, | ||
IChecklistProps, | ||
} from './props'; | ||
|
||
Component({ | ||
props: ChecklistDefaultProps, | ||
mixins: [ | ||
mixinValue({ | ||
transformValue(val) { | ||
const value = val || []; | ||
return { | ||
needUpdate: true, | ||
value, | ||
}; | ||
}, | ||
}), | ||
], | ||
methods: { | ||
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) | ||
); | ||
} | ||
const Checkbox = (props: IChecklistProps) => { | ||
const [state, { isControlled, update }] = useMixState< | ||
Array<string | number> | string | number | ||
>(props.defaultValue, { | ||
value: props.value, | ||
postState(val) { | ||
const value = val || []; | ||
return { | ||
valid: true, | ||
value, | ||
}; | ||
}, | ||
}); | ||
|
||
const { triggerEventValues } = useComponentEvent(props); | ||
useHandleCustomEvent<ChecklistItem>('onChange', (item) => { | ||
const { multiple, options } = props; | ||
const value = item.value; | ||
if (multiple) { | ||
let currentValue = state as Array<string | number>; | ||
if (currentValue.indexOf(value) > -1) { | ||
currentValue = currentValue.filter((v) => v !== value); | ||
} else { | ||
if (!this.isControlled()) { | ||
this.update(value); | ||
} | ||
if (onChange) { | ||
onChange( | ||
value, | ||
options.find((v) => v.value === value) as any, | ||
fmtEvent(this.props) | ||
); | ||
} | ||
currentValue = [...currentValue, value]; | ||
} | ||
if (!isControlled) { | ||
update(currentValue); | ||
} | ||
triggerEventValues('change', [ | ||
currentValue, | ||
options.filter((v) => currentValue.indexOf(v.value) > -1), | ||
]); | ||
} else { | ||
if (!isControlled) { | ||
update(value); | ||
} | ||
triggerEventValues('change', [ | ||
value, | ||
options.find((v) => v.value === value), | ||
]); | ||
} | ||
}); | ||
|
||
return { | ||
mixin: { | ||
value: state, | ||
}, | ||
}, | ||
}); | ||
}; | ||
}; | ||
|
||
mountComponent(Checkbox, ChecklistFunctionalProps); |
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,10 +1,13 @@ | ||
import { ChecklistItemDefaultProps } from './props'; | ||
import { useEvent } from 'functional-mini/component'; | ||
import { mountComponent } from '../../_util/component'; | ||
import { useComponentEvent } from '../../_util/hooks/useComponentEvent'; | ||
import { ChecklistItemDefaultProps, IChecklistItemProps } from './props'; | ||
|
||
Component({ | ||
props: ChecklistItemDefaultProps, | ||
methods: { | ||
onChecklistItemClick() { | ||
this.props.onChange(this.props.item); | ||
}, | ||
}, | ||
}); | ||
const CheckListItem = (props: IChecklistItemProps) => { | ||
const { triggerEvent } = useComponentEvent(props); | ||
useEvent('onChecklistItemClick', () => { | ||
triggerEvent('change', props.item); | ||
}); | ||
}; | ||
|
||
mountComponent(CheckListItem, 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 |
---|---|---|
@@ -1,54 +1,61 @@ | ||
import { ChecklistDefaultProps } from './props'; | ||
import mixinValue from '../mixins/value'; | ||
import fmtEvent from '../_util/fmtEvent'; | ||
import '../_util/assert-component2'; | ||
import { mountComponent } from '../_util/component'; | ||
import { useComponentEvent } from '../_util/hooks/useComponentEvent'; | ||
import { useHandleCustomEvent } from '../_util/hooks/useHandleCustomEvent'; | ||
import { useMixState } from '../_util/hooks/useMixState'; | ||
import { | ||
ChecklistFunctionalProps, | ||
ChecklistItem, | ||
IChecklistProps, | ||
} from './props'; | ||
|
||
Component({ | ||
props: ChecklistDefaultProps, | ||
mixins: [ | ||
mixinValue({ | ||
transformValue(val) { | ||
const value = val || []; | ||
return { | ||
needUpdate: true, | ||
value, | ||
}; | ||
}, | ||
}), | ||
], | ||
methods: { | ||
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) | ||
); | ||
} | ||
const Checkbox = (props: IChecklistProps) => { | ||
const [state, { isControlled, update }] = useMixState< | ||
Array<string | number> | string | number | ||
>(props.defaultValue, { | ||
value: props.value, | ||
postState(val) { | ||
const value = val || []; | ||
return { | ||
valid: true, | ||
value, | ||
}; | ||
}, | ||
}); | ||
|
||
const { triggerEventValues } = useComponentEvent(props); | ||
useHandleCustomEvent<ChecklistItem>('onChange', (item) => { | ||
const { multiple, options } = props; | ||
const value = item.value; | ||
if (multiple) { | ||
let currentValue = state as Array<string | number>; | ||
if (currentValue.indexOf(value) > -1) { | ||
currentValue = currentValue.filter((v) => v !== value); | ||
} else { | ||
if (!this.isControlled()) { | ||
this.update(value); | ||
} | ||
if (onChange) { | ||
onChange( | ||
value, | ||
options.find((v) => v.value === value) as any, | ||
fmtEvent(this.props) | ||
); | ||
} | ||
currentValue = [...currentValue, value]; | ||
} | ||
if (!isControlled) { | ||
update(currentValue); | ||
} | ||
triggerEventValues('change', [ | ||
currentValue, | ||
options.filter((v) => currentValue.indexOf(v.value) > -1), | ||
]); | ||
} else { | ||
if (!isControlled) { | ||
update(value); | ||
} | ||
triggerEventValues('change', [ | ||
value, | ||
options.find((v) => v.value === value), | ||
]); | ||
} | ||
}); | ||
|
||
return { | ||
mixin: { | ||
value: state, | ||
}, | ||
}, | ||
}); | ||
}; | ||
}; | ||
|
||
mountComponent(Checkbox, ChecklistFunctionalProps); |
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