Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(switch): add type generics #973

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions packages/docs/components/Switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,34 @@ title: Switch

### Props

| Prop name | Description | Type | Values | Default |
| ------------------ | -------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| autocomplete | Same as native autocomplete options to use in HTML5 validation | string | - | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;autocomplete: "off"<br>}</code> |
| disabled | Same as native disabled | boolean | - | <code style='white-space: nowrap; padding: 0;'>false</code> |
| falseValue | Overrides the returned value when it's not checked | string\|number\|boolean | - | <code style='white-space: nowrap; padding: 0;'>false</code> |
| label | Input label, unnecessary when default slot is used | string | - | |
| name | Name attribute on native checkbox | string | - | |
| nativeValue | Same as native value | string\|number\|boolean | - | |
| override | Override existing theme classes completely | boolean | - | |
| passiveVariant | Color of the switch when is passive | string | `primary`, `info`, `success`, `warning`, `danger`, `and any other custom color` | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;passiveVariant: undefined<br>}</code> |
| position | Label position | string | - | <code style='white-space: nowrap; padding: 0;'>"right"</code> |
| required | Same as native required | boolean | - | <code style='white-space: nowrap; padding: 0;'>false</code> |
| rounded | Rounded style | boolean | - | <code style='white-space: nowrap; padding: 0;'>true</code> |
| size | Size of the control | string | `small`, `medium`, `large` | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;size: undefined<br>}</code> |
| trueValue | Overrides the returned value when it's checked | string\|number\|boolean | - | <code style='white-space: nowrap; padding: 0;'>true</code> |
| useHtml5Validation | Enable html 5 native validation | boolean | - | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>{<br>&nbsp;&nbsp;useHtml5Validation: true<br>}</code> |
| v-model | | string\|number\|boolean | - | |
| variant | Color of the control | string | `primary`, `info`, `success`, `warning`, `danger`, `and any other custom color` | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;variant: undefined<br>}</code> |
| Prop name | Description | Type | Values | Default |
| ------------------ | -------------------------------------------------------------- | ------------------------------- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| autocomplete | Same as native autocomplete options to use in HTML5 validation | string | - | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;autocomplete: "off"<br>}</code> |
| disabled | Same as native disabled | boolean | - | <code style='white-space: nowrap; padding: 0;'>false</code> |
| falseValue | Overrides the returned value when it's not checked | string\|number\|boolean\|object | - | <code style='white-space: nowrap; padding: 0;'>false</code> |
| label | Input label, unnecessary when default slot is used | string | - | |
| name | Name attribute on native checkbox | string | - | |
| nativeValue | Same as native value | string\|number\|boolean\|object | - | |
| override | Override existing theme classes completely | boolean | - | |
| passiveVariant | Color of the switch when is passive | string | `primary`, `info`, `success`, `warning`, `danger`, `and any other custom color` | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;passiveVariant: undefined<br>}</code> |
| position | Label position | string | - | <code style='white-space: nowrap; padding: 0;'>"right"</code> |
| required | Same as native required | boolean | - | <code style='white-space: nowrap; padding: 0;'>false</code> |
| rounded | Rounded style | boolean | - | <code style='white-space: nowrap; padding: 0;'>true</code> |
| size | Size of the control | string | `small`, `medium`, `large` | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;size: undefined<br>}</code> |
| trueValue | Overrides the returned value when it's checked | string\|number\|boolean\|object | - | <code style='white-space: nowrap; padding: 0;'>true</code> |
| useHtml5Validation | Enable html 5 native validation | boolean | - | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>{<br>&nbsp;&nbsp;useHtml5Validation: true<br>}</code> |
| v-model | | string\|number\|boolean\|object | - | |
| variant | Color of the control | string | `primary`, `info`, `success`, `warning`, `danger`, `and any other custom color` | <div><small>From <b>config</b>:</small></div><code style='white-space: nowrap; padding: 0;'>switch: {<br>&nbsp;&nbsp;variant: undefined<br>}</code> |

### Events

| Event name | Properties | Description |
| ----------------- | -------------------------------------------------------------------------------------- | ------------------------------- |
| update:modelValue | **value** `string \| number \| boolean` - updated modelValue prop | modelValue prop two-way binding |
| input | **value** `string, number, boolean` - input value<br/>**event** `Event` - native event | on input change event |
| focus | **event** `Event` - native event | on input focus event |
| blur | **event** `Event` - native event | on input blur event |
| invalid | **event** `Event` - native event | on input invalid event |
| Event name | Properties | Description |
| ----------------- | ---------------------------------------------------------------------------------------------- | ------------------------------- |
| update:modelValue | **value** `string, number, boolean, object` - updated modelValue prop | modelValue prop two-way binding |
| input | **value** `string, number, boolean, object` - input value<br/>**event** `Event` - native event | on input change event |
| focus | **event** `Event` - native event | on input focus event |
| blur | **event** `Event` - native event | on input blur event |
| invalid | **event** `Event` - native event | on input invalid event |

### Slots

Expand Down
54 changes: 39 additions & 15 deletions packages/oruga/src/components/switch/Switch.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup lang="ts">
<script setup lang="ts" generic="T extends String | Number | Boolean | Object">
import { computed, ref, type PropType } from "vue";

import { getOption } from "@/utils/config";
Expand All @@ -21,8 +21,14 @@ defineOptions({
const props = defineProps({
/** Override existing theme classes completely */
override: { type: Boolean, default: undefined },
/** @model */
modelValue: { type: [String, Number, Boolean], default: undefined },
/**
* @type string|number|boolean|object
* @model
*/
modelValue: {
type: [String, Number, Boolean, Object] as PropType<T>,
default: undefined,
},
/**
* Color of the control
* @values primary, info, success, warning, danger, and any other custom color
Expand All @@ -49,18 +55,36 @@ const props = defineProps({
},
/** Input label, unnecessary when default slot is used */
label: { type: String, default: undefined },
/** Same as native value */
nativeValue: { type: [String, Number, Boolean], default: undefined },
/**
* Same as native value
* @type string|number|boolean|object
*/
nativeValue: {
type: [String, Number, Boolean, Object] as PropType<T>,
default: undefined,
},
/** Same as native disabled */
disabled: { type: Boolean, default: false },
/** Same as native required */
required: { type: Boolean, default: false },
/** Name attribute on native checkbox */
name: { type: String, default: undefined },
/** Overrides the returned value when it's checked */
trueValue: { type: [String, Number, Boolean], default: true },
/** Overrides the returned value when it's not checked */
falseValue: { type: [String, Number, Boolean], default: false },
/**
* Overrides the returned value when it's checked
* @type string|number|boolean|object
*/
trueValue: {
type: [String, Number, Boolean, Object] as PropType<T>,
default: true,
},
/**
* Overrides the returned value when it's not checked
* @type string|number|boolean|object
*/
falseValue: {
type: [String, Number, Boolean, Object] as PropType<T>,
default: false,
},
/** Rounded style */
rounded: { type: Boolean, default: true },
/** Label position */
Expand Down Expand Up @@ -146,15 +170,15 @@ const props = defineProps({
const emits = defineEmits<{
/**
* modelValue prop two-way binding
* @param value {string | number | boolean} updated modelValue prop
* @param value {string, number, boolean, object} updated modelValue prop
*/
(e: "update:modelValue", value: string | number | boolean): void;
(e: "update:modelValue", value: T): void;
/**
* on input change event
* @param value {string, number, boolean} input value
* @param value {string, number, boolean, object} input value
* @param event {Event} native event
*/
(e: "input", value: string | number | boolean, event: Event): void;
(e: "input", value: T, event: Event): void;
/**
* on input focus event
* @param event {Event} native event
Expand All @@ -181,7 +205,7 @@ const { onBlur, onFocus, onInvalid, setFocus } = useInputHandler(
props,
);

const vmodel = defineModel<string | number | boolean>({ default: undefined });
const vmodel = defineModel<T>({ default: undefined });

const isChecked = computed(
() =>
Expand Down Expand Up @@ -251,7 +275,7 @@ const labelClasses = defineClasses(["labelClass", "o-switch__label"]);
// --- Expose Public Functionalities ---

/** expose functionalities for programmatic usage */
defineExpose({ focus: setFocus, value: vmodel.value });
defineExpose({ focus: setFocus, value: vmodel });
</script>

<template>
Expand Down
Loading