Skip to content

Commit fb82226

Browse files
committed
fix: display checkbox for boolean values
1 parent bd9d1ca commit fb82226

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

demo-element-plus/src/viewer/FCheckboxViewer.vue

+23-9
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
import { FCheckbox, useForm } from '@fancy-crud/vue'
2121
import { Bus, ResetFieldsByFormIdCommand } from '@fancy-crud/core'
2222
23-
// const options = [
24-
// 'Option 1',
25-
// 'Option 2',
26-
// 'Option 3',
27-
// ]
28-
2923
const options = [
3024
{ label: 'Option 1', value: 'option-1' },
3125
{ label: 'Option 2', value: 'option-2' },
@@ -37,15 +31,35 @@ const bus = new Bus()
3731
const form = useForm({
3832
id: 'checkbox-field',
3933
fields: {
34+
terms: {
35+
label: 'By clicking this checkbox, you agree to our terms and conditions',
36+
type: 'checkbox',
37+
inRow: true,
38+
modelValue: false,
39+
},
4040
checkbox: {
4141
label: 'Checkbox',
4242
type: 'checkbox',
43+
inRow: true,
44+
options: [true],
45+
},
46+
artists: {
47+
label: 'Artists',
48+
type: 'checkbox',
4349
multiple: true,
44-
modelValue: [],
4550
optionLabel: 'label',
46-
// optionValue: 'value',
51+
optionValue: 'value',
4752
options,
48-
inRow: true,
53+
modelValue: ['option-1'],
54+
},
55+
artists2: {
56+
label: 'Artists 2',
57+
type: 'checkbox',
58+
multiple: true,
59+
optionLabel: 'name',
60+
optionValue: 'id',
61+
url: 'artists/',
62+
modelValue: ['option-1'],
4963
},
5064
},
5165
settings: {

demo-quasar/src/viewer/FCheckboxViewer.vue

+22-8
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
import { FCheckbox, useForm } from '@fancy-crud/vue'
2121
import { Bus, ResetFieldsByFormIdCommand } from '@fancy-crud/core'
2222
23-
// const options = [
24-
// 'Option 1',
25-
// 'Option 2',
26-
// 'Option 3',
27-
// ]
28-
2923
const options = [
3024
{ label: 'Option 1', value: 'option-1' },
3125
{ label: 'Option 2', value: 'option-2' },
@@ -37,15 +31,35 @@ const bus = new Bus()
3731
const form = useForm({
3832
id: 'checkbox-field',
3933
fields: {
34+
terms: {
35+
label: 'By clicking this checkbox, you agree to our terms and conditions',
36+
type: 'checkbox',
37+
inRow: true,
38+
modelValue: false,
39+
},
4040
checkbox: {
4141
label: 'Checkbox',
4242
type: 'checkbox',
43+
inRow: true,
44+
options: [true],
45+
},
46+
artists: {
47+
label: 'Artists',
48+
type: 'checkbox',
4349
multiple: true,
44-
modelValue: [],
4550
optionLabel: 'label',
4651
optionValue: 'value',
4752
options,
48-
inRow: false,
53+
modelValue: ['option-1'],
54+
},
55+
artists2: {
56+
label: 'Artists 2',
57+
type: 'checkbox',
58+
multiple: true,
59+
optionLabel: 'name',
60+
optionValue: 'id',
61+
url: 'artists/',
62+
modelValue: ['option-1'],
4963
},
5064
},
5165
settings: {

packages/vue/src/forms/composables/fields/checkbox.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ export function useCheckboxField<T = any>(props: DefaultProps & { field: Normali
1212

1313
const { validate } = useRules(fields, ruleConfigStore.searchById(props.formId))
1414

15-
const { options } = useOptions(props)
15+
const isABooleanCheckbox = (
16+
!Array.isArray(props.field.options)
17+
&& !props.field.multiple
18+
&& !props.field.url
19+
&& !props.field.optionLabel
20+
&& !props.field.optionValue
21+
)
22+
23+
const { options } = useOptions(props, isABooleanCheckbox)
1624
const { hintText, hasFieldErrors } = useHintText(props)
1725

1826
const inRowDisplay = computed(() => {

packages/vue/src/forms/composables/fields/utils/options.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ interface Props {
55
options?: any[]
66
optionValue?: string
77
optionLabel?: string
8+
label?: string
89
}
910
}
1011

11-
export function useOptions(props: Props) {
12+
export function useOptions(props: Props, isABooleanCheckbox: boolean) {
1213
const options = computed(() => {
13-
const options = props.field.options || []
14+
let options = props.field.options || []
15+
let defaultOptionValue = ''
16+
let defaultOptionLabel = ''
1417

15-
const optionValue = props.field.optionValue || ''
16-
const optionLabel = props.field.optionLabel || ''
18+
if (isABooleanCheckbox) {
19+
options = [
20+
{ label: props.field.label, value: true },
21+
]
22+
defaultOptionLabel = 'label'
23+
defaultOptionValue = 'value'
24+
}
25+
26+
const optionValue = props.field.optionValue || defaultOptionValue
27+
const optionLabel = props.field.optionLabel || defaultOptionLabel
1728

1829
return options.reduce((previousValue: Options, currentValue: any): Options => {
1930
const value: unknown = currentValue[optionValue] || currentValue

0 commit comments

Comments
 (0)