2021-12-08 14:06:14 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import BaseControl, { ControlProps } from "./BaseControl";
|
|
|
|
|
import { ColumnProperties } from "widgets/TableWidget/component/Constants";
|
|
|
|
|
import { StyledDropDown, StyledDropDownContainer } from "./StyledControls";
|
|
|
|
|
import { DropdownOption } from "components/ads/Dropdown";
|
2022-07-14 05:00:30 +00:00
|
|
|
import {
|
|
|
|
|
DSEventDetail,
|
|
|
|
|
DSEventTypes,
|
|
|
|
|
DS_EVENT,
|
|
|
|
|
emitInteractionAnalyticsEvent,
|
|
|
|
|
} from "utils/AppsmithUtils";
|
2021-12-08 14:06:14 +00:00
|
|
|
|
|
|
|
|
class PrimaryColumnDropdownControl extends BaseControl<ControlProps> {
|
2022-07-14 05:00:30 +00:00
|
|
|
containerRef = React.createRef<HTMLDivElement>();
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.containerRef.current?.addEventListener(
|
|
|
|
|
DS_EVENT,
|
|
|
|
|
this.handleAdsEvent as (arg0: Event) => void,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
this.containerRef.current?.removeEventListener(
|
|
|
|
|
DS_EVENT,
|
|
|
|
|
this.handleAdsEvent as (arg0: Event) => void,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleAdsEvent = (e: CustomEvent<DSEventDetail>) => {
|
|
|
|
|
if (
|
|
|
|
|
e.detail.component === "Dropdown" &&
|
|
|
|
|
e.detail.event === DSEventTypes.KEYPRESS
|
|
|
|
|
) {
|
|
|
|
|
emitInteractionAnalyticsEvent(this.containerRef.current, {
|
|
|
|
|
key: e.detail.meta.key,
|
|
|
|
|
});
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-08 14:06:14 +00:00
|
|
|
render() {
|
|
|
|
|
// Get columns from widget properties
|
|
|
|
|
const columns: Record<string, ColumnProperties> = this.props
|
|
|
|
|
.widgetProperties.primaryColumns;
|
|
|
|
|
const options: any[] = [];
|
|
|
|
|
|
|
|
|
|
for (const i in columns) {
|
|
|
|
|
options.push({
|
|
|
|
|
label: columns[i].label,
|
|
|
|
|
id: columns[i].id,
|
|
|
|
|
value: i,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let defaultSelected: DropdownOption = {
|
|
|
|
|
label: "No selection.",
|
|
|
|
|
value: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const selected: DropdownOption = options.find(
|
|
|
|
|
(option) => option.value === this.props.propertyValue,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (selected) {
|
|
|
|
|
defaultSelected = selected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2022-07-14 05:00:30 +00:00
|
|
|
<StyledDropDownContainer ref={this.containerRef}>
|
2021-12-08 14:06:14 +00:00
|
|
|
<StyledDropDown
|
2022-05-06 05:04:17 +00:00
|
|
|
dropdownMaxHeight="200px"
|
|
|
|
|
fillOptions
|
2021-12-08 14:06:14 +00:00
|
|
|
onSelect={this.onItemSelect}
|
|
|
|
|
options={options}
|
|
|
|
|
selected={defaultSelected}
|
|
|
|
|
showLabelOnly
|
|
|
|
|
width="100%"
|
|
|
|
|
/>
|
|
|
|
|
</StyledDropDownContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 05:00:30 +00:00
|
|
|
onItemSelect = (
|
|
|
|
|
value?: string,
|
|
|
|
|
_option?: DropdownOption,
|
|
|
|
|
isUpdatedViaKeyboard?: boolean,
|
|
|
|
|
): void => {
|
2021-12-08 14:06:14 +00:00
|
|
|
if (value) {
|
2022-07-14 05:00:30 +00:00
|
|
|
this.updateProperty(this.props.propertyName, value, isUpdatedViaKeyboard);
|
2021-12-08 14:06:14 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static getControlType() {
|
|
|
|
|
return "PRIMARY_COLUMNS_DROPDOWN";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface PrimaryColumnDropdownControlProps extends ControlProps {
|
|
|
|
|
propertyValue: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PrimaryColumnDropdownControl;
|