Skip to content

Commit

Permalink
feat: checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
JasKang committed Mar 9, 2024
1 parent 1d865e7 commit 146eaa3
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 107 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"references.preferredLocation": "peek",
"eslint.format.enable": true,
"eslint.experimental.useFlatConfig": true,
"tailwindCSS.experimental.classRegex": [["/\\*tw\\*/ ([^;]*);", "'([^']*)'"], "\\/\\*\\s?tw:\\s?\\*\\/\\s?`(.*)`"]
"tailwindCSS.experimental.classRegex": [
["tw\\((([^()]*|\\([^()]*\\))*)\\)", "[\"'`]([^\"'`]*).*?[\"'`]"],
"\\/\\*\\s?tw:\\s?\\*\\/\\s?`(.*)`"
]
}
4 changes: 2 additions & 2 deletions packages/vue/src/Button/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { ref, computed, PropType } from 'vue'
import { LoadingIcon } from '../Icon/LoadingIcon'
import Loading from '../Icon/Loading.vue'
import { ColorAlias } from '@/utils/style'
import { createButtonCss } from './style'
Expand Down Expand Up @@ -44,7 +44,7 @@ const css = computed(() =>
<button :class="css" :disabled="props.disabled" type="button">
<template v-if="loading">
<i class="h-[1em] w-[1em] [&_+_*]:ml-1.5 [&_svg]:h-full [&_svg]:w-full">
<LoadingIcon class="animate-spin" />
<Loading class="animate-spin" />
</i>
</template>
<template v-else-if="slots.icon">
Expand Down
30 changes: 15 additions & 15 deletions packages/vue/src/Button/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,79 +15,79 @@ export const createButtonCss = tw(
{
variant: 'default',
color: 'normal',
class: /*tw:*/ `border-gray-300 text-current bg-white enabled:hover:bg-gray-50`,
class: ` border-gray-300 text-current bg-white enabled:hover:bg-gray-50`,
},
{
variant: 'default',
color: 'primary',
class: /*tw:*/ `border-transparent text-white bg-primary-500 enabled:hover:bg-primary-600`,
class: `border-transparent text-white bg-primary-500 enabled:hover:bg-primary-600`,
},
{
variant: 'default',
color: 'success',
class: /*tw:*/ `border-transparent text-white bg-success-500 enabled:hover:bg-success-600`,
class: `border-transparent text-white bg-success-500 enabled:hover:bg-success-600`,
},
{
variant: 'default',
color: 'warning',
class: /*tw:*/ `border-transparent text-white bg-warning-500 enabled:hover:bg-warning-600`,
class: `border-transparent text-white bg-warning-500 enabled:hover:bg-warning-600`,
},
{
variant: 'default',
color: 'danger',
class: /*tw:*/ `border-transparent text-white bg-danger-500 enabled:hover:bg-danger-600`,
class: `border-transparent text-white bg-danger-500 enabled:hover:bg-danger-600`,
},

{
variant: 'secondary',
color: 'normal',
class: /*tw:*/ `border-transparent text-current bg-gray-100 enabled:hover:bg-gray-200`,
class: `border-transparent text-current bg-gray-100 enabled:hover:bg-gray-200`,
},
{
variant: 'secondary',
color: 'primary',
class: /*tw:*/ `border-transparent text-primary-600 bg-primary-100 enabled:hover:bg-primary-200`,
class: `border-transparent text-primary-600 bg-primary-100 enabled:hover:bg-primary-200`,
},
{
variant: 'secondary',
color: 'success',
class: /*tw:*/ `border-transparent text-success-600 bg-success-100 enabled:hover:bg-success-200`,
class: `border-transparent text-success-600 bg-success-100 enabled:hover:bg-success-200`,
},
{
variant: 'secondary',
color: 'warning',
class: /*tw:*/ `border-transparent text-warning-600 bg-warning-100 enabled:hover:bg-warning-200`,
class: `border-transparent text-warning-600 bg-warning-100 enabled:hover:bg-warning-200`,
},
{
variant: 'secondary',
color: 'danger',
class: /*tw:*/ `border-transparent text-danger-600 bg-danger-100 enabled:hover:bg-danger-200`,
class: `border-transparent text-danger-600 bg-danger-100 enabled:hover:bg-danger-200`,
},

{
variant: 'ghost',
color: 'normal',
class: /*tw:*/ `border-transparent text-current bg-transparent enabled:hover:bg-gray-200`,
class: `border-transparent text-current bg-transparent enabled:hover:bg-gray-200`,
},
{
variant: 'ghost',
color: 'primary',
class: /*tw:*/ `border-transparent text-primary-600 bg-transparent enabled:hover:bg-primary-200`,
class: `border-transparent text-primary-600 bg-transparent enabled:hover:bg-primary-200`,
},
{
variant: 'ghost',
color: 'success',
class: /*tw:*/ `border-transparent text-success-600 bg-transparent enabled:hover:bg-success-200`,
class: `border-transparent text-success-600 bg-transparent enabled:hover:bg-success-200`,
},
{
variant: 'ghost',
color: 'warning',
class: /*tw:*/ `border-transparent text-warning-600 bg-transparent enabled:hover:bg-warning-200`,
class: `border-transparent text-warning-600 bg-transparent enabled:hover:bg-warning-200`,
},
{
variant: 'ghost',
color: 'danger',
class: /*tw:*/ `border-transparent text-danger-600 bg-transparent enabled:hover:bg-danger-200`,
class: `border-transparent text-danger-600 bg-transparent enabled:hover:bg-danger-200`,
},
],
variants: {
Expand Down
56 changes: 56 additions & 0 deletions packages/vue/src/Checkbox/Checkbox.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<script setup lang="ts">
import { ref, computed } from 'vue'
defineOptions({ name: 'TCheckbox' })
const emit = defineEmits<{ click: [any] }>()
const slots = defineSlots<{ default?(_: {}): any }>()
const props = defineProps({value: { type: null, required: true },
name: String,
disabled: Boolean,
checked: { type: Boolean, default: undefined },})
const group = inject(CheckboxGroupInjectKey, null)
console.log(parent)
const [innerChecked, setInnerChecked] = useModelValue(props, {
defaultValue: group ? group.value.value.includes(props.value) : false,
valuePropName: 'checked',
onChange: (val: boolean) => {
emit('change', val)
if (group) {
if (val) {
group.insert(props.value)
} else {
group.remove(props.value)
}
}
},
})
const checked = computed(() => (group ? group.value.value.includes(props.value) : innerChecked.value))
const onInput = (e: Event) => {
const el = e.currentTarget as HTMLInputElement
console.log(el.checked)
setInnerChecked(el.checked)
}
</script>
<template>
<label
class={[
'relative inline-flex items-center',
props.disabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer',
]}
>
<input
class="form-input h-4 w-4 rounded border-gray-300 text-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500"
style={{ boxShadow: 'none', cursor: 'inherit' }}
type="checkbox"
name={props.name}
disabled={props.disabled}
checked={checked.value}
onInput={onInput}
/>
{slots.default ? <span class="relative ml-2 text-sm font-medium">{slots.default()}</span> : null}
</label>
</template>
36 changes: 36 additions & 0 deletions packages/vue/src/Checkbox/CheckboxGroup.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script setup lang="ts">
import { ref, computed } from 'vue'
defineOptions({ name: 'TCheckboxGroup' })
const emit = defineEmits<{ click: [any] }>()
const slots = defineSlots<{ default?(_: {}): any }>()
const props = defineProps({ name: String, value: Array as PropType<unknown[]>, disabled: Boolean })
const [state, setState] = useModelValue<unknown[]>(props, {
defaultValue: [],
onChange: (val: any[]) => {
emit('change', val)
},
})
provide(CheckboxGroupInjectKey, {
value: state,
props: props,
insert: (val: unknown) => {
if (state.value.indexOf(val) === -1) {
setState([...state.value, val])
}
},
remove: (val: unknown) => {
const index = state.value.indexOf(val)
if (index !== -1) {
const r = [...state.value]
r.splice(index, 1)
setState(r)
}
},
})
</script>
<template>
<div></div>
</template>
<style lang="scss"></style>
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/vue/src/Icon/ChevronUpDown.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
defineOptions({ name: 'ChevronUpDown' })
defineOptions({ name: 'ChevronUpDownIcon' })
</script>
<template>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
Expand Down
13 changes: 13 additions & 0 deletions packages/vue/src/Icon/Loading.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup lang="ts">
defineOptions({ name: 'LoadingIcon' })
</script>
<template>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle opacity="0.25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path
opacity="0.75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
</template>
14 changes: 0 additions & 14 deletions packages/vue/src/Icon/LoadingIcon.tsx

This file was deleted.

73 changes: 0 additions & 73 deletions packages/vue/src/utils/style.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/vue/src/utils/theme.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type ColorAlias = 'primary' | 'success' | 'warning' | 'danger'

export type Size = 'sm' | 'md' | 'lg'

export type Status = 'primary' | 'success' | 'warning' | 'danger'
Expand Down
1 change: 1 addition & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export default {
htmlWhitespaceSensitivity: 'css',
vueIndentScriptAndStyle: false,
plugins: ['prettier-plugin-tailwindcss'],
tailwindFunctions: ['tw'],
}
8 changes: 7 additions & 1 deletion tailwind.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ module.exports = {
},
},
},
content: ['./index.html', './packages/vue/src/*.{ts,tsx,md,mdx}', './packages/docs/*.{ts,tsx,md,mdx}'],
content: [
'./index.html',
'./packages/vue/src/**/*.{ts,tsx}',
'./packages/docs/*.{md,mdx}',
'./packages/docs/components/**/*.{md,mdx}',
'./packages/docs/.vitepress/theme/**/*.{md,mdx}',
],
}

0 comments on commit 146eaa3

Please sign in to comment.