forked from spinnaker/deck
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/presentation): Support inline radio buttons (spinnaker#7078)
- Remove inline checkbox and radio button CSS tweaks (revert margin/padding to Bootstrap 3 defaults) - Add margin hack for inline radio/checkboxes to support wrapping to the next line
- Loading branch information
1 parent
fb2b002
commit 9bbc002
Showing
3 changed files
with
42 additions
and
27 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
48 changes: 30 additions & 18 deletions
48
app/scripts/modules/core/src/presentation/forms/inputs/RadioButtonInput.tsx
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 |
---|---|---|
@@ -1,40 +1,52 @@ | ||
import * as React from 'react'; | ||
import { Option } from 'react-select'; | ||
|
||
import { Markdown, OmitControlledInputPropsFrom, StringsAsOptions } from 'core/presentation'; | ||
import { Markdown, OmitControlledInputPropsFrom } from 'core/presentation'; | ||
|
||
import { isStringArray, orEmptyString, validationClassName } from './utils'; | ||
import { IFormInputProps } from '../interface'; | ||
|
||
interface IRadioButtonInputProps | ||
extends IFormInputProps, | ||
OmitControlledInputPropsFrom<React.TextareaHTMLAttributes<any>> { | ||
options: Array<string | Option<string | number>>; | ||
stringOptions?: string[]; | ||
options?: Option[]; | ||
inputClassName?: string; | ||
inline?: boolean; | ||
} | ||
|
||
export const RadioButtonInput = (props: IRadioButtonInputProps) => { | ||
const { value, validation, inputClassName, options, ...otherProps } = props; | ||
const className = `RadioButtonInput radio ${orEmptyString(inputClassName)} ${validationClassName(validation)}`; | ||
const { inline, value: selectedValue, validation, inputClassName, options, stringOptions, ...otherProps } = props; | ||
const radioOptions = isStringArray(stringOptions) ? stringOptions.map(value => ({ value, label: value })) : options; | ||
|
||
const RadioButtonsElement = ({ opts }: { opts: Array<Option<string>> }) => ( | ||
const userClassName = orEmptyString(inputClassName); | ||
const validClassName = validationClassName(validation); | ||
const elementClassName = `RadioButtonInput radio ${userClassName} ${validClassName}`; | ||
|
||
const RadioButton = ({ option }: { option: Option }) => ( | ||
<label key={option.label} className={inline ? 'radio-inline clickable' : 'inline clickable'}> | ||
<input type="radio" {...otherProps} value={option.value as any} checked={option.value === selectedValue} /> | ||
<Markdown message={option.label} /> | ||
</label> | ||
); | ||
|
||
const VerticalRadioButtons = ({ opts }: { opts: Option[] }) => ( | ||
<div className="vertical left"> | ||
<div className={elementClassName}> | ||
{opts.map(option => ( | ||
<RadioButton key={option.label} option={option} /> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
|
||
const InlineRadioButtons = ({ opts }: { opts: Option[] }) => ( | ||
<div className={elementClassName}> | ||
{opts.map(option => ( | ||
<div key={option.label} className={className}> | ||
<label> | ||
<input type="radio" {...otherProps} value={option.value} checked={option.value === value} /> | ||
<span className="marked"> | ||
<Markdown message={option.label} /> | ||
</span> | ||
</label> | ||
</div> | ||
<RadioButton key={option.label} option={option} /> | ||
))} | ||
</div> | ||
); | ||
|
||
if (isStringArray(options)) { | ||
return <StringsAsOptions strings={options}>{opts => <RadioButtonsElement opts={opts} />}</StringsAsOptions>; | ||
} else { | ||
return <RadioButtonsElement opts={options as Array<Option<string>>} />; | ||
} | ||
return inline ? <InlineRadioButtons opts={radioOptions} /> : <VerticalRadioButtons opts={radioOptions} />; | ||
}; |
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