UNPKG

3.43 kBTypeScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// HACKHACK: these components should go in separate files
18/* eslint-disable max-classes-per-file */
19
20import * as React from "react";
21
22import { DISPLAYNAME_PREFIX, removeNonHTMLProps } from "../../common/props";
23import { IRef, refHandler, setRef } from "../../common/refs";
24import { AbstractButton, IButtonProps, IAnchorButtonProps, ButtonProps, AnchorButtonProps } from "./abstractButton";
25
26// eslint-disable-next-line deprecation/deprecation
27export { IAnchorButtonProps, IButtonProps, ButtonProps, AnchorButtonProps };
28
29export class Button extends AbstractButton<HTMLButtonElement> {
30 public static displayName = `${DISPLAYNAME_PREFIX}.Button`;
31
32 // need to keep this ref so that we can access it in AbstractButton#handleKeyUp
33 public buttonRef: HTMLButtonElement | null = null;
34
35 protected handleRef: IRef<HTMLButtonElement> = refHandler(this, "buttonRef", this.props.elementRef);
36
37 public render() {
38 return (
39 <button
40 type="button"
41 ref={this.handleRef}
42 {...removeNonHTMLProps(this.props)}
43 {...this.getCommonButtonProps()}
44 >
45 {this.renderChildren()}
46 </button>
47 );
48 }
49
50 public componentDidUpdate(prevProps: ButtonProps) {
51 if (prevProps.elementRef !== this.props.elementRef) {
52 setRef(prevProps.elementRef, null);
53 this.handleRef = refHandler(this, "buttonRef", this.props.elementRef);
54 setRef(this.props.elementRef, this.buttonRef);
55 }
56 }
57}
58
59export class AnchorButton extends AbstractButton<HTMLAnchorElement> {
60 public static displayName = `${DISPLAYNAME_PREFIX}.AnchorButton`;
61
62 // need to keep this ref so that we can access it in AbstractButton#handleKeyUp
63 public buttonRef: HTMLAnchorElement | null = null;
64
65 protected handleRef: IRef<HTMLAnchorElement> = refHandler(this, "buttonRef", this.props.elementRef);
66
67 public render() {
68 const { href, tabIndex = 0 } = this.props;
69 const commonProps = this.getCommonButtonProps();
70
71 return (
72 <a
73 role="button"
74 ref={this.handleRef}
75 {...removeNonHTMLProps(this.props)}
76 {...commonProps}
77 href={commonProps.disabled ? undefined : href}
78 tabIndex={commonProps.disabled ? -1 : tabIndex}
79 >
80 {this.renderChildren()}
81 </a>
82 );
83 }
84
85 public componentDidUpdate(prevProps: AnchorButtonProps) {
86 if (prevProps.elementRef !== this.props.elementRef) {
87 setRef(prevProps.elementRef, null);
88 this.handleRef = refHandler(this, "buttonRef", this.props.elementRef);
89 setRef(this.props.elementRef, this.buttonRef);
90 }
91 }
92}