mirror of
https://github.com/apache/superset.git
synced 2026-05-31 05:09:20 +00:00
feat(Pie Chart): threshold for Other (#33348)
This commit is contained in:
committed by
GitHub
parent
8a8fb49617
commit
fa1693dc5f
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 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 { render, screen, userEvent } from 'spec/helpers/testing-library';
|
||||
import NumberControl from '.';
|
||||
|
||||
const mockedProps = {
|
||||
min: -5,
|
||||
max: 10,
|
||||
step: 1,
|
||||
default: 0,
|
||||
};
|
||||
|
||||
test('render', () => {
|
||||
const { container } = render(<NumberControl {...mockedProps} />);
|
||||
expect(container).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('type number', async () => {
|
||||
const props = {
|
||||
...mockedProps,
|
||||
onChange: jest.fn(),
|
||||
};
|
||||
render(<NumberControl {...props} />);
|
||||
const input = screen.getByRole('spinbutton');
|
||||
await userEvent.type(input, '9');
|
||||
expect(props.onChange).toHaveBeenCalledTimes(1);
|
||||
expect(props.onChange).toHaveBeenLastCalledWith(9);
|
||||
});
|
||||
|
||||
test('type >max', async () => {
|
||||
const props = {
|
||||
...mockedProps,
|
||||
onChange: jest.fn(),
|
||||
};
|
||||
render(<NumberControl {...props} />);
|
||||
const input = screen.getByRole('spinbutton');
|
||||
await userEvent.type(input, '20');
|
||||
expect(props.onChange).toHaveBeenCalledTimes(1);
|
||||
expect(props.onChange).toHaveBeenLastCalledWith(2);
|
||||
});
|
||||
|
||||
test('type NaN', async () => {
|
||||
const props = {
|
||||
...mockedProps,
|
||||
onChange: jest.fn(),
|
||||
};
|
||||
render(<NumberControl {...props} />);
|
||||
const input = screen.getByRole('spinbutton');
|
||||
await userEvent.type(input, 'not a number');
|
||||
expect(props.onChange).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 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 { styled } from '@superset-ui/core';
|
||||
import { InputNumber } from 'src/components/Input';
|
||||
import ControlHeader, { ControlHeaderProps } from '../../ControlHeader';
|
||||
|
||||
type NumberValueType = number | undefined;
|
||||
|
||||
export interface NumberControlProps extends ControlHeaderProps {
|
||||
onChange?: (value: NumberValueType) => void;
|
||||
value?: NumberValueType;
|
||||
label?: string;
|
||||
description?: string;
|
||||
min?: number;
|
||||
max?: number;
|
||||
step?: number;
|
||||
placeholder?: string;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
const FullWidthDiv = styled.div`
|
||||
width: 100%;
|
||||
`;
|
||||
|
||||
const FullWidthInputNumber = styled(InputNumber)`
|
||||
width: 100%;
|
||||
`;
|
||||
|
||||
function parseValue(value: string | number | null | undefined) {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return undefined;
|
||||
}
|
||||
const num = Number(value);
|
||||
return Number.isNaN(num) ? undefined : num;
|
||||
}
|
||||
|
||||
export default function NumberControl({
|
||||
min,
|
||||
max,
|
||||
step,
|
||||
placeholder,
|
||||
value,
|
||||
onChange,
|
||||
disabled,
|
||||
...rest
|
||||
}: NumberControlProps) {
|
||||
return (
|
||||
<FullWidthDiv>
|
||||
<ControlHeader {...rest} />
|
||||
<FullWidthInputNumber
|
||||
min={min}
|
||||
max={max}
|
||||
step={step}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={value => onChange?.(parseValue(value))}
|
||||
disabled={disabled}
|
||||
aria-label={rest.label}
|
||||
/>
|
||||
</FullWidthDiv>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user