-
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.
feat(admin): add useToggleInputContext composable (#223)
- Loading branch information
1 parent
2f72250
commit c0e1f59
Showing
17 changed files
with
112 additions
and
65 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
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,43 +1,50 @@ | ||
<template> | ||
<label | ||
:for="id" | ||
:for="toggleId" | ||
class="cursor-pointer flex items-center"> | ||
<input | ||
:id="id" | ||
v-model="model" | ||
class="invisible" | ||
type="hidden" /> | ||
|
||
<div | ||
class="relative" | ||
v-test="AdminComponent.ToggleInput" | ||
:class="{ | ||
'opacity-50': element.isDisabled || element.isSuspended || element.isReadOnly, | ||
}"> | ||
}" | ||
class="relative"> | ||
<input | ||
:id="id" | ||
v-model="model" | ||
type="checkbox" | ||
class="sr-only" | ||
:id="toggleId" | ||
v-model="toggleModel" | ||
:disabled="element.isDisabled || element.isSuspended || element.isReadOnly" | ||
:readonly="element.isReadOnly" /> | ||
:readonly="element.isReadOnly" | ||
class="sr-only" | ||
type="checkbox" /> | ||
|
||
<div | ||
class="bg-gray-600 block h-8 rounded-full transition-colors w-14" | ||
:class="{ | ||
'bg-green-400 dark:bg-green-600': model, | ||
'bg-gray-600': !model, | ||
}" /> | ||
'bg-green-400 dark:bg-green-600': toggleModel, | ||
'bg-gray-600': !toggleModel, | ||
}" | ||
class="bg-gray-600 block h-8 rounded-full transition-colors w-14" /> | ||
|
||
<div | ||
class="absolute bg-white h-6 left-1 rounded-full top-1 transition w-6" | ||
:class="{ | ||
'translate-x-6': model, | ||
'translate-x-0': !model, | ||
}" /> | ||
'translate-x-6': toggleModel, | ||
'translate-x-0': !toggleModel, | ||
}" | ||
class="absolute bg-white h-6 left-1 rounded-full top-1 transition w-6" /> | ||
</div> | ||
</label> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import {type ToggleInputEmits, type ToggleInputProps, useElementContext} from '@myparcel-pdk/admin'; | ||
import {AdminComponent, type ToggleInputEmits, type ToggleInputProps, useToggleInputContext} from '@myparcel-pdk/admin'; | ||
// eslint-disable-next-line vue/no-unused-properties | ||
const props = defineProps<ToggleInputProps>(); | ||
const emit = defineEmits<ToggleInputEmits>(); | ||
const {id, model} = useElementContext(props, emit); | ||
const {id, model, toggleId, toggleModel} = useToggleInputContext(props, emit); | ||
</script> |
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
27 changes: 17 additions & 10 deletions
27
apps/admin-preset-default/src/components/DefaultToggleInput.vue
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,34 +1,41 @@ | ||
<template> | ||
<label v-test="[AdminComponent.ToggleInput, element]"> | ||
<div> | ||
<input | ||
:id="id" | ||
v-model="model" | ||
:name="id" | ||
:disabled="element.isDisabled || element.isSuspended || element.isReadOnly" | ||
:readonly="element.isReadOnly" | ||
:value="true" | ||
type="checkbox" /> | ||
type="hidden" /> | ||
|
||
<label :for="id"> | ||
{{ translate(`toggle_${model ? 'yes' : 'no'}`) }} | ||
<label v-test="AdminComponent.ToggleInput"> | ||
<input | ||
:id="toggleId" | ||
v-model="toggleModel" | ||
:disabled="element.isDisabled || element.isSuspended || element.isReadOnly" | ||
:readonly="element.isReadOnly" | ||
:value="true" | ||
type="checkbox" /> | ||
|
||
<label :for="toggleId"> | ||
{{ translate(`toggle_${toggleModel ? 'yes' : 'no'}`) }} | ||
</label> | ||
</label> | ||
</label> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { | ||
AdminComponent, | ||
type ToggleInputEmits, | ||
type ToggleInputProps, | ||
useElementContext, | ||
useLanguage, | ||
useToggleInputContext, | ||
} from '@myparcel-pdk/admin'; | ||
// eslint-disable-next-line vue/no-unused-properties | ||
const props = defineProps<ToggleInputProps>(); | ||
const emit = defineEmits<ToggleInputEmits>(); | ||
const {id, model} = useElementContext(props, emit); | ||
const {id, model, toggleId, toggleModel} = useToggleInputContext(props, emit); | ||
const {translate} = useLanguage(); | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {ref, type Ref, watch, type WritableComputedRef} from 'vue'; | ||
import {get} from '@vueuse/core'; | ||
import {booleanToTriState, triStateToBoolean} from '../utils'; | ||
import {type ToggleInputEmits, type ToggleInputModelValue, type ToggleInputProps} from '../types'; | ||
import {useElementContext} from './useElementContext'; | ||
|
||
interface ToggleInputContext { | ||
id: string; | ||
model: WritableComputedRef<ToggleInputModelValue>; | ||
toggleId: string; | ||
toggleModel: Ref<boolean>; | ||
} | ||
|
||
type UseToggleInputContext = (props: ToggleInputProps, emit: ToggleInputEmits) => ToggleInputContext; | ||
|
||
export const useToggleInputContext: UseToggleInputContext = (props, emit) => { | ||
const toggleModel = ref<boolean>(triStateToBoolean(get(props.modelValue))); | ||
const {id, model} = useElementContext<ToggleInputModelValue>(props, emit); | ||
|
||
// When the toggle is changed, the model is updated to 1/0 | ||
watch(toggleModel, (toggle) => { | ||
model.value = booleanToTriState(toggle); | ||
}); | ||
|
||
// When the model is changed manually, like from another element, the toggle is updated | ||
watch(model, (model) => { | ||
toggleModel.value = triStateToBoolean(model); | ||
}); | ||
|
||
return { | ||
id, | ||
model, | ||
toggleId: `${id}__toggle`, | ||
toggleModel, | ||
}; | ||
}; |
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
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,13 @@ | ||
import {describe, expect, it} from 'vitest'; | ||
import {TriState} from '@myparcel-pdk/common'; | ||
import {booleanToTriState} from './booleanToTriState'; | ||
|
||
describe('booleanToTriState', () => { | ||
it.each([ | ||
[true, TriState.On], | ||
[false, TriState.Off], | ||
[undefined, TriState.Off], | ||
])('should return %s for %s', (value, expected) => { | ||
expect(booleanToTriState(value)).toEqual(expected); | ||
}); | ||
}); |
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,5 @@ | ||
import {TriState} from '@myparcel-pdk/common'; | ||
|
||
export const booleanToTriState = (value?: boolean): TriState.On | TriState.Off => { | ||
return value ? TriState.On : TriState.Off; | ||
}; |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.