Skip to content

Commit

Permalink
Refactor #5612 - InputSwitch / ToggleSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
tugcekucukoglu committed Apr 18, 2024
1 parent 1428cc1 commit f240b95
Show file tree
Hide file tree
Showing 16 changed files with 384 additions and 263 deletions.
179 changes: 13 additions & 166 deletions components/lib/inputswitch/InputSwitch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,207 +2,54 @@
*
* InputSwitch is used to select a boolean value.
*
* [Live Demo](https://www.primevue.org/inputswitch/)
* [Live Demo](https://www.primevue.org/toggleswitch/)
*
* @module inputswitch
*
*/
import { ComponentHooks } from '../basecomponent';
import { PassThroughOptions } from '../passthrough';
import { ClassComponent, DesignToken, GlobalComponentConstructor, PassThrough } from '../ts-helpers';

export declare type InputSwitchPassThroughOptionType = InputSwitchPassThroughAttributes | ((options: InputSwitchPassThroughMethodOptions) => InputSwitchPassThroughAttributes | string) | string | null | undefined;

import * as ToggleSwitch from '../toggleswitch';
import { ClassComponent, GlobalComponentConstructor } from '../ts-helpers';
/**
* Custom passthrough(pt) option method.
*/
export interface InputSwitchPassThroughMethodOptions {
/**
* Defines instance.
*/
instance: any;
/**
* Defines valid properties.
*/
props: InputSwitchProps;
/**
* Defines current options.
*/
context: InputSwitchContext;
/**
* Defines valid attributes.
*/
attrs: any;
/**
* Defines parent options.
*/
parent: any;
/**
* Defines passthrough(pt) options in global config.
*/
global: object | undefined;
}
export interface InputSwitchPassThroughMethodOptions extends ToggleSwitch.ToggleSwitchPassThroughMethodOptions {}

/**
* Custom passthrough(pt) options.
* @see {@link InputSwitchProps.pt}
*/
export interface InputSwitchPassThroughOptions {
/**
* Used to pass attributes to the root's DOM element.
*/
root?: InputSwitchPassThroughOptionType;
/**
* Used to pass attributes to the input's DOM element.
*/
input?: InputSwitchPassThroughOptionType;
/**
* Used to pass attributes to the slider's DOM element.
*/
slider?: InputSwitchPassThroughOptionType;
/**
* Used to manage all lifecycle hooks.
* @see {@link BaseComponent.ComponentHooks}
*/
hooks?: ComponentHooks;
}
export interface InputSwitchPassThroughOptions extends ToggleSwitch.ToggleSwitchPassThroughOptions {}

/**
* Custom passthrough attributes for each DOM elements
*/
export interface InputSwitchPassThroughAttributes {
[key: string]: any;
}
export interface InputSwitchPassThroughAttributes extends ToggleSwitch.ToggleSwitchPassThroughAttributes {}

/**
* Defines valid properties in InputSwitch component.
*/
export interface InputSwitchProps {
/**
* Specifies whether a inputswitch should be checked or not.
* @defaultValue false
*/
modelValue?: boolean | string | undefined;
/**
* Value in checked state.
* @defaultValue true
*/
trueValue?: any;
/**
* Value in unchecked state.
* @defaultValue false
*/
falseValue?: any;
/**
* When present, it specifies that the component should have invalid state style.
* @defaultValue false
*/
invalid?: boolean | undefined;
/**
* When present, it specifies that the component should be disabled.
* @defaultValue false
*/
disabled?: boolean | undefined;
/**
* When present, it specifies that an input field is read-only.
* @default false
*/
readonly?: boolean | undefined;
/**
* Index of the element in tabbing order.
*/
tabindex?: number | undefined;
/**
* Identifier of the underlying input element.
*/
inputId?: string | undefined;
/**
* Style class of the input field.
*/
inputClass?: string | object | undefined;
/**
* Inline style of the input field.
*/
inputStyle?: object | undefined;
/**
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
*/
ariaLabelledby?: string | undefined;
/**
* Establishes a string value that labels the component.
*/
ariaLabel?: string | undefined;
/**
* It generates scoped CSS variables using design tokens for the component.
*/
dt?: DesignToken<any>;
/**
* Used to pass attributes to DOM elements inside the component.
* @type {InputSwitchPassThroughOptions}
*/
pt?: PassThrough<InputSwitchPassThroughOptions>;
/**
* Used to configure passthrough(pt) options of the component.
* @type {PassThroughOptions}
*/
ptOptions?: PassThroughOptions;
/**
* When enabled, it removes component related styles in the core.
* @defaultValue false
*/
unstyled?: boolean;
}
export interface InputSwitchProps extends ToggleSwitch.ToggleSwitchProps {}

/**
* Defines current options in InputSwitch component.
*/
export interface InputSwitchContext {
/**
* Current checked state of the item as a boolean.
* @defaultValue false
*/
checked: boolean;
/**
* Current disabled state of the item as a boolean.
* @defaultValue false
*/
disabled: boolean;
}
export interface InputSwitchContext extends ToggleSwitch.ToggleSwitchContext {}

export interface InputSwitchSlots {}
export interface InputSwitchSlots extends ToggleSwitch.ToggleSwitchSlots {}

/**
* Defines valid emits in InputSwitch component.
*/
export interface InputSwitchEmits {
/**
* Emitted when the value changes.
* @param {boolean} value - New value.
*/
'update:modelValue'(value: boolean): void;
/**
* Callback to invoke on value change.
* @param {Event} event - Browser event.
*/
change(event: Event): void;
/**
* Callback to invoke when the component receives focus.
* @param {Event} event - Browser event.
*/
focus(event: Event): void;
/**
* Callback to invoke when the component loses focus.
* @param {Event} event - Browser event.
*/
blur(event: Event): void;
}
export interface InputSwitchEmits extends ToggleSwitch.ToggleSwitchEmits {}

/**
* @deprecated Deprecated since v4. Use ToggleSwitch component instead.
*
* **PrimeVue - InputSwitch**
*
* _InputSwitch is used to select a boolean value._
*
* [Live Demo](https://www.primevue.org/inputswitch/)
* [Live Demo](https://www.primevue.org/toggleswitch/)
* --- ---
* ![PrimeVue](https://primefaces.org/cdn/primevue/images/logo-100.png)
*
Expand Down
63 changes: 4 additions & 59 deletions components/lib/inputswitch/InputSwitch.vue
Original file line number Diff line number Diff line change
@@ -1,66 +1,11 @@
<template>
<div :class="cx('root')" :style="sx('root')" v-bind="getPTOptions('root')" :data-p-highlight="checked" :data-p-disabled="disabled">
<input
:id="inputId"
type="checkbox"
role="switch"
:class="[cx('input'), inputClass]"
:style="inputStyle"
:checked="checked"
:tabindex="tabindex"
:disabled="disabled"
:readonly="readonly"
:aria-checked="checked"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
:aria-invalid="invalid || undefined"
@focus="onFocus"
@blur="onBlur"
@change="onChange"
v-bind="getPTOptions('input')"
/>
<span :class="cx('slider')" v-bind="getPTOptions('slider')"></span>
</div>
</template>

<script>
import BaseInputSwitch from './BaseInputSwitch.vue';
import ToggleSwitch from 'primevue/toggleswitch';
export default {
name: 'InputSwitch',
extends: BaseInputSwitch,
inheritAttrs: false,
emits: ['update:modelValue', 'change', 'focus', 'blur'],
methods: {
getPTOptions(key) {
const _ptm = key === 'root' ? this.ptmi : this.ptm;
return _ptm(key, {
context: {
checked: this.checked,
disabled: this.disabled
}
});
},
onChange(event) {
if (!this.disabled && !this.readonly) {
const newValue = this.checked ? this.falseValue : this.trueValue;
this.$emit('update:modelValue', newValue);
this.$emit('change', event);
}
},
onFocus(event) {
this.$emit('focus', event);
},
onBlur(event) {
this.$emit('blur', event);
}
},
computed: {
checked() {
return this.modelValue === this.trueValue;
}
extends: ToggleSwitch,
mounted() {
console.warn('Deprecated since v4. Use ToggleSwitch component instead.');
}
};
</script>
4 changes: 2 additions & 2 deletions components/lib/inputswitch/style/InputSwitchStyle.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { BaseStyle } from '../../base/style';
import { ToggleSwitchStyle } from '../../toggleswitch/style/ToggleSwitchStyle';

export interface InputSwitchStyle extends BaseStyle {}
export interface InputSwitchStyle extends ToggleSwitchStyle {}
21 changes: 1 addition & 20 deletions components/lib/inputswitch/style/InputSwitchStyle.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import BaseStyle from 'primevue/base/style';

const inlineStyles = {
root: { position: 'relative' }
};

const classes = {
root: ({ instance, props }) => [
'p-toggleswitch p-component',
{
'p-toggleswitch-checked': instance.checked,
'p-disabled': props.disabled,
'p-invalid': props.invalid
}
],
input: 'p-toggleswitch-input',
slider: 'p-toggleswitch-slider'
};

export default BaseStyle.extend({
name: 'inputswitch',
classes,
inlineStyles
name: 'inputswitch'
});
Loading

0 comments on commit f240b95

Please sign in to comment.