Commit fb82226 1 parent bd9d1ca commit fb82226 Copy full SHA for fb82226
File tree 4 files changed +69
-22
lines changed
demo-element-plus/src/viewer
packages/vue/src/forms/composables/fields
4 files changed +69
-22
lines changed Original file line number Diff line number Diff line change 20
20
import { FCheckbox , useForm } from ' @fancy-crud/vue'
21
21
import { Bus , ResetFieldsByFormIdCommand } from ' @fancy-crud/core'
22
22
23
- // const options = [
24
- // 'Option 1',
25
- // 'Option 2',
26
- // 'Option 3',
27
- // ]
28
-
29
23
const options = [
30
24
{ label: ' Option 1' , value: ' option-1' },
31
25
{ label: ' Option 2' , value: ' option-2' },
@@ -37,15 +31,35 @@ const bus = new Bus()
37
31
const form = useForm ({
38
32
id: ' checkbox-field' ,
39
33
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
+ },
40
40
checkbox: {
41
41
label: ' Checkbox' ,
42
42
type: ' checkbox' ,
43
+ inRow: true ,
44
+ options: [true ],
45
+ },
46
+ artists: {
47
+ label: ' Artists' ,
48
+ type: ' checkbox' ,
43
49
multiple: true ,
44
- modelValue: [],
45
50
optionLabel: ' label' ,
46
- // optionValue: 'value',
51
+ optionValue: ' value' ,
47
52
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' ],
49
63
},
50
64
},
51
65
settings: {
Original file line number Diff line number Diff line change 20
20
import { FCheckbox , useForm } from ' @fancy-crud/vue'
21
21
import { Bus , ResetFieldsByFormIdCommand } from ' @fancy-crud/core'
22
22
23
- // const options = [
24
- // 'Option 1',
25
- // 'Option 2',
26
- // 'Option 3',
27
- // ]
28
-
29
23
const options = [
30
24
{ label: ' Option 1' , value: ' option-1' },
31
25
{ label: ' Option 2' , value: ' option-2' },
@@ -37,15 +31,35 @@ const bus = new Bus()
37
31
const form = useForm ({
38
32
id: ' checkbox-field' ,
39
33
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
+ },
40
40
checkbox: {
41
41
label: ' Checkbox' ,
42
42
type: ' checkbox' ,
43
+ inRow: true ,
44
+ options: [true ],
45
+ },
46
+ artists: {
47
+ label: ' Artists' ,
48
+ type: ' checkbox' ,
43
49
multiple: true ,
44
- modelValue: [],
45
50
optionLabel: ' label' ,
46
51
optionValue: ' value' ,
47
52
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' ],
49
63
},
50
64
},
51
65
settings: {
Original file line number Diff line number Diff line change @@ -12,7 +12,15 @@ export function useCheckboxField<T = any>(props: DefaultProps & { field: Normali
12
12
13
13
const { validate } = useRules ( fields , ruleConfigStore . searchById ( props . formId ) )
14
14
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 )
16
24
const { hintText, hasFieldErrors } = useHintText ( props )
17
25
18
26
const inRowDisplay = computed ( ( ) => {
Original file line number Diff line number Diff line change @@ -5,15 +5,26 @@ interface Props {
5
5
options ?: any [ ]
6
6
optionValue ?: string
7
7
optionLabel ?: string
8
+ label ?: string
8
9
}
9
10
}
10
11
11
- export function useOptions ( props : Props ) {
12
+ export function useOptions ( props : Props , isABooleanCheckbox : boolean ) {
12
13
const options = computed ( ( ) => {
13
- const options = props . field . options || [ ]
14
+ let options = props . field . options || [ ]
15
+ let defaultOptionValue = ''
16
+ let defaultOptionLabel = ''
14
17
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
17
28
18
29
return options . reduce ( ( previousValue : Options , currentValue : any ) : Options => {
19
30
const value : unknown = currentValue [ optionValue ] || currentValue
You can’t perform that action at this time.
0 commit comments