-
Notifications
You must be signed in to change notification settings - Fork 9
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
8 changed files
with
149 additions
and
8 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,50 @@ | ||
# Toggle | ||
|
||
## Examples | ||
|
||
Toggle | ||
|
||
```html :::demo | ||
<div class="text-center"> | ||
<dv-toggle /> | ||
</div> | ||
``` | ||
|
||
Toggle color | ||
|
||
```html :::demo | ||
<div class="text-center"> | ||
<dv-toggle checked /> | ||
<div class="h-2" /> | ||
<dv-toggle checked type="primary" /> | ||
<div class="h-2" /> | ||
<dv-toggle checked type="secondary" /> | ||
<div class="h-2" /> | ||
<dv-toggle checked type="accent" /> | ||
</div> | ||
``` | ||
|
||
Toggle size | ||
|
||
```html :::demo | ||
<div class="text-center"> | ||
<dv-toggle size="xs" /> | ||
<div class="h-2" /> | ||
<dv-toggle size="sm" /> | ||
<div class="h-2" /> | ||
<dv-toggle size="md" /> | ||
<div class="h-2" /> | ||
<dv-toggle size="lg" /> | ||
</div> | ||
``` | ||
|
||
## Toggle | ||
|
||
### Attributes | ||
|
||
| name | description | type | default | | ||
| -------- | ---------------------------------- | ----------------------------------- | ------- | | ||
| checked | toggle checked status | boolean | - | | ||
| onChange | toggle checked status change event | Function | - | | ||
| type | toogle color type | netural, primary, secondary, accent | netural | | ||
| size | toogle size | xs, sm, md, lg | - | |
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,7 @@ | ||
# Demo for development | ||
|
||
```html :::demo | ||
<div class="text-center"> </div> | ||
<div class="text-center"> | ||
<dv-toggle /> | ||
</div> | ||
``` |
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 { InputHTMLAttributes } from 'vue'; | ||
|
||
export type InputChangeEvent = Omit<Event, 'target'> & { | ||
target: HTMLInputElement; | ||
}; |
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,71 @@ | ||
import { InputChangeEvent } from '@/@types/dom'; | ||
import { componentV2 } from '@/shared/styled'; | ||
import { ExtractFromProps, IBrandColor, ISize } from '@/shared/types/common'; | ||
import { isUndefined } from '@/shared/utils'; | ||
import { computed, nextTick, PropType, reactive } from 'vue'; | ||
import styles from './style'; | ||
|
||
export const toggleProps = { | ||
checked: { | ||
type: Boolean, | ||
default: void 0, | ||
}, | ||
onChange: { | ||
type: Function as PropType<(e: InputChangeEvent) => void>, | ||
default: void 0, | ||
}, | ||
type: { | ||
type: String as PropType<IBrandColor>, | ||
default: 'netural', | ||
}, | ||
size: { | ||
type: String as PropType<ISize>, | ||
default: 'md', | ||
}, | ||
}; | ||
|
||
export type IToggleProps = ExtractFromProps<typeof toggleProps>; | ||
|
||
export const Toggle = componentV2<IToggleProps>( | ||
{ | ||
name: 'Toggle', | ||
props: toggleProps, | ||
setup: (props) => { | ||
const state = reactive({ | ||
checked: props.checked, | ||
}); | ||
|
||
const finalVal = computed(() => | ||
isUndefined(props.checked) ? state.checked : props.checked, | ||
); | ||
|
||
const cls = computed(() => [ | ||
'dv-toggle toggle', | ||
{ | ||
[`toggle-${props.type}`]: props.type, | ||
[`toggle-${props.size}`]: props.size, | ||
}, | ||
]); | ||
|
||
return () => { | ||
return ( | ||
<input | ||
type="checkbox" | ||
class={cls.value} | ||
checked={finalVal.value} | ||
onChange={(e: InputChangeEvent) => { | ||
const newVal = !finalVal.value; | ||
state.checked = newVal; | ||
props.onChange?.(e); | ||
nextTick(() => { | ||
// force sync with finalVal | ||
e.target.checked = finalVal.value; | ||
}); | ||
}} | ||
/> | ||
); | ||
}; | ||
}, | ||
}, | ||
styles, | ||
); |
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 s1 from '@styles/components/unstyled/toggle.css'; | ||
import s2 from '@styles/components/styled/toggle.css'; | ||
import s3 from '@styles/utilities/unstyled/toggle.css'; | ||
|
||
export default [s1, s2, s3]; |