|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { ReactWrapperComponent, InputRendererOptions, JsxRenderFunc } from '@angular-react/core'; |
| 5 | +import { |
| 6 | + ChangeDetectorRef, |
| 7 | + EventEmitter, |
| 8 | + ElementRef, |
| 9 | + Input, |
| 10 | + Renderer2, |
| 11 | + ViewChild, |
| 12 | + OnInit, |
| 13 | + Output, |
| 14 | +} from '@angular/core'; |
| 15 | +import { ITextFieldProps } from 'office-ui-fabric-react/lib/TextField'; |
| 16 | + |
| 17 | +export class FabBaseTextFieldComponent extends ReactWrapperComponent<ITextFieldProps> implements OnInit { |
| 18 | + @ViewChild('reactNode') protected reactNodeRef: ElementRef; |
| 19 | + |
| 20 | + @Input() componentRef?: ITextFieldProps['componentRef']; |
| 21 | + @Input() multiline?: ITextFieldProps['multiline']; |
| 22 | + @Input() resizable?: ITextFieldProps['resizable']; |
| 23 | + @Input() autoAdjustHeight?: ITextFieldProps['autoAdjustHeight']; |
| 24 | + @Input() underlined?: ITextFieldProps['underlined']; |
| 25 | + @Input() borderless?: ITextFieldProps['borderless']; |
| 26 | + @Input() label?: ITextFieldProps['label']; |
| 27 | + @Input() description?: ITextFieldProps['description']; |
| 28 | + @Input() prefix?: ITextFieldProps['prefix']; |
| 29 | + @Input() suffix?: ITextFieldProps['suffix']; |
| 30 | + @Input() iconProps?: ITextFieldProps['iconProps']; |
| 31 | + @Input() defaultValue?: ITextFieldProps['defaultValue']; |
| 32 | + @Input() value?: ITextFieldProps['value']; |
| 33 | + @Input() disabled?: ITextFieldProps['disabled']; |
| 34 | + @Input() readOnly?: ITextFieldProps['readOnly']; |
| 35 | + @Input() errorMessage?: ITextFieldProps['errorMessage']; |
| 36 | + @Input() deferredValidationTime?: ITextFieldProps['deferredValidationTime']; |
| 37 | + @Input() className?: ITextFieldProps['className']; |
| 38 | + @Input() inputClassName?: ITextFieldProps['inputClassName']; |
| 39 | + @Input() ariaLabel?: ITextFieldProps['ariaLabel']; |
| 40 | + @Input() validateOnFocusIn?: ITextFieldProps['validateOnFocusIn']; |
| 41 | + @Input() validateOnFocusOut?: ITextFieldProps['validateOnFocusOut']; |
| 42 | + @Input() validateOnLoad?: ITextFieldProps['validateOnLoad']; |
| 43 | + @Input() theme?: ITextFieldProps['theme']; |
| 44 | + @Input() styles?: ITextFieldProps['styles']; |
| 45 | + @Input() autoComplete?: ITextFieldProps['autoComplete']; |
| 46 | + @Input() mask?: ITextFieldProps['mask']; |
| 47 | + @Input() maskChar?: ITextFieldProps['maskChar']; |
| 48 | + @Input() maskFormat?: ITextFieldProps['maskFormat']; |
| 49 | + @Input() getErrorMessage?: ITextFieldProps['onGetErrorMessage']; |
| 50 | + |
| 51 | + @Input() renderLabel?: InputRendererOptions<ITextFieldProps>; |
| 52 | + @Input() renderDescription?: InputRendererOptions<ITextFieldProps>; |
| 53 | + @Input() renderPrefix?: InputRendererOptions<ITextFieldProps>; |
| 54 | + @Input() renderSuffix?: InputRendererOptions<ITextFieldProps>; |
| 55 | + |
| 56 | + @Output() readonly onChange = new EventEmitter<{ event: Event; newValue?: string }>(); |
| 57 | + @Output() readonly onBeforeChange = new EventEmitter<{ newValue: any }>(); |
| 58 | + @Output() readonly onNotifyValidationResult = new EventEmitter<{ errorMessage: string; value: string | undefined }>(); |
| 59 | + |
| 60 | + /* Non-React props, more native support for Angular */ |
| 61 | + // support for two-way data binding for `@Input() checked`. |
| 62 | + @Output() readonly valueChange = new EventEmitter<string>(); |
| 63 | + |
| 64 | + onRenderLabel: (props?: ITextFieldProps, defaultRender?: JsxRenderFunc<ITextFieldProps>) => JSX.Element; |
| 65 | + onRenderDescription: (props?: ITextFieldProps, defaultRender?: JsxRenderFunc<ITextFieldProps>) => JSX.Element; |
| 66 | + onRenderPrefix: (props?: ITextFieldProps, defaultRender?: JsxRenderFunc<ITextFieldProps>) => JSX.Element; |
| 67 | + onRenderSuffix: (props?: ITextFieldProps, defaultRender?: JsxRenderFunc<ITextFieldProps>) => JSX.Element; |
| 68 | + |
| 69 | + constructor(elementRef: ElementRef, changeDetectorRef: ChangeDetectorRef, renderer: Renderer2) { |
| 70 | + super(elementRef, changeDetectorRef, renderer); |
| 71 | + |
| 72 | + this.onChangeHandler = this.onChangeHandler.bind(this); |
| 73 | + this.onBeforeChangeHandler = this.onBeforeChangeHandler.bind(this); |
| 74 | + this.onNotifyValidationResultHandler = this.onNotifyValidationResultHandler.bind(this); |
| 75 | + } |
| 76 | + |
| 77 | + ngOnInit() { |
| 78 | + this.onRenderLabel = this.createRenderPropHandler(this.renderLabel); |
| 79 | + this.onRenderDescription = this.createRenderPropHandler(this.renderDescription); |
| 80 | + this.onRenderPrefix = this.createRenderPropHandler(this.renderPrefix); |
| 81 | + this.onRenderSuffix = this.createRenderPropHandler(this.renderSuffix); |
| 82 | + } |
| 83 | + |
| 84 | + onChangeHandler(event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string) { |
| 85 | + this.onChange.emit({ event: event.nativeEvent, newValue }); |
| 86 | + |
| 87 | + this.valueChange.emit(newValue); |
| 88 | + } |
| 89 | + |
| 90 | + onBeforeChangeHandler(newValue: any) { |
| 91 | + this.onBeforeChange.emit({ newValue }); |
| 92 | + } |
| 93 | + |
| 94 | + onNotifyValidationResultHandler(errorMessage: string, value: string | undefined) { |
| 95 | + this.onNotifyValidationResult.emit({ errorMessage, value }); |
| 96 | + } |
| 97 | +} |
0 commit comments