Skip to content

Commit

Permalink
Merge pull request #245 from marceloatg/feature/checkbox-group
Browse files Browse the repository at this point in the history
SldsCheckboxGroup created
  • Loading branch information
marceloatg authored Dec 8, 2023
2 parents 5c14bf4 + b1111c6 commit 6b1acc9
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 10 deletions.
46 changes: 39 additions & 7 deletions docs/examples/test.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<template>

<!-- Checkbox group -->
<example-container class="slds-m-bottom_medium">
<slds-checkbox-group
v-model="values"
:options="options1"
inline
/>
value: {{ values }}
</example-container>

<!-- Lookup -->
<example-container class="slds-m-bottom_medium">
<slds-grid>
Expand Down Expand Up @@ -417,11 +427,13 @@ import SldsLookup from "../../src/components/slds-lookup/slds-lookup.vue"
import SldsGrid from "../../src/components/slds-grid/slds-grid.vue"
import SldsColumn from "../../src/components/slds-grid/slds-column.vue"
import SldsInput from "../../src/components/slds-input/slds-input.vue"
import SldsCheckboxGroup from "../../src/components/slds-checkbox-group/slds-checkbox-group.vue"
export default {
name: "test",
components: {
SldsCheckboxGroup,
SldsInput,
SldsColumn,
SldsGrid,
Expand Down Expand Up @@ -531,16 +543,36 @@ export default {
test1: "en",
options1: [
{ label: "English", value: "en", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{ label: "German", value: "de", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{ label: "Spanish", value: "es", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{ label: "French", value: "fr", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{ label: "Italian", value: "it", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{ label: "Japanese", value: "ja", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{
label: "English",
value: "en",
description: "Lorem ipsum dolor sit amet.",
iconName: "standard:user",
},
{ label: "German", value: "de", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{
label: "Spanish",
value: "es",
description: "Lorem ipsum dolor sit amet.",
iconName: "standard:user",
},
{ label: "French", value: "fr", description: "Lorem ipsum dolor sit amet.", iconName: "standard:user" },
{
label: "Italian",
value: "it",
description: "Lorem ipsum dolor sit amet.",
iconName: "standard:user",
},
{
label: "Japanese",
value: "ja",
description: "Lorem ipsum dolor sit amet.",
iconName: "standard:user",
},
],
options2: [
{ label: "English", value: "en", iconName: "standard:user" },
{ label: "English", value: "en", iconName: "standard:user" },
{ label: "German", value: "de", iconName: "standard:user" },
{ label: "Spanish", value: "es", iconName: "standard:user" },
{ label: "French", value: "fr", iconName: "standard:user" },
Expand Down
2 changes: 2 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export { default as SldsCheckbox } from "./slds-checkbox/slds-checkbox.vue"
export { default as SldsCheckboxButton } from "./slds-checkbox-button/slds-checkbox-button.vue"
export { default as SldsCheckboxButtonGroup } from "./slds-checkbox-button-group/slds-checkbox-button-group.vue"
export { default as SldsCheckboxButtonOption } from "./slds-checkbox-button-group/slds-checkbox-button-option.vue"
export { default as SldsCheckboxGroup } from "./slds-checkbox-group/slds-checkbox-group.vue"
export { default as SldsCheckboxGroupOption } from "./slds-checkbox-group/slds-checkbox-group-option.vue"
export { default as SldsCheckboxOutput } from "./slds-checkbox-output/slds-checkbox-output.vue"
export { default as SldsCheckboxToggle } from "./slds-checkbox-toggle/slds-checkbox-toggle.vue"
export { default as SldsColumn } from "./slds-grid/slds-column.vue"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import SldsFormElement from "../slds-form-element/slds-form-element.vue"
import { EVENTS } from "../../constants"
import { type CheckboxButtonGroupOption } from "./checkbox-button-group-option"
import { defineComponent, type PropType } from "vue"
import type { ValidationError } from "../slds-form-element/validation-error"
export default defineComponent({
name: "SldsCheckboxButtonGroup",
Expand All @@ -63,7 +64,7 @@ export default defineComponent({
/**
* Array of error objects from vuelidate.
*/
errors: Array,
errors: { type: Array as PropType<ValidationError[]>, default: () => [] as ValidationError[] },
/**
* Inline help text.
Expand Down
17 changes: 17 additions & 0 deletions src/components/slds-checkbox-group/checkbox-group-option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface CheckboxGroupOption {
/**
* Checkbox option label.
*/
label: string

/**
* Checkbox option value.
* This property is used as the v-for key.
*/
value: string

/**
* Indicates whether this option is disabled.
*/
disabled: boolean
}
72 changes: 72 additions & 0 deletions src/components/slds-checkbox-group/slds-checkbox-group-option.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<template>
<div class="slds-checkbox" @click="handleClick">

<!-- Input -->
<input
type="checkbox"
:checked="checked"
:disabled="disabled"
:class="inputClassNames"
>

<!-- Faux -->
<label class="slds-checkbox__label">

<span class="slds-checkbox_faux"/>

<span class="slds-form-element__label">
{{ label }}
</span>

</label>

</div>
</template>

<script lang="ts">
import { defineComponent } from "vue"
export default defineComponent({
name: "SldsCheckboxGroupOption",
props: {
/**
* Indicates whether this option is checked.
*/
checked: Boolean,
/**
* Indicates whether this option is disabled.
*/
disabled: Boolean,
/**
* Option label.
*/
label: { type: String, required: true },
},
computed: {
/**
* The CSS class names for the input.
*/
inputClassNames(): string {
let classNames = ""
if (this.disabled) classNames += " disabled"
return classNames
},
},
methods: {
/**
* Handles the click event on the checkbox button.
* @param event The fired event.
*/
handleClick(event: Event): void {
if (this.disabled) event.preventDefault()
},
},
})
</script>

109 changes: 109 additions & 0 deletions src/components/slds-checkbox-group/slds-checkbox-group.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<template>
<slds-form-element
:errors="errors"
:help="help"
:label="label"
:required="required"
:stacked="stacked"
:tooltip="tooltip"
>

<!-- Tooltip -->
<template v-if="$slots.tooltip" #tooltip>
<slot name="tooltip"/>
</template>

<!-- Default slot -->
<template #default>
<slot>
<slds-checkbox-group-option
v-for="option in options"
:key="option.value"
:checked="modelValue.includes(option.value)"
:label="option.label"
:disabled="option.disabled || disabled"
@click="handleClick($event, option)"
/>
</slot>
</template>

<!-- Inline help -->
<template #help>
<slot name="help"/>
</template>

<!-- Error messages -->
<template #error>
<slot name="error"/>
</template>

</slds-form-element>
</template>

<script lang="ts">
import SldsFormElement from "../slds-form-element/slds-form-element.vue"
import SldsCheckboxGroupOption from "../slds-checkbox-group/slds-checkbox-group-option.vue"
import { defineComponent, type PropType } from "vue"
import { type ValidationError } from "../slds-form-element/validation-error"
import { type CheckboxGroupOption } from "./checkbox-group-option"
import { EVENTS } from "../../constants"
export default defineComponent ({
name: "SldsCheckboxGroup",
components: { SldsCheckboxGroupOption, SldsFormElement },
props: {
disabled: Boolean,
/**
* Array of error objects from vuelidate.
*/
errors: { type: Array as PropType<ValidationError[]>, default: () => [] as ValidationError[] },
/**
* Inline help text.
* When using the help slot this prop is ignored.
*/
help: String,
label: String,
modelValue: { type: Array as PropType<string[]>, default: () => [] },
options: { type: Array as PropType<CheckboxGroupOption[]>, default: () => [] },
required: Boolean,
/**
* Indicates whether the input is stacked among other inputs.
*/
stacked: Boolean,
tooltip: String,
},
methods: {
/**
* Handles the click event on the checkbox group.
* @param event The fired event.
* @param option The clicked option.
*/
handleClick(event: Event, option: CheckboxGroupOption): void {
if (!event || this.disabled || option.disabled) {
event.preventDefault()
return
}
const value = [...this.modelValue]
const index = value.indexOf(option.value)
if (index === -1) value.push(option.value)
else value.splice(index, 1)
this.$emit(EVENTS.UPDATE_MODEL_VALUE, value)
},
},
})
</script>

6 changes: 4 additions & 2 deletions src/components/slds-checkbox-toggle/slds-checkbox-toggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
<script lang="ts">
import SldsFormElement from "../slds-form-element/slds-form-element.vue"
import { EVENTS } from "../../constants"
import { defineComponent } from "vue"
import { defineComponent, type PropType } from "vue"
import type { ValidationError } from "../slds-form-element/validation-error"
export default defineComponent({
name: "SldsCheckboxToggle",
Expand All @@ -79,10 +80,11 @@ export default defineComponent({
inheritAttrs: false,
props: {
/**
* Array of error objects from vuelidate.
*/
errors: Array,
errors: { type: Array as PropType<ValidationError[]>, default: () => [] as ValidationError[] },
disabled: Boolean,
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./constants"

export type { AccordionSection } from "./components/slds-accordion/accordion-section"
export type { Breadcrumb } from "./components/slds-breadcrumbs/breadcrumb"
export type { CheckboxGroupOption } from "./components/slds-checkbox-group/checkbox-group-option"
export type { CheckboxButtonGroupOption } from "./components/slds-checkbox-button-group/checkbox-button-group-option"
export type { DataTableColumn } from "./components/slds-data-table/data-table-column"
export type { DropdownOption } from "./components/slds-dropdown/dropdown-option"
Expand Down

0 comments on commit 6b1acc9

Please sign in to comment.