From a5afef9178daa32e7e0068ca238046868fa3d22c Mon Sep 17 00:00:00 2001 From: Ivan Samoylov Date: Wed, 7 Feb 2018 01:49:10 +0600 Subject: [PATCH 1/3] fixed support for the componentId attribute + changed initialization attributed componentId, added default value this._id + changed initialization of the variable renderProps + changed render function for and + added test to check attribute --- .../components/TextField/TextField.test.tsx | 21 ++++++++++++++++- .../src/components/TextField/TextField.tsx | 23 ++++++++++--------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/office-ui-fabric-react/src/components/TextField/TextField.test.tsx b/packages/office-ui-fabric-react/src/components/TextField/TextField.test.tsx index cca8acb4468347..c3805b1d23e214 100644 --- a/packages/office-ui-fabric-react/src/components/TextField/TextField.test.tsx +++ b/packages/office-ui-fabric-react/src/components/TextField/TextField.test.tsx @@ -84,7 +84,7 @@ describe('TextField', () => { const renderedDOM: HTMLElement = renderIntoDocument( ); @@ -122,6 +122,25 @@ describe('TextField', () => { expect(inputDOM.id).toEqual(labelDOM.htmlFor); }); + it('should associate the label and input box use custom id', () => { + const exampleComponentId: string = 'test-component-id'; + + const renderedDOM: HTMLElement = renderIntoDocument( + + ); + + const inputDOM: HTMLInputElement = renderedDOM.getElementsByTagName('input')[0]; + const labelDOM: HTMLLabelElement = renderedDOM.getElementsByTagName('label')[0]; + + // Assert the input ID and label FOR attribute are the equal to exampleComponentId. + expect(inputDOM.id).toEqual(exampleComponentId); + expect(labelDOM.htmlFor).toEqual(exampleComponentId); + }); + it('should render a disabled input element', () => { const renderedDOM: HTMLElement = renderIntoDocument( diff --git a/packages/office-ui-fabric-react/src/components/TextField/TextField.tsx b/packages/office-ui-fabric-react/src/components/TextField/TextField.tsx index bb711fbd70f7ee..d41d3ab2ef98b1 100644 --- a/packages/office-ui-fabric-react/src/components/TextField/TextField.tsx +++ b/packages/office-ui-fabric-react/src/components/TextField/TextField.tsx @@ -143,12 +143,13 @@ export class TextField extends BaseComponent i onRenderAddon = this._onRenderAddon, // @deprecated onRenderPrefix = this._onRenderPrefix, onRenderSuffix = this._onRenderSuffix, - onRenderLabel = this._onRenderLabel + onRenderLabel = this._onRenderLabel, + componentId = this._id } = this.props; let { isFocused } = this.state; const errorMessage = this._errorMessage; this._isDescriptionAvailable = Boolean(description || errorMessage); - const renderProps: ITextFieldProps = { ...this.props, componentId: this._id }; + const renderProps: ITextFieldProps = { ...this.props, componentId }; const textFieldClassName = css('ms-TextField', styles.root, className, { ['is-required ' + styles.rootIsRequiredLabel]: this.props.label && required, @@ -175,7 +176,7 @@ export class TextField extends BaseComponent i { onRenderPrefix(this.props, this._onRenderPrefix) } ) } - { multiline ? this._renderTextArea() : this._renderInput() } + { multiline ? this._renderTextArea(renderProps) : this._renderInput(renderProps) } { (iconClass || iconProps) && } { (suffix !== undefined || this.props.onRenderSuffix) && (
@@ -343,12 +344,12 @@ export class TextField extends BaseComponent i return errorMessage; } - private _renderTextArea(): React.ReactElement> { - let textAreaProps = getNativeProps(this.props, textAreaProperties, ['defaultValue']); + private _renderTextArea(props: ITextFieldProps): React.ReactElement> { + let textAreaProps = getNativeProps(props, textAreaProperties, ['defaultValue']); return (