Refactor radio group buttons
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Eric NGUYEN 2022-08-16 13:46:54 +02:00
parent 9ce184df26
commit 6d56ea49cf
4 changed files with 80 additions and 68 deletions

View file

@ -2,6 +2,7 @@ import * as React from 'react';
import { XPositionReference } from '../../Enums/XPositionReference'; import { XPositionReference } from '../../Enums/XPositionReference';
import IProperties from '../../Interfaces/IProperties'; import IProperties from '../../Interfaces/IProperties';
import { InputGroup } from '../InputGroup/InputGroup'; import { InputGroup } from '../InputGroup/InputGroup';
import { RadioGroupButtons } from '../RadioGroupButtons/RadioGroupButtons';
interface IDynamicFormProps { interface IDynamicFormProps {
properties: IProperties properties: IProperties
@ -103,41 +104,17 @@ const DynamicForm: React.FunctionComponent<IDynamicFormProps> = (props) => {
checked={props.properties.isAnchor} checked={props.properties.isAnchor}
onChange={(event) => props.onChange('isAnchor', event.target.checked)} onChange={(event) => props.onChange('isAnchor', event.target.checked)}
/> />
<label> <RadioGroupButtons
Horizontal alignment
</label>
<div id='XPositionReference'>
<label>
<input
type='radio'
name='XPositionReference' name='XPositionReference'
value={XPositionReference.Left} value={props.properties.XPositionReference.toString()}
checked={props.properties.XPositionReference === XPositionReference.Left} labelText='Horizontal alignment'
onChange={(event) => props.onChange('XPositionReference', (event.target as HTMLInputElement).value)} inputGroups={[
{ text: 'Left', value: XPositionReference.Left.toString() },
{ text: 'Center', value: XPositionReference.Center.toString() },
{ text: 'Right', value: XPositionReference.Right.toString() }
]}
onChange={(event) => props.onChange('XPositionReference', event.target.value)}
/> />
Left
</label>
<label>
<input
type='radio'
name='XPositionReference'
value={XPositionReference.Center}
checked={props.properties.XPositionReference === XPositionReference.Center}
onChange={(event) => props.onChange('XPositionReference', (event.target as HTMLInputElement).value)}
/>
Center
</label>
<label>
<input
type='radio'
name='XPositionReference'
value={XPositionReference.Right}
checked={props.properties.XPositionReference === XPositionReference.Right}
onChange={(event) => props.onChange('XPositionReference', (event.target as HTMLInputElement).value)}
/>
Right
</label>
</div>
{ getCSSInputs(props.properties, props.onChange) } { getCSSInputs(props.properties, props.onChange) }
</div> </div>
); );

View file

@ -2,6 +2,7 @@ import * as React from 'react';
import { XPositionReference } from '../../Enums/XPositionReference'; import { XPositionReference } from '../../Enums/XPositionReference';
import IProperties from '../../Interfaces/IProperties'; import IProperties from '../../Interfaces/IProperties';
import { InputGroup } from '../InputGroup/InputGroup'; import { InputGroup } from '../InputGroup/InputGroup';
import { RadioGroupButtons } from '../RadioGroupButtons/RadioGroupButtons';
interface IStaticFormProps { interface IStaticFormProps {
properties: IProperties properties: IProperties
@ -97,39 +98,16 @@ const StaticForm: React.FunctionComponent<IStaticFormProps> = (props) => {
type='checkbox' type='checkbox'
defaultChecked={props.properties.isAnchor} defaultChecked={props.properties.isAnchor}
/> />
<label> <RadioGroupButtons
Horizontal alignment
</label>
<div id='XPositionReference'>
<label>
<input
type='radio'
name='XPositionReference' name='XPositionReference'
value={XPositionReference.Left} defaultValue={props.properties.XPositionReference.toString()}
defaultChecked={props.properties.XPositionReference === XPositionReference.Left} labelText='Horizontal alignment'
inputGroups={[
{ text: 'Left', value: XPositionReference.Left.toString() },
{ text: 'Center', value: XPositionReference.Center.toString() },
{ text: 'Right', value: XPositionReference.Right.toString() }
]}
/> />
Left
</label>
<label>
<input
type='radio'
name='XPositionReference'
value={XPositionReference.Center}
defaultChecked={props.properties.XPositionReference === XPositionReference.Center}
/>
Center
</label>
<label>
<input
type='radio'
name='XPositionReference'
value={XPositionReference.Right}
defaultChecked={props.properties.XPositionReference === XPositionReference.Right}
/>
Right
</label>
</div>
{ getCSSInputs(props.properties) } { getCSSInputs(props.properties) }
</div> </div>
</form>); </form>);

View file

@ -0,0 +1,53 @@
import * as React from 'react';
import { IInputGroup } from '../../Interfaces/IInputGroup';
interface IRadioGroupButtonsProps {
name: string
value?: string
defaultValue?: string
labelText: string
inputGroups: IInputGroup[]
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void
}
export const RadioGroupButtons: React.FunctionComponent<IRadioGroupButtonsProps> = (props) => {
let inputGroups;
if (props.value !== undefined) {
inputGroups = props.inputGroups.map((inputGroup) => (
<label key={inputGroup.value}>
<input
type='radio'
name={props.name}
value={inputGroup.value}
checked={props.value === inputGroup.value}
onChange={props.onChange}
/>
{inputGroup.text}
</label>
));
} else {
inputGroups = props.inputGroups.map((inputGroup) => (
<label key={inputGroup.value}>
<input
type='radio'
name={props.name}
value={inputGroup.value}
defaultChecked={props.defaultValue === inputGroup.value}
onChange={props.onChange}
/>
{inputGroup.text}
</label>
));
}
return (
<>
<label>
{props.labelText}
</label>
<div id='XPositionReference'>
{ inputGroups }
</div>
</>
);
};

View file

@ -0,0 +1,4 @@
export interface IInputGroup {
text: string
value: string
}