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

Commit 5fa522e

Browse files
committed
feat(button): update button tests and add button spinner
1 parent 0895916 commit 5fa522e

File tree

5 files changed

+134
-86
lines changed

5 files changed

+134
-86
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<template>
2+
<div>
3+
<c-button
4+
mr="3"
5+
:is-loading="isLoading"
6+
@click="doTask"
7+
color-scheme="teal"
8+
>
9+
Simple loading
10+
</c-button>
11+
12+
<chakra.p mt="3">With loading text</chakra.p>
13+
14+
<c-button
15+
mr="3"
16+
:is-loading="isLoading"
17+
@click="doTask"
18+
loading-text="Working..."
19+
color-scheme="teal"
20+
>
21+
Loading text button
22+
</c-button>
23+
</div>
24+
</template>
25+
26+
<script lang="ts">
27+
import { defineComponent, ref } from 'vue'
28+
29+
export default defineComponent({
30+
setup() {
31+
const isLoading = ref(false)
32+
33+
let loadingTimeout: any
34+
const doTask = () => {
35+
isLoading.value = true
36+
loadingTimeout = setTimeout(() => {
37+
isLoading.value = false
38+
console.log({ isLoading: isLoading.value })
39+
clearTimeout(loadingTimeout)
40+
}, 2000)
41+
}
42+
43+
return {
44+
isLoading,
45+
doTask,
46+
}
47+
},
48+
})
49+
</script>

packages/c-button/src/button.ts

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ComponentThemeConfig } from '@chakra-ui/vue-theme'
1313
import { dataAttr, mergeWith } from '@chakra-ui/vue-utils'
1414
import { useButtonGroup } from './button-group'
1515
import { CIcon } from '@chakra-ui/c-icon'
16+
import { CSpinner } from '@chakra-ui/c-spinner'
1617

1718
type ButtonTypes = 'button' | 'reset' | 'submit'
1819

@@ -49,6 +50,63 @@ const props = {
4950
},
5051
}
5152

53+
const CButtonSpinner = defineComponent({
54+
name: 'CButtonSpinner',
55+
inheritAttrs: false,
56+
props: {
57+
label: Boolean as PropType<boolean>,
58+
spacing: [Number, String, Array] as PropType<
59+
number | string | string[] | number[]
60+
>,
61+
},
62+
setup(props, { attrs }) {
63+
const spinnerStyles = computed<SystemStyleObject>(() => ({
64+
display: 'flex',
65+
alignItems: 'center',
66+
position: props.label ? 'relative' : 'absolute',
67+
marginEnd: props.label ? props.spacing : 0,
68+
}))
69+
return () =>
70+
h(
71+
chakra('div', {
72+
label: 'button__spinner',
73+
}),
74+
{
75+
...spinnerStyles.value,
76+
...attrs,
77+
},
78+
[
79+
h(CSpinner, {
80+
color: 'currentColor',
81+
width: '1em',
82+
height: '1em',
83+
}),
84+
]
85+
)
86+
},
87+
})
88+
89+
/**
90+
* CButtonIcon
91+
*
92+
* Button icon component
93+
*/
94+
const CButtonIcon = defineComponent({
95+
name: 'CButtonIcon',
96+
inheritAttrs: false,
97+
props: {
98+
icon: String as PropType<string>,
99+
},
100+
setup(props, { attrs }) {
101+
return () =>
102+
h(CIcon, {
103+
label: 'button__icon',
104+
name: props.icon,
105+
...attrs,
106+
})
107+
},
108+
})
109+
52110
/**
53111
* CButton
54112
*
@@ -90,9 +148,8 @@ const CButton = defineComponent({
90148

91149
return () =>
92150
h(
93-
chakra('button'),
151+
chakra(props.as),
94152
{
95-
as: props.as,
96153
label: 'button',
97154
disabled: props.isDisabled || props.isLoading,
98155
type: props.as === 'button' ? undefined : props.type,
@@ -101,73 +158,33 @@ const CButton = defineComponent({
101158
...buttonStyles,
102159
...attrs,
103160
},
104-
[
105-
props.leftIcon && !props.isLoading
106-
? h(CButtonIcon, {
107-
icon: props.leftIcon,
108-
marginEnd: props.iconSpacing,
109-
})
110-
: null,
111-
slots?.default?.({}),
112-
props.rightIcon && !props.isLoading
113-
? h(CButtonIcon, {
114-
icon: props.rightIcon,
115-
marginStart: props.iconSpacing,
116-
})
117-
: null,
161+
() => [
162+
props.leftIcon &&
163+
!props.isLoading &&
164+
h(CButtonIcon, {
165+
icon: props.leftIcon,
166+
marginEnd: props.iconSpacing,
167+
}),
168+
props.isLoading &&
169+
// @ts-expect-error Suppress (No overload matches this call.). Not sure about how to fix this now
170+
h(CButtonSpinner, {
171+
spacing: props.iconSpacing,
172+
label: props.loadingText,
173+
__css: { fontSize: '1em', lineHeight: 'normal' },
174+
}),
175+
props.isLoading
176+
? props.loadingText ||
177+
h(chakra.span, { opacity: 0 }, slots?.default?.())
178+
: slots?.default?.(),
179+
props.rightIcon &&
180+
!props.isLoading &&
181+
h(CButtonIcon, {
182+
icon: props.rightIcon,
183+
marginStart: props.iconSpacing,
184+
}),
118185
]
119186
)
120187
},
121188
})
122189

123-
const CButtonSpinner = defineComponent({
124-
name: 'CButtonSpinner',
125-
props: {
126-
label: Boolean as PropType<boolean>,
127-
spacing: [Number, String, Array] as PropType<
128-
number | string | string[] | number[]
129-
>,
130-
__css: Object as PropType<SystemStyleObject>,
131-
},
132-
setup(props, { attrs, slots }) {
133-
const spinnerStyles: SystemStyleObject = {
134-
display: 'flex',
135-
alignItems: 'center',
136-
position: props.label ? 'relative' : 'absolute',
137-
marginEnd: props.label ? props.spacing : 0,
138-
...props.__css,
139-
}
140-
return () =>
141-
h(
142-
chakra.div,
143-
{
144-
label: 'button__spinner',
145-
__css: spinnerStyles,
146-
},
147-
slots ?? slots
148-
)
149-
},
150-
})
151-
152-
/**
153-
* CButtonIcon
154-
*
155-
* Button icon component
156-
*/
157-
const CButtonIcon = defineComponent({
158-
name: 'CButtonIcon',
159-
inheritAttrs: false,
160-
props: {
161-
icon: String as PropType<string>,
162-
},
163-
setup(props, { attrs }) {
164-
return () =>
165-
h(CIcon, {
166-
label: 'button__icon',
167-
name: props.icon,
168-
...attrs,
169-
})
170-
},
171-
})
172-
173190
export default CButton

packages/c-button/tests/__snapshots__/c-button.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
exports[`should render properly 1`] = `
44
<DocumentFragment>
55
<button
6-
as="button"
76
class="css-myeq6i"
87
>
8+
<!---->
99
<!---->
1010
Happy button
1111
<!---->

packages/c-button/tests/__snapshots__/c-button.test.tsx.snap

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/system/src/chakra.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ interface StyleResolverOptions extends StyleResolverProps {
5050

5151
interface ChakraFactoryOptions extends StyleResolverProps {}
5252

53-
declare global {
54-
namespace JSX {
55-
interface IntrinsicAttributes extends StyleResolverProps {}
56-
}
57-
}
58-
5953
const chakraProps = {
6054
__css: Object as PropType<StyleResolverProps['__css']>,
6155
sx: Object as PropType<StyleResolverProps['sx']>,
@@ -126,6 +120,7 @@ export const chakra: IChakraFactory = (
126120
options = {} as ChakraFactoryOptions
127121
): DefineComponent => {
128122
return defineComponent({
123+
name: `chakra-factory-${String(tag)}`,
129124
inheritAttrs: false,
130125
props: chakraProps,
131126
setup(props, { slots, attrs }) {

0 commit comments

Comments
 (0)