1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | import * as React from "react";
|
21 |
|
22 | import { DISPLAYNAME_PREFIX, removeNonHTMLProps } from "../../common/props";
|
23 | import { IRef, refHandler, setRef } from "../../common/refs";
|
24 | import { AbstractButton, IButtonProps, IAnchorButtonProps, ButtonProps, AnchorButtonProps } from "./abstractButton";
|
25 |
|
26 |
|
27 | export { IAnchorButtonProps, IButtonProps, ButtonProps, AnchorButtonProps };
|
28 |
|
29 | export class Button extends AbstractButton<HTMLButtonElement> {
|
30 | public static displayName = `${DISPLAYNAME_PREFIX}.Button`;
|
31 |
|
32 |
|
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 |
|
59 | export class AnchorButton extends AbstractButton<HTMLAnchorElement> {
|
60 | public static displayName = `${DISPLAYNAME_PREFIX}.AnchorButton`;
|
61 |
|
62 |
|
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 | }
|