Skip to content

Commit 8d9582a

Browse files
authored
Merge pull request #301 from topcoder-platform/GAME-108_bjcs
GAME-108 Support Checkboxes -> fix/GAME-108
2 parents 03cd149 + 460cd29 commit 8d9582a

File tree

9 files changed

+64
-105
lines changed

9 files changed

+64
-105
lines changed

src-ts/lib/form/form-functions/form.functions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ function handleFieldEvent<T>(input: HTMLInputElement | HTMLTextAreaElement, inpu
121121
inputDef.touched = true
122122

123123
// set the def value
124-
inputDef.value = input.value
124+
if (input.type === 'checkbox') {
125+
const checkbox: HTMLInputElement = input as HTMLInputElement
126+
inputDef.value = checkbox.checked
127+
inputDef.checked = checkbox.checked
128+
} else {
129+
inputDef.value = input.value
130+
}
125131

126132
// now let's validate the field
127133
const formElements: HTMLFormControlsCollection = (input.form as HTMLFormElement).elements

src-ts/lib/form/form-groups/FormGroups.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FormInputModel } from '../form-input.model'
66

77
import { FormCardSet } from './form-card-set'
88
import FormGroupItem from './form-group-item/FormGroupItem'
9-
import { InputCheckbox, InputRating, InputText, InputTextarea } from './form-input'
9+
import { InputRating, InputText, InputTextarea } from './form-input'
1010
import { FormInputRow } from './form-input-row'
1111
import { InputTextTypes } from './form-input/input-text/InputText'
1212
import FormRadio from './form-radio'
@@ -23,12 +23,13 @@ const FormGroups: (props: FormGroupsProps) => JSX.Element = (props: FormGroupsPr
2323

2424
const { formDef, onBlur, onChange }: FormGroupsProps = props
2525

26-
const getTabIndex: (input: FormInputModel, index: number) => number = (input, index) => {
26+
function getTabIndex(input: FormInputModel, index: number): number {
2727
const tabIndex: number = input.notTabbable ? -1 : index + 1 + (formDef.tabIndexStart || 0)
2828
return tabIndex
2929
}
3030

31-
const renderInputField: (input: FormInputModel, index: number) => JSX.Element | undefined = (input, index) => {
31+
function renderInputField(input: FormInputModel, index: number): JSX.Element | undefined {
32+
3233
const tabIndex: number = getTabIndex(input, index)
3334

3435
let inputElement: JSX.Element
@@ -40,7 +41,7 @@ const FormGroups: (props: FormGroupsProps) => JSX.Element = (props: FormGroupsPr
4041
{...input}
4142
onChange={onChange}
4243
tabIndex={tabIndex}
43-
value={input.value}
44+
value={input.value as number | undefined}
4445
/>
4546
)
4647
break
@@ -51,17 +52,19 @@ const FormGroups: (props: FormGroupsProps) => JSX.Element = (props: FormGroupsPr
5152
onBlur={onBlur}
5253
onChange={onChange}
5354
tabIndex={tabIndex}
54-
value={input.value}
55+
value={input.value as string | undefined}
5556
/>
5657
)
5758
break
5859
case 'checkbox':
5960
inputElement = (
60-
<InputCheckbox
61+
<InputText
6162
{...input}
63+
checked={!!input.value}
64+
onBlur={onBlur}
6265
onChange={onChange}
6366
tabIndex={tabIndex}
64-
type={'checkbox'}
67+
type='checkbox'
6568
/>
6669
)
6770
break
@@ -91,7 +94,7 @@ const FormGroups: (props: FormGroupsProps) => JSX.Element = (props: FormGroupsPr
9194
onChange={onChange}
9295
tabIndex={tabIndex}
9396
type={input.type as InputTextTypes || 'text'}
94-
value={input.value}
97+
value={input.value as string | undefined}
9598
/>
9699
)
97100
break
@@ -108,9 +111,18 @@ const FormGroups: (props: FormGroupsProps) => JSX.Element = (props: FormGroupsPr
108111
)
109112
}
110113

111-
const formGroups: Array<JSX.Element | undefined> = formDef?.groups?.map((element: FormGroup, index: number) => {
112-
return <FormGroupItem key={`element-${index}`} group={element} renderFormInput={renderInputField} totalGroupCount={formDef.groups?.length || 0} />
113-
}) || []
114+
const formGroups: Array<JSX.Element | undefined> = formDef?.groups
115+
?.map((element: FormGroup, index: number) => {
116+
return (
117+
<FormGroupItem
118+
key={`element-${index}`}
119+
group={element}
120+
renderFormInput={renderInputField}
121+
totalGroupCount={formDef.groups?.length || 0}
122+
/>
123+
)
124+
})
125+
|| []
114126

115127
return (
116128
<div className={styles['form-groups']}>

src-ts/lib/form/form-groups/form-input/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './input-checkbox'
21
export * from './form-input-autcomplete-option.enum'
32
export * from './input-rating'
43
export * from './input-text'

src-ts/lib/form/form-groups/form-input/input-checkbox/InputCheckbox.module.scss

Lines changed: 0 additions & 32 deletions
This file was deleted.

src-ts/lib/form/form-groups/form-input/input-checkbox/InputCheckbox.tsx

Lines changed: 0 additions & 53 deletions
This file was deleted.

src-ts/lib/form/form-groups/form-input/input-checkbox/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src-ts/lib/form/form-groups/form-input/input-text/InputText.module.scss

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,32 @@
3030
}
3131

3232
&.checkbox {
33-
& {
34-
width: 20px;
35-
height: 20px;
33+
@extend .body-small;
34+
color: $black-60;
35+
box-sizing: border-box;
36+
border: 0;
37+
width: 100%;
38+
padding: 0;
39+
margin: 0;
40+
height: auto;
41+
border-radius: 0;
42+
43+
&:focus {
44+
box-shadow: none;
45+
border: none;
46+
outline: none;
47+
color: $black-100;
48+
}
49+
50+
&:disabled {
51+
background-color: $black-10;
52+
}
53+
54+
&.checkbox {
55+
& {
56+
width: 20px;
57+
height: 20px;
58+
}
3659
}
3760
}
3861
}

src-ts/lib/form/form-groups/form-input/input-text/InputText.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type InputTextTypes = 'checkbox' | 'password' | 'text'
1010

1111
interface InputTextProps {
1212
readonly autocomplete?: FormInputAutocompleteOption
13+
readonly checked?: boolean
1314
readonly className?: string
1415
readonly dirty?: boolean
1516
readonly disabled?: boolean
@@ -24,11 +25,15 @@ interface InputTextProps {
2425
readonly spellCheck?: boolean
2526
readonly tabIndex: number
2627
readonly type: InputTextTypes
27-
readonly value?: string | number
28+
readonly value?: string | number | boolean
2829
}
2930

3031
const InputText: FC<InputTextProps> = (props: InputTextProps) => {
3132

33+
const defaultValue: string | number | undefined = props.type === 'checkbox' && !!props.checked
34+
? 'on'
35+
: props.value as string | number | undefined
36+
3237
return (
3338
<InputWrapper
3439
{...props}
@@ -40,7 +45,7 @@ const InputText: FC<InputTextProps> = (props: InputTextProps) => {
4045
<input
4146
autoComplete={props.autocomplete}
4247
className={cn(styles['form-input-text'], styles[props.type])}
43-
defaultValue={props.value}
48+
defaultValue={defaultValue}
4449
disabled={!!props.disabled}
4550
onBlur={props.onBlur}
4651
onChange={props.onChange}

src-ts/lib/form/form-input.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export interface FormInputModel {
4141
touched?: boolean
4242
readonly type: 'card-set' | 'checkbox' | 'password' | 'radio' | 'rating' | 'text' | 'textarea'
4343
readonly validators?: ReadonlyArray<ValidatorFn>
44-
value?: string
44+
value?: string | boolean
4545
}

0 commit comments

Comments
 (0)