Files
redash/client/app/components/visualizations/editor/withControlLabel.jsx
Arik Fraimovich 56d3be2248 Prettier all the Javascript code & GitHub Action (#4433)
* Prettier all the JS files

* Add GitHub Action to autoformat code pushed to master

* Fix eslint violation due to formatting.

* Remove GitHub actions for styling

* Add restyled.io config
2019-12-11 17:05:38 +02:00

83 lines
2.3 KiB
JavaScript

import React, { useMemo } from "react";
import PropTypes from "prop-types";
import hoistNonReactStatics from "hoist-non-react-statics";
import * as Grid from "antd/lib/grid";
import Typography from "antd/lib/typography";
import "./control-label.less";
export function ControlLabel({ layout, label, labelProps, disabled, children }) {
if (layout === "vertical" && label) {
return (
<div className="visualization-editor-control-label visualization-editor-control-label-vertical">
<label {...labelProps}>
<Typography.Text disabled={disabled}>{label}</Typography.Text>
</label>
{children}
</div>
);
}
if (layout === "horizontal" && label) {
return (
<Grid.Row
className="visualization-editor-control-label visualization-editor-control-label-horizontal"
type="flex"
align="middle"
gutter={15}>
<Grid.Col span={12}>
<label {...labelProps}>
<Typography.Text disabled={disabled}>{label}</Typography.Text>
</label>
</Grid.Col>
<Grid.Col span={12}>{children}</Grid.Col>
</Grid.Row>
);
}
return children;
}
ControlLabel.propTypes = {
layout: PropTypes.oneOf(["vertical", "horizontal"]),
label: PropTypes.node,
labelProps: PropTypes.object, // eslint-disable-line react/forbid-prop-types
disabled: PropTypes.bool,
children: PropTypes.node,
};
ControlLabel.defaultProps = {
layout: "vertical",
label: null,
disabled: false,
children: null,
};
export default function withControlLabel(WrappedControl) {
// eslint-disable-next-line react/prop-types
function ControlWrapper({ id, layout, label, labelProps, disabled, ...props }) {
const fallbackId = useMemo(
() =>
`visualization-editor-control-${Math.random()
.toString(36)
.substr(2, 10)}`,
[]
);
labelProps = {
...labelProps,
htmlFor: id || fallbackId,
};
return (
<ControlLabel layout={layout} label={label} labelProps={labelProps} disabled={disabled}>
<WrappedControl id={labelProps.htmlFor} disabled={disabled} {...props} />
</ControlLabel>
);
}
// Copy static methods from `WrappedComponent`
hoistNonReactStatics(ControlWrapper, WrappedControl);
return ControlWrapper;
}