Skip to content

Commit 2873800

Browse files
authored
fix(button): use default behavior when is link (#399)
1 parent 8bca51c commit 2873800

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

packages/core/src/components/button/button.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { newSpecPage } from '@stencil/core/testing'
2+
23
import { AtomButton } from './button'
34

45
describe('AtomButton', () => {
@@ -140,8 +141,10 @@ describe('AtomButton', () => {
140141

141142
await page.waitForChanges()
142143
const formEl = page.body.querySelector('form') as HTMLFormElement
144+
143145
formEl.requestSubmit = jest.fn
144146
const buttonEl = page.root?.shadowRoot?.querySelector('ion-button')
147+
145148
jest.spyOn(formEl, 'requestSubmit')
146149

147150
buttonEl?.click()
@@ -157,8 +160,10 @@ describe('AtomButton', () => {
157160

158161
await page.waitForChanges()
159162
const formEl = page.body.querySelector('form') as HTMLFormElement
163+
160164
formEl.reset = jest.fn()
161165
const buttonEl = page.root?.shadowRoot?.querySelector('ion-button')
166+
162167
jest.spyOn(formEl, 'reset')
163168
buttonEl?.click()
164169

@@ -173,11 +178,35 @@ describe('AtomButton', () => {
173178

174179
await page.waitForChanges()
175180
const formEl = page.body.querySelector('form') as HTMLFormElement
181+
176182
formEl.reset = jest.fn()
177183
const buttonEl = page.root?.shadowRoot?.querySelector('ion-button')
184+
178185
jest.spyOn(formEl, 'reset')
179186
buttonEl?.click()
180187

181188
expect(formEl.reset).not.toHaveBeenCalled()
182189
})
190+
191+
it('should allow default event when is link', async () => {
192+
const button = new AtomButton()
193+
194+
button.href = 'http://example.com'
195+
button.download = 'name_file'
196+
button.target = '_blank'
197+
198+
const event = new MouseEvent('click', {
199+
bubbles: true,
200+
cancelable: true,
201+
})
202+
203+
jest.spyOn(event, 'preventDefault')
204+
jest.spyOn(event, 'stopPropagation')
205+
206+
//@ts-expect-error - Testing private method
207+
button.handleClick(event)
208+
209+
expect(event.preventDefault).not.toHaveBeenCalled()
210+
expect(event.stopPropagation).not.toHaveBeenCalled()
211+
})
183212
})

packages/core/src/components/button/button.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ export class AtomButton {
3838
submit: 'requestSubmit',
3939
}
4040

41+
private get isLink() {
42+
return this.href || this.download || this.target
43+
}
44+
4145
private handleClick = (event) => {
46+
if (this.isLink) return
47+
4248
event.preventDefault()
4349
event.stopPropagation()
4450

packages/core/src/components/button/stories/button.core.stories.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,45 @@ export const IconAndText: StoryObj = {
115115
...Primary.args,
116116
},
117117
}
118+
119+
export const Link: StoryObj = {
120+
render: (args) => html`
121+
<atom-button
122+
color="${args.color}"
123+
fill="${args.fill}"
124+
expand="${args.expand}"
125+
size="${args.size}"
126+
disabled="${args.disabled}"
127+
loading="${args.loading}"
128+
type="${args.type}"
129+
mode="${args.mode}"
130+
shape="${args.shape}"
131+
download="${args.download}"
132+
href="${args.href}"
133+
target="${args.target}"
134+
>
135+
${args.label}
136+
</atom-button>
137+
`,
138+
args: {
139+
...Primary.args,
140+
href: undefined,
141+
download: undefined,
142+
target: undefined,
143+
},
144+
argTypes: {
145+
href: {
146+
options: ['Download', 'Navigate'],
147+
mapping: {
148+
Download: '/custom/jsm.svg',
149+
Navigate: 'https://www.juntossomosmais.com.br',
150+
},
151+
},
152+
download: {
153+
control: 'text',
154+
},
155+
target: {
156+
options: ['_blank', '_self', '_parent', '_top'],
157+
},
158+
},
159+
}

packages/core/src/components/button/stories/button.react.stories.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,45 @@ export const IconAndText: StoryObj = {
114114
...Primary.args,
115115
},
116116
}
117+
118+
export const Link: StoryObj = {
119+
render: (args: any) => (
120+
<AtomButton
121+
color={args.color}
122+
fill={args.fill}
123+
size={args.size}
124+
disabled={args.disabled}
125+
loading={args.loading}
126+
type={args.type}
127+
mode={args.mode}
128+
expand={args.expand}
129+
shape={args.shape}
130+
download={args.download}
131+
href={args.href}
132+
target={args.target}
133+
>
134+
{args.label}
135+
</AtomButton>
136+
),
137+
args: {
138+
...Primary.args,
139+
href: undefined,
140+
download: undefined,
141+
target: undefined,
142+
},
143+
argTypes: {
144+
href: {
145+
options: ['Download', 'Navigate'],
146+
mapping: {
147+
Download: '/custom/jsm.svg',
148+
Navigate: 'https://www.juntossomosmais.com.br',
149+
},
150+
},
151+
download: {
152+
control: 'text',
153+
},
154+
target: {
155+
options: ['_blank', '_self', '_parent', '_top'],
156+
},
157+
},
158+
}

0 commit comments

Comments
 (0)