-
Notifications
You must be signed in to change notification settings - Fork 2.9k
ChoiceGroup: getStyles conversion #4852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
3c8e03d
361fef6
d96b05b
643a80f
20e4607
da2b46e
5145e28
f0852d6
02cf116
69d9d28
1cc6cbe
a4d430f
64bd7bd
73a21f4
bd57d47
802523a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@uifabric/utilities", | ||
| "comment": "Added resetIds for predictable jest tests", | ||
| "type": "minor" | ||
| } | ||
| ], | ||
| "packageName": "@uifabric/utilities", | ||
| "email": "adrum@microsoft.com" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "office-ui-fabric-react", | ||
| "comment": "ChoiceGroup getStyles conversion", | ||
| "type": "minor" | ||
| } | ||
| ], | ||
| "packageName": "office-ui-fabric-react", | ||
| "email": "adrum@microsoft.com" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,5 +29,8 @@ | |
| "license": "MIT", | ||
| "devDependencies": { | ||
| "@microsoft/rush": "4.3.0" | ||
| }, | ||
| "dependencies": { | ||
| "npm": "^6.0.0" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually unintentional. npm told me at some point to run a command, which I diligently obeyed, and this edit got auto-added. Commentary is welcome on why npm thought this was necessary... ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe npm was telling you that a new version of npm is available and prompting you to upgrade :) But.. you might have entered incorrectly (specifically, missed the You probably want to revert this. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './components/ChoiceGroupOption/index'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import * as React from 'react'; | ||
| import { Label } from '../../Label'; | ||
| import { ChoiceGroupOption, OnFocusCallback, OnChangeCallback } from '../../ChoiceGroupOption'; | ||
| import { IChoiceGroupOption, IChoiceGroupProps, IChoiceGroupStyleProps, IChoiceGroupStyles } from './ChoiceGroup.types'; | ||
| import { | ||
| BaseComponent, | ||
| customizable, | ||
| classNamesFunction, | ||
| createRef, | ||
| getId | ||
| } from '../../Utilities'; | ||
|
|
||
| const getClassNames = classNamesFunction<IChoiceGroupStyleProps, IChoiceGroupStyles>(); | ||
|
|
||
| export interface IChoiceGroupState { | ||
| keyChecked: string | number; | ||
|
|
||
| /** Is true when the control has focus. */ | ||
| keyFocused?: string | number; | ||
| } | ||
|
|
||
| @customizable('ChoiceGroup', ['theme']) | ||
| export class ChoiceGroupBase extends BaseComponent<IChoiceGroupProps, IChoiceGroupState> { | ||
| public static defaultProps: IChoiceGroupProps = { | ||
| options: [] | ||
| }; | ||
|
|
||
| private _id: string; | ||
| private _labelId: string; | ||
| private _inputElement = createRef<HTMLInputElement>(); | ||
| private focusedVars: { [key: string]: OnFocusCallback } = {}; | ||
| private changedVars: { [key: string]: OnChangeCallback } = {}; | ||
|
|
||
| constructor(props: IChoiceGroupProps, ) { | ||
| super(props); | ||
|
|
||
| this._warnDeprecations({ 'onChanged': 'onChange' }); | ||
| this._warnMutuallyExclusive({ | ||
| selectedKey: 'defaultSelectedKey' | ||
| }); | ||
|
|
||
| this.state = { | ||
| keyChecked: (props.defaultSelectedKey === undefined) ? | ||
| this._getKeyChecked(props)! : | ||
| props.defaultSelectedKey, | ||
| keyFocused: undefined | ||
| }; | ||
|
|
||
| this._id = getId('ChoiceGroup'); | ||
| this._labelId = getId('ChoiceGroupLabel'); | ||
| } | ||
|
|
||
| public componentWillReceiveProps(newProps: IChoiceGroupProps): void { | ||
| const newKeyChecked = this._getKeyChecked(newProps); | ||
| const oldKeyCheched = this._getKeyChecked(this.props); | ||
|
|
||
| if (newKeyChecked !== oldKeyCheched) { | ||
| this.setState({ | ||
| keyChecked: newKeyChecked!, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public render(): JSX.Element { | ||
| const { | ||
| className, | ||
| theme, | ||
| getStyles, | ||
| options, | ||
| label, | ||
| required, | ||
| disabled, | ||
| name | ||
| } = this.props; | ||
| const { keyChecked, keyFocused } = this.state; | ||
|
|
||
| const classNames = getClassNames(getStyles!, { | ||
| theme: theme!, | ||
| className, | ||
| optionsContainIconOrImage: options!.some(option => Boolean(option.iconProps || option.imageSrc)) | ||
| }); | ||
|
|
||
| return ( | ||
| // Need to assign role application on containing div because JAWS doesn't call OnKeyDown without this role | ||
| <div role='application' className={ classNames.applicationRole }> | ||
| <div | ||
| className={ classNames.root } | ||
| role='radiogroup' | ||
| aria-labelledby={ `${this.props.label ? this._id + '-label' : ''} ${(this.props as any)['aria-labelledby'] || ''}` } | ||
| > | ||
| { label && (<Label className={ classNames.label } required={ required } id={ this._id + '-label' }>{ label }</Label>) } | ||
| <div className={ classNames.flexContainer }> | ||
| { options!.map((option: IChoiceGroupOption) => { | ||
|
|
||
| const innerOptionProps = { | ||
| ...option, | ||
| focused: option.key === keyFocused, | ||
| checked: option.key === keyChecked, | ||
| disabled: option.disabled || disabled, | ||
| id: `${this._id}-${option.key}`, | ||
| labelId: `${this._labelId}-${option.key}`, | ||
| name: name || this._id, | ||
| required | ||
| }; | ||
|
|
||
| return ( | ||
| <ChoiceGroupOption | ||
| key={ option.key } | ||
| onBlur={ this._onBlur } | ||
| onFocus={ this._onFocus(option.key) } | ||
| onChange={ this._onChange(option.key) } | ||
| { ...innerOptionProps } | ||
| /> | ||
| ); | ||
| }) } | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| public focus() { | ||
| if (this._inputElement.current) { | ||
| this._inputElement.current.focus(); | ||
| } | ||
| } | ||
|
|
||
| private _onFocus = (key: string) => | ||
| this.focusedVars[key] ? this.focusedVars[key] : this.focusedVars[key] = | ||
| (ev: React.FocusEvent<HTMLElement>, option: IChoiceGroupOption) => { | ||
| this.setState({ | ||
| keyFocused: key, | ||
| keyChecked: this.state.keyChecked | ||
| }); | ||
| } | ||
|
|
||
| private _onBlur = (ev: React.FocusEvent<HTMLElement>, option: IChoiceGroupOption) => { | ||
| this.setState({ | ||
| keyFocused: undefined, | ||
| keyChecked: this.state.keyChecked | ||
| }); | ||
| } | ||
|
|
||
| private _onChange = (key: string) => | ||
| this.changedVars[key] ? this.changedVars[key] : this.changedVars[key] = | ||
| (evt, option: IChoiceGroupOption) => { | ||
| const { onChanged, onChange, selectedKey, options } = this.props; | ||
|
|
||
| // Only manage state in uncontrolled scenarios. | ||
| if (selectedKey === undefined) { | ||
| this.setState({ | ||
| keyChecked: key | ||
| }); | ||
| } | ||
|
|
||
| const originalOption = options!.find((value: IChoiceGroupOption) => value.key === key); | ||
|
|
||
| // TODO: onChanged deprecated, remove else if after 07/17/2017 when onChanged has been removed. | ||
| if (onChange) { | ||
| onChange(evt, originalOption); | ||
| } else if (onChanged) { | ||
| onChanged(originalOption!); | ||
| } | ||
| } | ||
|
|
||
| private _getKeyChecked(props: IChoiceGroupProps): string | number | undefined { | ||
| if (props.selectedKey !== undefined) { | ||
| return props.selectedKey; | ||
| } | ||
|
|
||
| const optionsChecked = props.options!.filter((option: IChoiceGroupOption) => { | ||
| return option.checked; | ||
| }); | ||
|
|
||
| if (optionsChecked.length === 0) { | ||
| return undefined; | ||
| } else { | ||
| return optionsChecked[0].key; | ||
| } | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we are targeting the 6.0 branch, we should rename all the changefiles to 6_0_choiceGroupGetStyles.json manually