Skip to main content

Select

TkSelect component description.

import { TkSelect } from '@takeoff-ui/react'

Basic

View Code
<TkSelect
label="Text Input"
options={[
{ value: "female", label: "Female" },
{ value: "male", label: "Male" },
{ value: "other", label: "Other" },
]}
hint="Hint text"
placeholder="Placeholder text"
/>

Filter

You can perform filtering either server-side or client-side using the filter emit.


View Code
const selectOptions = [
{ value: "female", label: "Female", searchField: "XX" },
{ value: "male", label: "Male", searchField: "XY" },
{ value: "other", label: "Other", searchField: "00" },
];

const [value, setValue] = useState();
const [value1, setValue1] = useState();

return (
<div>
<TkSelect
editable
label="Default Filter"
options={selectOptions}
value={value}
onTkChange={(e) => setValue(e.detail)}
/>
<br />
<TkSelect
editable
label="Custom Filter Function"
options={selectOptions}
filter={async (text: string, options: any[]) => {
return options.filter(
(item) =>
item.searchField.toLowerCase().indexOf(text.toLowerCase()) > -1
);
}}
value={value1}
onTkChange={(e) => setValue1(e.detail)}
/>
</div>
);

Loading

You can use loading prop for waiting the response from the server-side.

View Code
const selectOptions = [
{ value: "female", label: "Female"},
{ value: "male", label: "Male"},
{ value: "other", label: "Other"},
];

const [value, setValue] = useState();
const [loading, setLoading] = useState(false);

const fetchData = async (text: string) => {
return new Promise((resolve) => {
setTimeout(() => {
let response;
response = options.filter(
(item) => item.label?.toLowerCase().indexOf(text?.toLowerCase()) > -1
);

resolve(response);
}, 2000);
});
};

return (
<div>
<TkSelect
editable
label="Loading Prop"
loading={loading}
options={selectOptions}
filter={async (text: string, selectOptions: any[]) => {
setLoading(true);
const response = await fetchData(text);
setLoading(false);
return response;
}}
value={value}
onTkChange={(e) => {
setValue(e.detail);
}}
/>
</div>
);

Custom Item

You can provide a template as an HTML string using the optionHtml prop.

View Code
<TkSelect
label="Custom Item"
optionLabelKey="name"
optionValueKey="code"
options={[
{
code: "SAW",
name: "Sabiha Gökçen Havalimanı",
country: "İstanbul",
},
{ code: "ESB", name: "Esenboğa Havalimanı", country: "Ankara" },
{ code: "AYT", name: "Antalya Havalimanı", country: "Antalya" },
]}
optionHtml={(item) => {
return `<div style="display: flex; flex-direction: column;">
<div style="displaY: flex;justify-content: space-between;">
<div style="font-weight: bold;">${item.name}</div>
<div style="color: var(--primary-base)">${item.code}</div>
</div>
<div>${item.country}</div>
</div>`;
}}
value={value}
onTkChange={(e) => setValue(e.detail)}
/>

Multiple Select

View Code
<TkSelect
label="Multiple Input"
multiple
options={[
{ value: "female", label: "Female" },
{ value: "male", label: "Male" },
{ value: "other", label: "Other" },
]}
hint="Hint text"
/>

Size

The "size" prop can be used with one of the sizes "small", "base", and "large". The default value is "base".

View Code
<TkSelect
label="Text Input"
options={[
{ value: "female", label: "Female" },
{ value: "male", label: "Male" },
{ value: "other", label: "Other" },
]}
hint="Hint text"
placeholder="Placeholder text"
size="small"
value={value}
onTkChange={(e) => setValue(e.detail)}
/>
<TkSelect
label="Text Input"
options={[
{ value: "female", label: "Female" },
{ value: "male", label: "Male" },
{ value: "other", label: "Other" },
]}
hint="Hint text"
placeholder="Placeholder text"
size="base"
value={value1}
onTkChange={(e) => setValue1(e.detail)}
/>
<TkSelect
label="Text Input"
options={[
{ value: "female", label: "Female" },
{ value: "male", label: "Male" },
{ value: "other", label: "Other" },
]}
hint="Hint text"
placeholder="Placeholder text"
size="large"
value={value2}
onTkChange={(e) => setValue2(e.detail)}
/>

State

View Code
<TkSelect
label="Error"
placeholder="Error"
invalid={true}
error="Bu alan zorunludur"
/>
<TkSelect label="Readonly" placeholder="Readonly" readonly />
<TkSelect label="Disabled" placeholder="Disabled" disabled />

Select All

You can add selectAll option and edit the label via selectAllLabel.


Select All Event: false
View Code
  const handleSelectAll = e => {
const selected = e.detail;
setSelectAll(selected);
};

<TkSelect
label="Select All"
options={[
{ value: "female", label: "Female" },
{ value: "male", label: "Male" },
{ value: "other", label: "Other" },
]}
selectAllLabel="Select All"
multiple
selectAll={true}
onTkSelectAll={handleSelectAll}
/>
<div>Selected All: { selectAll ? 'true' : 'false' }</div>

Visible Item Count

Controls how many selected items are displayed before showing "+N others" in multiple selection mode.


View Code
<TkSelect
label="Select with Visible Item Count"
placeholder="Select multiple options"
multiple
visibleItemCount={3}
options={[
{ value: "1", label: "Option 1" },
{ value: "2", label: "Option 2" },
{ value: "3", label: "Option 3" },
{ value: "4", label: "Option 4" },
{ value: "5", label: "Option 5" },
{ value: "6", label: "Option 6" },
{ value: "7", label: "Option 7" },
{ value: "8", label: "Option 8" },
]}
hint="Only 3 items will be visible before showing +N others"
/>

OptionValueKey

Allows usage of Key for option values.

Selected Key's value:

Selected Object's value:

View Code
 
const selectOptions = [
{value: "female", label: "Female", symbol:"XX" },
{value: "male", label: "Male", symbol: "XY" },
{value: "other", label: "Other", symbol: "00" },
];
const [selectValue, setSelectValue]=useState();
const [selectObjectValue, setObjectSelectValue]=useState();

return(
<TkSelect
editable
label="Key Value Select"
value={selectValue}
options={selectOptions}
optionValueKey="symbol"
onTkChange={(e)=>{
setSelectValue(e.detail);
}}
/>
p><b>Selected Key's value:</b> {selectValue}</p>
<TkSelect
editable
label="Object Value Select"
options={selectOptions}
onTkChange={(e)=>{
setObjectSelectValue(e.detail);
}}
/>
<p><b>Selected Key's value:</b> {JSON.stringify(selectObjectValue)}</p>
);

Icon

You can use the icon prop to add an icon to the select input.

View Code
<TkSelect
icon={{ name: 'key', color: 'red' }}
label="Text Input"
options={[
{ value: "female", label: "Female" },
{ value: "male", label: "Male" },
{ value: "other", label: "Other" },
]}
hint="Hint text"
placeholder="Placeholder text"
/>

Clearable

This example clears the input of the select.

View Code
<TkSelect label="Clearable Select" options={options} value="Option 1" clearable />

Grouped Options

Display options under section headers using groupNameKey and groupOptionsKey.

View Code
<TkSelect
label="Grouped Cities"
placeholder="Select a city"
options={[
{
groupName: 'Marmara',
options: [
{ value: 'ankara', label: 'Ankara' },
{ value: 'istanbul', label: 'İzmir' },
{ value: 'izmir', label: 'İstanbul' },
],
},
{
groupName: 'Batı Karadeniz',
options: [
{ value: 'bolu', label: 'Bolu' },
{ value: 'bartin', label: 'Bartın' },
{ value: 'zonguldak', label: 'Zonguldak' },
],
},
{
groupName: 'İç Anadolu',
options: [
{ value: 'yozgat', label: 'Yozgat' },
{ value: 'kirikkale', label: 'Kırıkkale' },
],
},
]}
groupNameKey="groupName"
groupOptionsKey="options"
optionLabelKey="label"
optionValueKey="value"
/>

Chip Options

Sets options for all chips rendered in multiple selection mode.

View Code
<TkSelect
label="Test Chip Options"
options={['Apple', 'Banana', 'Cherry']}
multiple
value={value}
onTkChange={(e) => setValue(e.detail)}
chipOptions={{
icon: 'star',
type: 'outlined',
size: 'large',
variant: 'success',
}}
/>

API

Props

NameTypeDefaultDescription
booleanfalseEnables users to enter custom values that are not part of the predefined options.
IChipOptionsnullSets options for all chips rendered in multiple selection mode.
booleanfalseIndicates whether the input can be cleared
booleanfalseIf true, the user cannot interact with the input.
string'match-parent'Determines the width of the dropdown. Accepts values like 'match-parent', 'auto', or a specific width in '300px'.
booleanfalseThis property determines whether the input field within the select box is editable.
string'Nooptionsavailable'The message to display when there is no data available.
stringnullThis is the error message that will be displayed.
Functionthis.defaultFilterFunction used to filter current options based on the input value. Comes with a default filter function, but can be overridden with a custom function.
number0Sets the delay (in ms) before triggering the filter function.
string'label'The key to use for option group names. Required if grouped options are used.
string'options'The key to use for accessing grouped options array. Required if grouped options are used.
stringnullProvided a hint or additional information about the input.
IIconOptions, stringnullThe icon displayed in the select box.
booleanfalseIndicates whether the input is in an invalid state
stringnullDefines the label for the element.
booleanfalseRepresents whether the options are fethecd from service or not. If true renders spinner in options dropdown.
booleannullIf true the user can make multiple selections.
stringnullThe name of the control, which is submitted with the form data.
FunctionnullProvides a function to customize the options.
string'label'The key to use for option labels
stringnullThe key to use for option values
any[]nullThe list of options to be displayed in the select box.
stringnullPlaceholder text displayed when the input is empty.
booleanfalseIf true, the user cannot modify the value.
booleanfalseIf true enables selectAll option
string'All'Sets the label of the selectAll option
booleanfalseDisplays a red asterisk (*) next to the label for visual emphasis.
"base", "large", "small"'base'Sets size for the component.
anynullThe value of the input.
numbernullThe number of items to show in the collapsed select before listing +N others.

Events

NameDescription
tk-changeEmitted when the value has changed.
tk-select-allEmitted when the selectAll option is changed

Slots

NameDescription
empty-dataSet how the dropdown will appear when there is no data