Skip to content

Commit c459c71

Browse files
committed
feat(Select): add default prop to Select component
1 parent 67ddc6f commit c459c71

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

packages/select/src/components/Select.tsx

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import React from 'react';
22
import get from 'lodash.get';
3+
import {
4+
Button as BPButton,
5+
ButtonProps as BPButtonProps,
6+
} from '@blueprintjs/core';
37
import {
48
Select as BPSelect,
59
SelectProps as BPSelectProps,
@@ -15,6 +19,14 @@ interface FormikSelectProps<T>
1519
extends Omit<BPSelectProps<T>, 'itemRenderer' | 'onItemSelect'> {
1620
itemRenderer?: ItemRenderer<T>;
1721
onItemSelect?: (item: T, event?: React.SyntheticEvent<HTMLElement>) => void;
22+
input?: (props: {
23+
activeItem: T;
24+
label: string;
25+
text: string;
26+
value: string | number;
27+
}) => React.ReactNode;
28+
placeholder?: string;
29+
buttonProps?: Partial<BPButtonProps>;
1830
}
1931
interface SelectProps<T>
2032
extends Omit<FieldConfig, 'children' | 'as' | 'component'>,
@@ -23,12 +35,6 @@ interface SelectProps<T>
2335
valueAccessor?: string;
2436
labelAccessor?: string;
2537
textAccessor?: string;
26-
input: (props: {
27-
activeItem: T;
28-
label: string;
29-
text: string;
30-
value: string | number;
31-
}) => React.ReactNode;
3238
}
3339
interface FieldToSelectProps<T> extends FormikSelectProps<T>, FieldProps {
3440
valueAccessor: string;
@@ -60,6 +66,8 @@ function transformSelectToFieldProps<T extends SelectOptionProps>({
6066
form: { touched, errors, ...form },
6167
meta,
6268
input,
69+
buttonProps,
70+
placeholder,
6371
valueAccessor,
6472
labelAccessor,
6573
textAccessor,
@@ -73,15 +81,25 @@ function transformSelectToFieldProps<T extends SelectOptionProps>({
7381
(item) => getAccessor(_valueAccessor, item) === field.value
7482
) as T;
7583

76-
const children = input
77-
? input({
78-
activeItem,
79-
text: getAccessor(_textAccessor, activeItem),
80-
label: getAccessor(_labelAccessor, activeItem),
81-
value: getAccessor(_valueAccessor, activeItem),
82-
})
83-
: props.children;
84+
const activeItemText = getAccessor(_textAccessor, activeItem);
85+
const activeItemLabel = getAccessor(_labelAccessor, activeItem);
86+
const activeItemValue = getAccessor(_valueAccessor, activeItem);
8487

88+
const children = input ? (
89+
input({
90+
activeItem,
91+
text: activeItemText,
92+
label: activeItemLabel,
93+
value: activeItemValue,
94+
})
95+
) : (
96+
<BPButton
97+
text={activeItemText}
98+
rightIcon="double-caret-vertical"
99+
placeholder={placeholder || 'Select an item...'}
100+
{...buttonProps}
101+
/>
102+
);
85103
const itemPredicate: ItemPredicate<T> = (query, item, _index, exactMatch) => {
86104
const text = getAccessor(_textAccessor, item);
87105
const label = getAccessor(_labelAccessor, item);

stories/SelectPage.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const SelectPage = () => {
5656
<Select
5757
items={TOP_100_FILMS}
5858
name={'number'}
59-
input={({ text }) => <button>{text}</button>}
6059
valueAccessor={'year'}
6160
labelAccessor={'year'}
6261
textAccessor={'title'}

0 commit comments

Comments
 (0)