Skip to content

Commit

Permalink
edit switch
Browse files Browse the repository at this point in the history
  • Loading branch information
mdl95r committed Feb 25, 2024
1 parent 8a416cd commit a94d9a9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/components/Switch/Switch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
--switch-size-width: 40px;
--switch-size-height: 24px;

&__checkbox,
&__marker {
width: var(--switch-size-width);
height: var(--switch-size-height);
}

// .ui-switch__checkbox
&__checkbox {
opacity: 0;
Expand All @@ -24,9 +18,9 @@
cursor: pointer;

&:disabled {
cursor: not-allowed;

~.ui-switch__marker {
cursor: not-allowed;

&:after {
background: var(--switch-disabled-marker, rgba(1, 1, 1, .20));
}
Expand All @@ -46,7 +40,9 @@

// .ui-switch__marker
&__marker {
pointer-events: none;
width: var(--switch-size-width);
height: var(--switch-size-height);
cursor: pointer;
flex-shrink: 0;

&:after {
Expand Down
12 changes: 8 additions & 4 deletions src/components/Switch/Switch.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<template>
<div class="ui-switch">
<input
type="checkbox"
id="ui-switch"
type="checkbox"
:checked="modelValue"
:disabled="disabled"
class="ui-switch__checkbox"
data-testid="switch-input"
@change="switchHandler"
>

<div class="ui-switch__marker"></div>
<label class="ui-switch__marker" for="ui-switch" data-test-id="switch"></label>

<div v-if="$slots.default" class="ui-switch__text">
<slot />
Expand All @@ -25,16 +26,19 @@ defineProps({
default: false
},
/**
* Делает switch неактивным
*/
disabled: {
type: Boolean,
default: false,
}
})
const emit = defineEmits(['onUpdate:modelValue']);
const emit = defineEmits(['update:modelValue']);
const switchHandler = ($event) => {
emit('onUpdate:modelValue', $event.target.checked);
emit('update:modelValue', $event.target.checked);
}
</script>

Expand Down
4 changes: 3 additions & 1 deletion stories/Switch.stories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UiSwitch from "../src/components/Switch";
import './assets/main.scss';

export default {
title: 'Elements/Switch',
Expand Down Expand Up @@ -35,7 +36,8 @@ export const WithText = {
};
},
template: `
<UiSwitch v-bind="args" v-model="checked">Text</UiSwitch>
<UiSwitch v-bind="args" v-model="checked">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Velit molestias ex natus eligendi nulla tempora id illo a enim, sapiente officia corporis incidunt asperiores modi quibusdam ut et repellat neque! Lorem ipsum dolor sit, amet consectetur adipisicing elit. Velit molestias ex natus eligendi nulla tempora id illo a enim, sapiente officia corporis incidunt asperiores modi quibusdam ut et repellat neque!</UiSwitch>
`,
}),
};
Expand Down
14 changes: 14 additions & 0 deletions tests/Switch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@ describe('Switch', () => {
it('Switch render', () => {
expect(wrapper.exists()).toBe(true);
})

it('Switch emitting update:modelValue', async () => {
const wrapper = mount(Switch, {
attachTo: document.body,
props: {
modelValue: false,
'update:modelValue': (e) => wrapper.setProps({ modelValue: e })
}
})

await wrapper.find('[data-test-id="switch"]').trigger('click');

expect(wrapper.emitted()).toHaveProperty('update:modelValue')
})
});

0 comments on commit a94d9a9

Please sign in to comment.