This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
feat(plugin-chart-table): add column config control #1019
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/superset-ui-chart-controls/src/components/ControlForm/ControlFormItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React, { useState, FunctionComponentElement, ChangeEvent } from 'react'; | ||
import { JsonValue, useTheme } from '@superset-ui/core'; | ||
import ControlHeader, { ControlHeaderProps } from '../ControlHeader'; | ||
import InfoTooltipWithTrigger from '../InfoTooltipWithTrigger'; | ||
import { ControlFormItemComponents, ControlFormItemSpec } from './controls'; | ||
|
||
export * from './controls'; | ||
|
||
export type ControlFormItemProps = ControlFormItemSpec & { | ||
name: string; | ||
onChange?: (fieldValue: JsonValue) => void; | ||
}; | ||
|
||
export type ControlFormItemNode = FunctionComponentElement<ControlFormItemProps>; | ||
|
||
/** | ||
* Accept `false` or `0`, but not empty string. | ||
*/ | ||
function isEmptyValue(value?: JsonValue) { | ||
return value == null || value === ''; | ||
} | ||
|
||
export function ControlFormItem({ | ||
name, | ||
label, | ||
description, | ||
width, | ||
validators, | ||
required, | ||
onChange, | ||
value: initialValue, | ||
defaultValue, | ||
controlType, | ||
...props | ||
}: ControlFormItemProps) { | ||
const { gridUnit } = useTheme(); | ||
const [hovered, setHovered] = useState(false); | ||
const [value, setValue] = useState(initialValue === undefined ? defaultValue : initialValue); | ||
const [validationErrors, setValidationErrors] = useState< | ||
ControlHeaderProps['validationErrors'] | ||
>(); | ||
|
||
const handleChange = (e: ChangeEvent<HTMLInputElement> | JsonValue) => { | ||
const fieldValue = | ||
e && typeof e === 'object' && 'target' in e | ||
? e.target.type === 'checkbox' || e.target.type === 'radio' | ||
? e.target.checked | ||
: e.target.value | ||
: e; | ||
const errors = | ||
(validators | ||
?.map(validator => (!required && isEmptyValue(fieldValue) ? false : validator(fieldValue))) | ||
.filter(x => !!x) as string[]) || []; | ||
setValidationErrors(errors); | ||
setValue(fieldValue); | ||
if (errors.length === 0 && onChange) { | ||
onChange(fieldValue as JsonValue); | ||
} | ||
}; | ||
|
||
const Control = ControlFormItemComponents[controlType]; | ||
|
||
return ( | ||
<div | ||
css={{ | ||
margin: 2 * gridUnit, | ||
width, | ||
}} | ||
onMouseEnter={() => setHovered(true)} | ||
onMouseLeave={() => setHovered(false)} | ||
> | ||
{controlType === 'Checkbox' ? ( | ||
<ControlFormItemComponents.Checkbox checked={value as boolean} onChange={handleChange}> | ||
{label} {hovered && description && <InfoTooltipWithTrigger tooltip={description} />} | ||
</ControlFormItemComponents.Checkbox> | ||
) : ( | ||
<> | ||
{label && ( | ||
<ControlHeader | ||
name={name} | ||
label={label} | ||
description={description} | ||
validationErrors={validationErrors} | ||
hovered={hovered} | ||
required={required} | ||
/> | ||
)} | ||
{/* @ts-ignore */} | ||
<Control {...props} value={value} onChange={handleChange} /> | ||
</> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default ControlFormItem; |
92 changes: 92 additions & 0 deletions
92
packages/superset-ui-chart-controls/src/components/ControlForm/controls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React, { ReactNode } from 'react'; | ||
import { Slider, InputNumber, Input } from 'antd'; | ||
import Checkbox, { CheckboxProps } from 'antd/lib/checkbox'; | ||
import Select, { SelectOption } from '../Select'; | ||
import RadioButtonControl, { | ||
RadioButtonOption, | ||
} from '../../shared-controls/components/RadioButtonControl'; | ||
|
||
export const ControlFormItemComponents = { | ||
Slider, | ||
InputNumber, | ||
Input, | ||
Select, | ||
// Directly export Checkbox will result in "using name from external module" error | ||
// ref: https://stackoverflow.com/questions/43900035/ts4023-exported-variable-x-has-or-is-using-name-y-from-external-module-but | ||
Checkbox: Checkbox as React.ForwardRefExoticComponent< | ||
CheckboxProps & React.RefAttributes<HTMLInputElement> | ||
>, | ||
RadioButtonControl, | ||
}; | ||
|
||
export type ControlType = keyof typeof ControlFormItemComponents; | ||
|
||
export type ControlFormValueValidator<V> = (value: V) => string | false; | ||
|
||
export type ControlFormItemSpec<T extends ControlType = ControlType> = { | ||
controlType: T; | ||
label: ReactNode; | ||
description: ReactNode; | ||
placeholder?: string; | ||
required?: boolean; | ||
validators?: ControlFormValueValidator<any>[]; | ||
width?: number | string; | ||
/** | ||
* Time to delay change propagation. | ||
*/ | ||
debounceDelay?: number; | ||
} & (T extends 'Select' | ||
? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it worth extracting the type definition after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's worth it as you'd have to name them and reference them. The indirectness means you'd have to jump back and forth when glancing through code. I also don't see much added value in terms of readability. |
||
options: SelectOption<any>[]; | ||
value?: string; | ||
defaultValue?: string; | ||
creatable?: boolean; | ||
minWidth?: number | string; | ||
validators?: ControlFormValueValidator<string>[]; | ||
} | ||
: T extends 'RadioButtonControl' | ||
? { | ||
options: RadioButtonOption[]; | ||
value?: string; | ||
defaultValue?: string; | ||
} | ||
: T extends 'Checkbox' | ||
? { | ||
value?: boolean; | ||
defaultValue?: boolean; | ||
} | ||
: T extends 'InputNumber' | 'Slider' | ||
? { | ||
min?: number; | ||
max?: number; | ||
step?: number; | ||
value?: number; | ||
defaultValue?: number; | ||
validators?: ControlFormValueValidator<number>[]; | ||
} | ||
: T extends 'Input' | ||
? { | ||
controlType: 'Input'; | ||
value?: string; | ||
defaultValue?: string; | ||
validators?: ControlFormValueValidator<string>[]; | ||
} | ||
: {}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
Lax
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like less strict... Supposedly column types should be mutually exclusive, but we also have some more random types from different places.
LaxColumnType
makes sure the column type icon component can accept all these types, while more strictColumnType
will be used elsewhere.