Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit c2d513f

Browse files
Merge pull request #295 from chakra-ui/fix/switch-v-model
fix: CSwitch v-model works combined with `@change` event handler
2 parents 3bf03b9 + 8d6705b commit c2d513f

File tree

11 files changed

+103
-16
lines changed

11 files changed

+103
-16
lines changed

.changeset/pre.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"mode": "pre",
3+
"tag": "next",
4+
"initialVersions": {
5+
"@chakra-ui/vue": "0.6.1",
6+
"@chakra-ui/theme-vue": "0.2.6",
7+
"@chakra-ui/nuxt": "0.0.9",
8+
"chakra-ui-docs": "0.5.1"
9+
},
10+
"changesets": [
11+
"wise-donkeys-buy"
12+
]
13+
}

.changeset/wise-donkeys-buy.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@chakra-ui/vue': patch
3+
'@chakra-ui/nuxt': patch
4+
---
5+
6+
Fix: CSwitch component v-model + @change event handler.

packages/chakra-ui-core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 0.6.2-next.0
4+
5+
### Patch Changes
6+
7+
- Fix: CSwitch component v-model + @change event handler.
8+
39
## 0.6.1
410

511
### Patch Changes

packages/chakra-ui-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chakra-ui/vue",
3-
"version": "0.6.1",
3+
"version": "0.6.2-next.0",
44
"description": "Build Accessible and Responsive Vue.js websites and applications with speed ⚡️",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

packages/chakra-ui-core/src/CSwitch/CSwitch.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @see Source https://github.com/chakra-ui/chakra-ui-vue/blob/master/packages/chakra-ui-core/src/CSwitch/CSwitch.js
99
*/
1010

11-
import { forwardProps, extractListeners } from '../utils'
11+
import { forwardProps } from '../utils'
1212

1313
import CBox from '../CBox'
1414
import CVisuallyHidden from '../CVisuallyHidden'
@@ -29,6 +29,15 @@ const switchSizes = {
2929
}
3030
}
3131

32+
/** Emits events for functional components */
33+
const emitFunctionalEvent = (fn, ...args) => {
34+
if (fn && typeof fn === 'function') {
35+
fn(...args)
36+
} else if (Array.isArray(fn)) {
37+
fn.forEach(handler => typeof handler === 'function' && handler(...args))
38+
}
39+
}
40+
3241
/**
3342
* CSwitch component
3443
*
@@ -101,18 +110,14 @@ const CSwitch = {
101110
}
102111
}
103112

104-
// Events
105-
const nonNativeEvents = {
113+
const eventListeners = {
114+
...listeners,
106115
change: (e) => {
107-
const emitChange = listeners.change
108-
if (emitChange && typeof emitChange === 'function') {
109-
emitChange(!props.isChecked)
110-
}
116+
const newValue = !props.isChecked
117+
emitFunctionalEvent(listeners.change, newValue, e)
111118
}
112119
}
113120

114-
const { native, nonNative } = extractListeners({ listeners }, nonNativeEvents)
115-
116121
return h(CBox, {
117122
...rest,
118123
props: {
@@ -142,8 +147,7 @@ const CSwitch = {
142147
checked: props.isChecked,
143148
disabled: props.isDisabled
144149
},
145-
on: nonNative,
146-
nativeOn: native
150+
on: eventListeners
147151
}),
148152
h(CControlBox, {
149153
attrs: styleProps

packages/chakra-ui-core/src/CSwitch/CSwitch.stories.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,20 @@ storiesOf('UI | Switch', module)
4444
}
4545
}
4646
}))
47+
.add('With v-model + @change', () => ({
48+
components: { CBox, CSwitch },
49+
data: () => ({
50+
enable: false
51+
}),
52+
template: `
53+
<p>
54+
<span>{{enable}}</span>
55+
<c-switch v-model="enable" id="email-alerts" @change="action"/>
56+
</p>`,
57+
methods: {
58+
action: action('@change(event) + v-model'),
59+
handleClick (e) {
60+
console.log('Native event handler', e)
61+
}
62+
}
63+
}))

packages/chakra-ui-core/src/CSwitch/tests/CSwitch.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,29 @@ test('properly handles v-model', async () => {
4949

5050
expect(screen.getByText('enabled')).toBeInTheDocument()
5151
})
52+
53+
/**
54+
* Because the CSwitch is a functional component, it
55+
* handles event emission differently when the
56+
* consumer uses both v-model and the @change
57+
* event listener.
58+
*/
59+
it('should emit a change event when v-model + event listener are provided', async () => {
60+
const onChange = jest.fn()
61+
renderComponent({
62+
data: () => ({
63+
enable: false
64+
}),
65+
template: `
66+
<div>
67+
<span>{{enable ? 'enabled' : 'disabled'}}</span>
68+
<CSwitch v-model="enable" data-testid="switch" @change="handleChange" />
69+
</div>`,
70+
methods: { handleChange: onChange }
71+
})
72+
73+
expect(screen.getByText('disabled')).toBeInTheDocument()
74+
await userEvent.click(screen.getByTestId('switch'))
75+
expect(screen.getByText('enabled')).toBeInTheDocument()
76+
expect(onChange).toHaveBeenCalledTimes(1)
77+
})

packages/nuxt-chakra/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## 0.0.10-next.0
4+
5+
### Patch Changes
6+
7+
- Fix: CSwitch component v-model + @change event handler.
8+
- Updated dependencies [undefined]
9+
- @chakra-ui/vue@0.6.2-next.0
10+
311
## 0.0.7
412

513
### Patch Changes

packages/nuxt-chakra/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chakra-ui/nuxt",
3-
"version": "0.0.9",
3+
"version": "0.0.10-next.0",
44
"description": "Chakra UI Module for Nuxt.js",
55
"repository": "https://github.com/chakra-ui/chakra-ui-vue",
66
"license": "MIT",
@@ -18,7 +18,7 @@
1818
"test": "jest"
1919
},
2020
"dependencies": {
21-
"@chakra-ui/vue": "^0.6.1",
21+
"@chakra-ui/vue": "^0.6.2-next.0",
2222
"emotion": "^10.0.27"
2323
},
2424
"peerDependencies": {

website/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## 0.5.2-next.0
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [undefined]
8+
- @chakra-ui/vue@0.6.2-next.0
9+
310
## 0.5.1
411

512
### Patch Changes

0 commit comments

Comments
 (0)