-
Notifications
You must be signed in to change notification settings - Fork 27
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
Showing
7 changed files
with
89 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script setup lang="ts"> | ||
import { useModelValue } from '@/use/useModelValue' | ||
import { computed, inject } from 'vue' | ||
import { RadioGroupInjectKey } from './types' | ||
defineOptions({ name: 'TRadio' }) | ||
const emit = defineEmits<{ 'update:checked': [boolean]; change: [boolean] }>() | ||
const slots = defineSlots<{ default?(_: {}): any }>() | ||
const props = defineProps({ | ||
value: { type: null, required: true }, | ||
name: String, | ||
disabled: Boolean, | ||
checked: { type: Boolean, default: undefined }, | ||
}) | ||
const group = inject(RadioGroupInjectKey, null) | ||
console.log(parent) | ||
const [innerChecked, setInnerChecked] = useModelValue(props, { | ||
defaultValue: group ? group.value.value === props.value : false, | ||
valuePropName: 'checked', | ||
onChange: (val: boolean) => { | ||
emit('change', val) | ||
group?.select(props.value) | ||
}, | ||
}) | ||
const checked = computed(() => (group ? group.value.value === props.value : innerChecked.value)) | ||
const onInput = (e: Event) => { | ||
const el = e.currentTarget as HTMLInputElement | ||
console.log(el.checked) | ||
setInnerChecked(el.checked) | ||
} | ||
</script> | ||
<template> | ||
<label :class="['relative inline-flex items-center', disabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer']"> | ||
<input | ||
class="form-input h-4 w-4 rounded-full border-gray-300 text-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500" | ||
:style="{ boxShadow: 'none', cursor: 'inherit' }" | ||
type="radio" | ||
:name="name" | ||
:disabled="disabled" | ||
:checked="checked" | ||
:onInput="onInput" | ||
/> | ||
<template v-if="slots.default"> | ||
<span class="relative ml-2 text-sm font-medium"> | ||
<slot /> | ||
</span> | ||
</template> | ||
</label> | ||
</template> |
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,25 @@ | ||
<script setup lang="ts"> | ||
import { provide } from 'vue' | ||
import { RadioGroupInjectKey } from './types' | ||
import { useModelValue } from '@/use/useModelValue' | ||
defineOptions({ name: 'RadioGroup' }) | ||
const emit = defineEmits<{ 'update:value': [unknown]; change: [unknown] }>() | ||
const props = defineProps({ name: String, value: null, disabled: Boolean }) | ||
const [modelValue, setModelValue] = useModelValue<unknown>(props, { | ||
onChange: val => { | ||
emit('change', val) | ||
}, | ||
}) | ||
provide(RadioGroupInjectKey, { | ||
value: modelValue, | ||
select: (val: unknown) => { | ||
setModelValue(val) | ||
}, | ||
}) | ||
</script> | ||
<template> | ||
<slot /> | ||
</template> |
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,2 @@ | ||
export { default as Radio } from './Radio.vue' | ||
export { default as RadioGroup } from './RadioGroup.vue' |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { InjectionKey, ComputedRef } from 'vue' | ||
|
||
export const RadioGroupInjectKey: InjectionKey<{ | ||
value: ComputedRef<unknown> | ||
select: (val: unknown) => void | ||
}> = Symbol('CheckboxGroupInjectKey') |
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