-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from daleal/master
Release 0.3.0
- Loading branch information
Showing
14 changed files
with
203 additions
and
52 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,41 @@ | ||
import { defineComponent, ref } from 'vue'; | ||
import { useCheckbox } from '@/composables/checkbox'; | ||
import { useRender } from '@/utils/render'; | ||
|
||
export const LCheckbox = defineComponent({ | ||
name: 'LCheckbox', | ||
inheritAttrs: false, | ||
props: { | ||
modelValue: { | ||
type: Boolean, | ||
default: () => false, | ||
}, | ||
}, | ||
emits: { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
'update:modelValue': (value: boolean) => true, | ||
}, | ||
setup(props, { attrs, emit }) { | ||
const content = ref(props.modelValue); | ||
|
||
useCheckbox(content); | ||
|
||
const onInput = (event: Event) => { | ||
content.value = (event.target as HTMLInputElement).checked; | ||
emit('update:modelValue', (event.target as HTMLInputElement).checked); | ||
}; | ||
|
||
useRender(() => ( | ||
<> | ||
<input | ||
value={content.value} | ||
onInput={onInput} | ||
type="checkbox" | ||
{ ...attrs } | ||
/> | ||
</> | ||
)); | ||
}, | ||
}); | ||
|
||
export type LCheckbox = InstanceType<typeof LCheckbox> |
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 @@ | ||
export { LCheckbox } from './LCheckbox'; |
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,35 @@ | ||
import { computed, defineComponent } from 'vue'; | ||
import { createCheckboxGroup } from '@/composables/checkboxGroup'; | ||
import { useValidation, makeValidationProps } from '@/composables/validation'; | ||
import { useRender } from '@/utils/render'; | ||
|
||
export const LCheckboxGroup = defineComponent({ | ||
name: 'LCheckboxGroup', | ||
inheritAttrs: false, | ||
props: { | ||
...makeValidationProps<Array<boolean>>(), | ||
}, | ||
setup(props, { slots }) { | ||
const checkboxGroup = createCheckboxGroup(); | ||
|
||
const items = computed(() => checkboxGroup.items.value.map((content) => content.value)); | ||
|
||
const validation = useValidation<Array<boolean>>(props, items); | ||
|
||
useRender(() => ( | ||
<> | ||
<> | ||
{ slots.default?.() } | ||
</> | ||
{ validation.renderError.value && <p>{ validation.error.value }</p> } | ||
</> | ||
)); | ||
|
||
return { | ||
valid: validation.valid, | ||
error: validation.error, | ||
}; | ||
}, | ||
}); | ||
|
||
export type LCheckboxGroup = InstanceType<typeof LCheckboxGroup>; |
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 @@ | ||
export { LCheckboxGroup } from './LCheckboxGroup'; |
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,3 +1,5 @@ | ||
export * from './LCheckbox'; | ||
export * from './LCheckboxGroup'; | ||
export * from './LForm'; | ||
export * from './LInput'; | ||
export * from './LTextarea'; |
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,19 @@ | ||
import { onBeforeMount, onBeforeUnmount } from 'vue'; | ||
import { useCheckboxGroup } from '@/composables/checkboxGroup'; | ||
import { getUniqueId } from '@/utils/uniqueId'; | ||
|
||
// Types | ||
import type { Ref } from 'vue'; | ||
|
||
export const useCheckbox = (content: Ref<boolean>) => { | ||
const checkboxGroup = useCheckboxGroup(); | ||
const uid = getUniqueId(); | ||
|
||
onBeforeMount(() => { | ||
checkboxGroup?.register(uid, content); | ||
}); | ||
|
||
onBeforeUnmount(() => { | ||
checkboxGroup?.unregister(uid); | ||
}); | ||
}; |
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,44 @@ | ||
import { | ||
computed, inject, provide, shallowRef, | ||
} from 'vue'; | ||
|
||
// Types | ||
import type { InjectionKey, Ref } from 'vue'; | ||
|
||
type IdType = number; | ||
|
||
interface Checkbox { | ||
id: IdType, | ||
value: Ref<boolean>, | ||
} | ||
|
||
export interface CheckboxGroupProvide { | ||
register: ( | ||
id: IdType, | ||
value: Ref<boolean>, | ||
) => void, | ||
unregister: ( | ||
id: IdType, | ||
) => void, | ||
} | ||
|
||
export const CheckboxGroupKey: InjectionKey<CheckboxGroupProvide> = Symbol.for('loveform:checkbox-group'); | ||
|
||
export const createCheckboxGroup = () => { | ||
const items = shallowRef<Array<Checkbox>>([]); | ||
|
||
const values = computed(() => items.value.map((checkbox) => checkbox.value)); | ||
|
||
provide(CheckboxGroupKey, { | ||
register: (id: IdType, value: Ref<boolean>) => { | ||
items.value = [...items.value, { id, value }]; | ||
}, | ||
unregister: (id: IdType) => { | ||
items.value = items.value.filter((item) => item.id !== id); | ||
}, | ||
}); | ||
|
||
return { items: values }; | ||
}; | ||
|
||
export const useCheckboxGroup = () => inject(CheckboxGroupKey, null); |
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
Oops, something went wrong.