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

Commit 6e3437d

Browse files
committed
feat: add examples of form controls
1 parent 3cc5085 commit 6e3437d

File tree

8 files changed

+123
-11
lines changed

8 files changed

+123
-11
lines changed

packages/c-form-control/examples/base-c-form-control.vue renamed to packages/c-form-control/examples/input-example.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<c-form-control id="first-name" is-invalid is-required>
2+
<c-form-control id="first-name" is-invalid is-required w="400px">
33
<c-form-label> First name </c-form-label>
44
<c-input placeholder="First Name" />
55
<c-form-helper-text> Keep it very short and sweet! </c-form-helper-text>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<template>
2+
<c-form-control id="first-name" is-invalid is-required w="400px">
3+
<c-form-label> First name </c-form-label>
4+
<c-select>
5+
<option>Option 1</option>
6+
<option>Option 2</option>
7+
<option>Option 3</option>
8+
</c-select>
9+
<c-form-helper-text> Keep it very short and sweet! </c-form-helper-text>
10+
<c-form-error-message>
11+
<c-form-error-icon />
12+
Your first name is invalid
13+
</c-form-error-message>
14+
</c-form-control>
15+
</template>
16+
17+
<script lang="tsx" setup>
18+
import { h, defineComponent, computed, toRefs } from 'vue'
19+
import {
20+
chakra,
21+
useMultiStyleConfig,
22+
omitThemingProps,
23+
} from '@chakra-ui/vue-system'
24+
import { useFormControl } from '@chakra-ui/c-form-control'
25+
import { vueThemingProps } from '@chakra-ui/vue-utils'
26+
27+
const CSelect = defineComponent({
28+
props: vueThemingProps,
29+
setup(props, { slots }) {
30+
const styles = useMultiStyleConfig('Select', props)
31+
const _props = computed(() => toRefs(omitThemingProps(props)))
32+
const inputProps = useFormControl(_props.value)
33+
return () => (
34+
<chakra.select __css={styles.value.field} {...inputProps.value}>
35+
{slots}
36+
</chakra.select>
37+
)
38+
},
39+
})
40+
</script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<c-form-label font-weight="bold" color="blue">
3+
Not wrapped by FormControl
4+
</c-form-label>
5+
</template>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<template>
2+
<c-form-control id="first-name" w="400px">
3+
<c-form-label :_focus="{ color: 'blue.600' }"> First name </c-form-label>
4+
<c-input placeholder="First Name" />
5+
<c-form-error-message> Your first name is invalid </c-form-error-message>
6+
</c-form-control>
7+
</template>
8+
9+
<script lang="tsx" setup>
10+
import { h, defineComponent, computed, toRefs } from 'vue'
11+
import {
12+
chakra,
13+
useMultiStyleConfig,
14+
omitThemingProps,
15+
} from '@chakra-ui/vue-system'
16+
import { useFormControl } from '@chakra-ui/c-form-control'
17+
import { vueThemingProps } from '@chakra-ui/vue-utils'
18+
19+
const CInput = defineComponent({
20+
props: vueThemingProps,
21+
setup(props) {
22+
const styles = useMultiStyleConfig('Input', props)
23+
const _props = computed(() => toRefs(omitThemingProps(props)))
24+
const inputProps = useFormControl(_props.value)
25+
return () => (
26+
<chakra.input __css={styles.value.field} {...inputProps.value} />
27+
)
28+
},
29+
})
30+
</script>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<c-form-control id="first-name" is-invalid is-required w="400px">
3+
<c-form-label> First name </c-form-label>
4+
<c-textarea placeholder="First Name" />
5+
<c-form-helper-text> Keep it very short and sweet! </c-form-helper-text>
6+
<c-form-error-message>
7+
<c-form-error-icon />
8+
Your first name is invalid
9+
</c-form-error-message>
10+
</c-form-control>
11+
</template>
12+
13+
<script lang="tsx" setup>
14+
import { h, defineComponent, computed, toRefs } from 'vue'
15+
import {
16+
chakra,
17+
useMultiStyleConfig,
18+
omitThemingProps,
19+
} from '@chakra-ui/vue-system'
20+
import { useFormControl } from '@chakra-ui/c-form-control'
21+
import { vueThemingProps } from '@chakra-ui/vue-utils'
22+
23+
const CTextarea = defineComponent({
24+
props: vueThemingProps,
25+
setup(props) {
26+
const styles = useMultiStyleConfig('Textarea', props)
27+
const _props = computed(() => toRefs(omitThemingProps(props)))
28+
const inputProps = useFormControl(_props.value)
29+
return () => <chakra.textarea __css={styles.value} {...inputProps.value} />
30+
},
31+
})
32+
</script>

packages/c-form-control/src/c-form-label.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ export const CFormLabel = defineComponent({
3030
textAlign: 'start',
3131
...styles.value
3232
}}
33-
{...field.value.labelProps.value}
33+
{...field?.value?.labelProps.value}
3434
{...attrs}
3535
>
3636
{slots?.default?.()}
37-
{field.value?.isRequired?.value ? requiredIndicator.value : null}
37+
{field?.value?.isRequired?.value ? requiredIndicator.value : null}
3838
</chakra.label>
3939
)
4040
}

packages/c-form-control/src/use-form-control.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ export type CFormControlProviderContext = ComputedRef<Omit<
144144
const [
145145
FormControlProvider,
146146
useFormControlContext
147-
] = createContext<CFormControlProviderContext>()
147+
] = createContext<CFormControlProviderContext>({
148+
strict: false,
149+
name: 'FormControlContext'
150+
})
148151

149152
export { FormControlProvider, useFormControlContext }
150153

playground/src/App.vue

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
<sidebar :stories="routes" />
77
</chakra.div>
88
</perfect-scrollbar>
9-
<chakra.main w="full" pos="relative" border-left="1px solid" border-color="gray.200" padding="4">
10-
<router-view v-slot="{ Component, route }">
11-
<!-- <transition name="fade" mode="out-in"> -->
12-
<component :is="Component" />
13-
<!-- </transition> -->
14-
</router-view>
9+
<c-center w="full" pos="relative" border-left="1px solid" border-color="gray.200" padding="4">
10+
<c-square box-size="600px">
11+
<router-view v-slot="{ Component, route }">
12+
<!-- <transition name="fade" mode="out-in"> -->
13+
<component :is="Component" />
14+
<!-- </transition> -->
15+
</router-view>
16+
</c-square>
1517
<c-icon-button color="inherit" pos="absolute" @click="toggleColorMode" top="10" right="10" :aria-label="`Switch to ${colorMode === 'light' ? 'dark' : 'light'} mode`" :icon="colorMode === 'light' ? 'moon' : 'sun'" />
16-
</chakra.main>
18+
</c-center>
1719
</chakra.section>
1820
</template>
1921

0 commit comments

Comments
 (0)