Skip to content

Commit 37dd142

Browse files
committed
fix: allow disabled
1 parent 9189668 commit 37dd142

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

src/Fields/SelectField.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
UseControllerProps,
88
} from 'react-hook-form';
99

10-
import { OptionType, GroupedOptionType, Select, SelectProps } from '../Select';
10+
import { Select, SelectProps, OptionType } from '../Select';
1111

1212
import { useFieldContext } from './FieldProvider';
1313
import { FieldWrapper, FieldWrapperProps } from './FieldWrapper';
@@ -16,9 +16,7 @@ type SelectFieldProps<
1616
TFieldValues extends FieldValues,
1717
TFieldPath extends FieldPath<TFieldValues>,
1818
TFieldPathValue extends FieldPathValue<TFieldValues, TFieldPath>,
19-
TOption extends
20-
| OptionType<TFieldPathValue>
21-
| GroupedOptionType<TFieldPathValue>,
19+
TOption extends OptionType<TFieldPathValue>,
2220
> = UseControllerProps<TFieldValues, TFieldPath> &
2321
Pick<FieldWrapperProps<TFieldValues, TFieldPath>, 'formItem'> & {
2422
component?: SelectProps<TFieldPathValue, TOption>;
@@ -31,9 +29,7 @@ export function SelectField<
3129
TFieldValues,
3230
TFieldPath
3331
> = FieldPathValue<TFieldValues, TFieldPath>,
34-
TOption extends
35-
| OptionType<TFieldPathValue>
36-
| GroupedOptionType<TFieldPathValue> = OptionType<TFieldPathValue>,
32+
TOption extends OptionType<TFieldPathValue> = OptionType<TFieldPathValue>,
3733
>({
3834
formItem,
3935
component,

src/Select/index.stories.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const Multiple: Story<SelectProps> = function Multiple(args) {
3131
return <Default mode="multiple" {...args} />;
3232
};
3333

34-
export const Group: Story<SelectProps> = function Group(args) {
34+
export const OptGroup: Story<SelectProps> = function OptGroup(args) {
3535
return (
3636
<Select defaultValue="lucy" style={{ width: 120 }} {...args}>
3737
<Select.OptGroup label="Guys">
@@ -43,10 +43,38 @@ export const Group: Story<SelectProps> = function Group(args) {
4343
<Select.OptGroup label="Gals">
4444
<Select.Option value="lucy">Lucy</Select.Option>
4545
</Select.OptGroup>
46+
<Select.OptGroup label="Other" disabled>
47+
<Select.Option value="juice">Juice</Select.Option>
48+
</Select.OptGroup>
4649
</Select>
4750
);
4851
};
4952

53+
export const OptionsGroup: Story<SelectProps> = function OptionsGroups(args) {
54+
return (
55+
<Select
56+
defaultValue="lucy"
57+
style={{ width: 120 }}
58+
options={[
59+
{
60+
label: 'Guys',
61+
options: [
62+
{ label: 'Jack', value: 'jack' },
63+
{ label: 'Henry', value: 'henry', disabled: true },
64+
],
65+
},
66+
{ label: 'Gals', options: [{ label: 'Lucy', value: 'lucy' }] },
67+
{
68+
label: 'Other',
69+
disabled: true,
70+
options: [{ label: 'Juice', value: 'juice' }],
71+
},
72+
]}
73+
{...args}
74+
/>
75+
);
76+
};
77+
5078
export const CustomDropdown: Story<SelectProps> = function CustomDropdown(
5179
args,
5280
) {

src/Select/index.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ import styled from 'styled-components';
1212

1313
import { fontSizeFromTheme } from '../styled-utils';
1414

15-
export type OptionType<TValue = unknown> = {
15+
export type OptionType<TValue = unknown> =
16+
| ValueOptionType<TValue>
17+
| GroupedOptionType<TValue>;
18+
19+
export type ValueOptionType<TValue = unknown> = {
1620
label: ReactNode;
1721
value: TValue;
22+
disabled?: boolean;
1823
};
1924

2025
/** As described in https://4x.ant.design/components/select/#components-select-demo-optgroup. */
2126
export type GroupedOptionType<TValue = unknown> = {
2227
label: ReactNode;
23-
options: Array<OptionType<TValue>>;
28+
options: Array<ValueOptionType<TValue>>;
29+
disabled?: boolean;
2430
};
2531

2632
export type { FilterFunc as FilterOptionFunction } from 'rc-select/es/Select';
2733

2834
export type SelectProps<
2935
TValue = unknown,
30-
TOption extends
31-
| OptionType<TValue>
32-
| GroupedOptionType<TValue> = OptionType<TValue>,
36+
TOption extends OptionType<TValue> = OptionType<TValue>,
3337
> = AntdSelectProps<TValue, TOption> & RefAttributes<BaseSelectRef>;
3438

3539
const StyledSelect = styled(AntdSelect)`
@@ -58,10 +62,7 @@ const StyledDropdown = styled.div`
5862
}
5963
`;
6064

61-
function SelectInner<
62-
TValue,
63-
TOptionType extends OptionType<TValue> | GroupedOptionType<TValue>,
64-
>(
65+
function SelectInner<TValue, TOptionType extends OptionType<TValue>>(
6566
{ children, dropdownRender, ...props }: SelectProps<TValue, TOptionType>,
6667
ref: ForwardedRef<BaseSelectRef>,
6768
) {
@@ -87,9 +88,7 @@ function SelectInner<
8788

8889
export const Select = forwardRef(SelectInner) as unknown as (<
8990
TValue = unknown,
90-
TOptionType extends
91-
| OptionType<TValue>
92-
| GroupedOptionType<TValue> = OptionType<TValue>,
91+
TOptionType extends OptionType<TValue> = OptionType<TValue>,
9392
>(
9493
props: SelectProps<TValue, TOptionType> & RefAttributes<BaseSelectRef>,
9594
) => ReactElement) & {

0 commit comments

Comments
 (0)