-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 Added SQFormRadioButtonGroup component
Added SQFormRadioButtonGroup component to allow for a radio button input within the context of SQForm ✅ Closes: #64
- Loading branch information
1 parent
755d6e9
commit c043924
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Grid from '@material-ui/core/Grid'; | ||
import RadioGroup from '@material-ui/core/RadioGroup'; | ||
import FormHelperText from '@material-ui/core/FormHelperText'; | ||
import InputLabel from '@material-ui/core/InputLabel'; | ||
import {SQFormRadioButtonGroupItem} from '../../../src'; | ||
import {useForm} from './useForm'; | ||
|
||
function SQFormRadioButtonGroup({ | ||
name, | ||
onChange = () => {}, | ||
isRequired = false, | ||
size = 'auto', | ||
groupLabel, | ||
children | ||
}) { | ||
const { | ||
formikField: {field}, | ||
fieldHelpers: {handleChange, HelperTextComponent} | ||
} = useForm({ | ||
name, | ||
isRequired, | ||
onChange | ||
}); | ||
|
||
const childrenToRadioOptionComponents = () => { | ||
return children.map(radioOption => { | ||
return ( | ||
<SQFormRadioButtonGroupItem | ||
label={radioOption.label} | ||
value={radioOption.value} | ||
key={`SQFormRadioButtonGroupItem_${radioOption.value}`} | ||
/> | ||
); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Grid item sm={size}> | ||
<InputLabel id={groupLabel.toLowerCase()}>{groupLabel}</InputLabel> | ||
<RadioGroup | ||
value={field.value} | ||
aria-label={`SQFormRadioButtonGroup_${name}`} | ||
name={name} | ||
onChange={handleChange} | ||
> | ||
{childrenToRadioOptionComponents()} | ||
</RadioGroup> | ||
<FormHelperText required={isRequired}> | ||
{HelperTextComponent} | ||
</FormHelperText> | ||
</Grid> | ||
); | ||
} | ||
|
||
SQFormRadioButtonGroup.propTypes = { | ||
/** Name of the radio group */ | ||
name: PropTypes.string.isRequired, | ||
/** Function to call on value change */ | ||
onChange: PropTypes.func, | ||
/** Whether this radio selection is required */ | ||
isRequired: PropTypes.bool, | ||
/** Size of the input given full-width is 12. */ | ||
size: PropTypes.oneOf(['auto', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), | ||
/** Label to display above the group */ | ||
groupLabel: PropTypes.string.isRequired, | ||
/** Children must be an array of objects with radio button label and value information */ | ||
children: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
label: PropTypes.string.isRequired, | ||
value: PropTypes.any.isRequired | ||
}).isRequired | ||
).isRequired | ||
}; | ||
|
||
export default SQFormRadioButtonGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Radio from '@material-ui/core/Radio'; | ||
import FormControlLabel from '@material-ui/core/FormControlLabel'; | ||
|
||
function SQFormRadioButtonGroupItem({value, label}) { | ||
return <FormControlLabel value={value} label={label} control={<Radio />} />; | ||
} | ||
|
||
SQFormRadioButtonGroupItem.propTypes = { | ||
/** Value of the radio button */ | ||
value: PropTypes.any.isRequired, | ||
/** Label for the radio button */ | ||
label: PropTypes.string.isRequired | ||
}; | ||
|
||
export default SQFormRadioButtonGroupItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react'; | ||
import {SQForm} from 'scplus-shared-components'; | ||
import * as Yup from 'yup'; | ||
import Card from '@material-ui/core/Card'; | ||
import Grid from '@material-ui/core/Grid'; | ||
import {SQFormButton, SQFormRadioButtonGroup} from '../src'; | ||
|
||
export default { | ||
title: 'SQFormRadioGroup' | ||
}; | ||
|
||
export const radioGroup = () => { | ||
const initialValues = { | ||
testGroup: '' | ||
}; | ||
|
||
const validationSchema = { | ||
testGroup: Yup.string().required('Required') | ||
}; | ||
|
||
const onSubmit = formValues => { | ||
const selectedRadioValue = formValues.testGroup; | ||
window.alert(`Selected Radio Value: ${selectedRadioValue}`); | ||
}; | ||
|
||
const radioOptions = [ | ||
{label: 'Test1', value: 'test1'}, | ||
{label: 'Test2', value: 'test2'}, | ||
{label: 'Test3', value: 'test3'} | ||
]; | ||
|
||
return ( | ||
<Card raised style={{padding: 16}}> | ||
<SQForm | ||
initialValues={initialValues} | ||
onSubmit={onSubmit} | ||
validationSchema={validationSchema} | ||
> | ||
<SQFormRadioButtonGroup | ||
name="testGroup" | ||
groupLabel="Radio Group" | ||
isRequired | ||
> | ||
{radioOptions} | ||
</SQFormRadioButtonGroup> | ||
<Grid item sm={12}> | ||
<SQFormButton>Submit</SQFormButton> | ||
</Grid> | ||
</SQForm> | ||
</Card> | ||
); | ||
}; |