Merge pull request #3250 from benjiwheeler/join-flow-gender-custom-focus

Join flow gender custom focus
This commit is contained in:
Benjamin Wheeler 2019-08-21 00:33:15 +02:00 committed by GitHub
commit 5c604919b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 14 deletions

View file

@ -11,6 +11,7 @@ require('./formik-input.scss');
const FormikInput = ({ const FormikInput = ({
className, className,
error, error,
setRef,
toolTip, toolTip,
validationClassName, validationClassName,
wrapperClassName, wrapperClassName,
@ -30,6 +31,8 @@ const FormikInput = ({
{fail: error}, {fail: error},
className className
)} )}
/* formik uses "innerRef" to return the actual input element */
innerRef={setRef}
{...props} {...props}
/> />
{error ? ( {error ? (
@ -48,10 +51,10 @@ const FormikInput = ({
</div> </div>
); );
FormikInput.propTypes = { FormikInput.propTypes = {
className: PropTypes.string, className: PropTypes.string,
error: PropTypes.string, error: PropTypes.string,
setRef: PropTypes.func,
toolTip: PropTypes.string, toolTip: PropTypes.string,
type: PropTypes.string, type: PropTypes.string,
validationClassName: PropTypes.string, validationClassName: PropTypes.string,

View file

@ -10,24 +10,26 @@ require('./formik-radio-button.scss');
require('../forms/row.scss'); require('../forms/row.scss');
const FormikRadioButtonSubComponent = ({ const FormikRadioButtonSubComponent = ({
buttonValue,
children, children,
className, className,
field, field, // field.value is the current selected value of the entire radio group
id,
label, label,
labelClassName, labelClassName,
value,
...props ...props
}) => ( }) => (
<React.Fragment> <React.Fragment>
<input <input
checked={buttonValue === field.value} checked={value === field.value}
className={classNames( className={classNames(
'formik-radio-button', 'formik-radio-button',
className className
)} )}
id={id}
name={field.name} name={field.name}
type="radio" type="radio"
value={buttonValue} value={value}
onBlur={field.onBlur} /* eslint-disable-line react/jsx-handler-names */ onBlur={field.onBlur} /* eslint-disable-line react/jsx-handler-names */
onChange={field.onChange} /* eslint-disable-line react/jsx-handler-names */ onChange={field.onChange} /* eslint-disable-line react/jsx-handler-names */
{...props} {...props}
@ -39,7 +41,7 @@ const FormikRadioButtonSubComponent = ({
'formik-radio-label', 'formik-radio-label',
labelClassName labelClassName
)} )}
htmlFor={buttonValue} htmlFor={id}
> >
{label} {label}
</label> </label>
@ -49,7 +51,6 @@ const FormikRadioButtonSubComponent = ({
); );
FormikRadioButtonSubComponent.propTypes = { FormikRadioButtonSubComponent.propTypes = {
buttonValue: PropTypes.string,
children: PropTypes.node, children: PropTypes.node,
className: PropTypes.string, className: PropTypes.string,
field: PropTypes.shape({ field: PropTypes.shape({
@ -58,6 +59,7 @@ FormikRadioButtonSubComponent.propTypes = {
onChange: PropTypes.function, onChange: PropTypes.function,
value: PropTypes.string value: PropTypes.string
}), }),
id: PropTypes.string,
label: PropTypes.string, label: PropTypes.string,
labelClassName: PropTypes.string, labelClassName: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) value: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
@ -65,27 +67,31 @@ FormikRadioButtonSubComponent.propTypes = {
const FormikRadioButton = ({ const FormikRadioButton = ({
buttonValue,
className, className,
id,
isCustomInput, isCustomInput,
label, label,
name, name,
onSetCustom, onSetCustom,
onSetCustomRef,
value,
...props ...props
}) => ( }) => (
<Field <Field
buttonValue={buttonValue}
className={className} className={className}
component={FormikRadioButtonSubComponent} component={FormikRadioButtonSubComponent}
id={id}
label={label} label={label}
labelClassName={isCustomInput ? 'formik-radio-label-other' : ''} labelClassName={isCustomInput ? 'formik-radio-label-other' : ''}
name={name} name={name}
value={value}
{...props} {...props}
> >
{isCustomInput && ( {isCustomInput && (
<FormikInput <FormikInput
className="formik-radio-input" className="formik-radio-input"
name="custom" name="custom"
setRef={onSetCustomRef}
wrapperClassName="formik-radio-input-wrapper" wrapperClassName="formik-radio-input-wrapper"
/* eslint-disable react/jsx-no-bind */ /* eslint-disable react/jsx-no-bind */
onChange={event => onSetCustom(event.target.value)} onChange={event => onSetCustom(event.target.value)}
@ -97,12 +103,13 @@ const FormikRadioButton = ({
); );
FormikRadioButton.propTypes = { FormikRadioButton.propTypes = {
buttonValue: PropTypes.string,
className: PropTypes.string, className: PropTypes.string,
id: PropTypes.string,
isCustomInput: PropTypes.bool, isCustomInput: PropTypes.bool,
label: PropTypes.string, label: PropTypes.string,
name: PropTypes.string, name: PropTypes.string,
onSetCustom: PropTypes.func, onSetCustom: PropTypes.func,
onSetCustomRef: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]) value: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
}; };

View file

@ -11,10 +11,12 @@ const JoinFlowStep = require('./join-flow-step.jsx');
require('./join-flow-steps.scss'); require('./join-flow-steps.scss');
const GenderOption = ({ const GenderOption = ({
id,
label, label,
onSetFieldValue, onSetFieldValue,
selectedValue, selectedValue,
value value,
...props
}) => ( }) => (
<div <div
className={classNames( className={classNames(
@ -29,17 +31,20 @@ const GenderOption = ({
/* eslint-enable react/jsx-no-bind */ /* eslint-enable react/jsx-no-bind */
> >
<FormikRadioButton <FormikRadioButton
buttonValue={value}
className={classNames( className={classNames(
'join-flow-radio' 'join-flow-radio'
)} )}
id={id}
label={label} label={label}
name="gender" name="gender"
value={value}
{...props}
/> />
</div> </div>
); );
GenderOption.propTypes = { GenderOption.propTypes = {
id: PropTypes.string,
label: PropTypes.string, label: PropTypes.string,
onSetFieldValue: PropTypes.func, onSetFieldValue: PropTypes.func,
selectedValue: PropTypes.string, selectedValue: PropTypes.string,
@ -50,9 +55,13 @@ class GenderStep extends React.Component {
constructor (props) { constructor (props) {
super(props); super(props);
bindAll(this, [ bindAll(this, [
'handleSetCustomRef',
'handleValidSubmit' 'handleValidSubmit'
]); ]);
} }
handleSetCustomRef (customInputRef) {
this.customInput = customInputRef;
}
handleValidSubmit (formData, formikBag) { handleValidSubmit (formData, formikBag) {
formikBag.setSubmitting(false); formikBag.setSubmitting(false);
if (!formData.gender || formData.gender === 'null') { if (!formData.gender || formData.gender === 'null') {
@ -89,12 +98,14 @@ class GenderStep extends React.Component {
onSubmit={handleSubmit} onSubmit={handleSubmit}
> >
<GenderOption <GenderOption
id="GenderRadioOptionFemale"
label={this.props.intl.formatMessage({id: 'general.female'})} label={this.props.intl.formatMessage({id: 'general.female'})}
selectedValue={values.gender} selectedValue={values.gender}
value="Female" value="Female"
onSetFieldValue={setFieldValue} onSetFieldValue={setFieldValue}
/> />
<GenderOption <GenderOption
id="GenderRadioOptionMale"
label={this.props.intl.formatMessage({id: 'general.male'})} label={this.props.intl.formatMessage({id: 'general.male'})}
selectedValue={values.gender} selectedValue={values.gender}
value="Male" value="Male"
@ -115,26 +126,32 @@ class GenderStep extends React.Component {
{'gender-radio-row-selected': (values.gender === values.custom)} {'gender-radio-row-selected': (values.gender === values.custom)}
)} )}
/* eslint-disable react/jsx-no-bind */ /* eslint-disable react/jsx-no-bind */
onClick={() => setFieldValue('gender', values.custom, false)} onClick={() => {
setFieldValue('gender', values.custom, false);
if (this.customInput) this.customInput.focus();
}}
/* eslint-enable react/jsx-no-bind */ /* eslint-enable react/jsx-no-bind */
> >
<FormikRadioButton <FormikRadioButton
isCustomInput isCustomInput
buttonValue={values.custom}
className={classNames( className={classNames(
'join-flow-radio' 'join-flow-radio'
)} )}
id="GenderRadioOptionCustom"
label={this.props.intl.formatMessage({id: 'registration.genderOptionAnother'})} label={this.props.intl.formatMessage({id: 'registration.genderOptionAnother'})}
name="gender" name="gender"
value={values.custom}
/* eslint-disable react/jsx-no-bind */ /* eslint-disable react/jsx-no-bind */
onSetCustom={newCustomVal => setValues({ onSetCustom={newCustomVal => setValues({
gender: newCustomVal, gender: newCustomVal,
custom: newCustomVal custom: newCustomVal
})} })}
onSetCustomRef={this.handleSetCustomRef}
/* eslint-enable react/jsx-no-bind */ /* eslint-enable react/jsx-no-bind */
/> />
</div> </div>
<GenderOption <GenderOption
id="GenderRadioOptionPreferNot"
label={this.props.intl.formatMessage({id: 'registration.genderOptionPreferNotToSay'})} label={this.props.intl.formatMessage({id: 'registration.genderOptionPreferNotToSay'})}
selectedValue={values.gender} selectedValue={values.gender}
value="Prefer not to say" value="Prefer not to say"