Files
teleport/web/packages/shared/components/FieldSelect/FieldSelect.tsx
T
Lisa Kim eb1e69b60e [Discover] Implement Kube Flow Day 1 (#1268)
* In addition set up msw lib for storybook with suspense
2022-10-21 16:21:17 +00:00

89 lines
2.2 KiB
TypeScript

/*
Copyright 2019 Gravitational, Inc.
Licensed 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 from 'react';
import { Box, LabelInput } from 'design';
import { useRule } from 'shared/components/Validation';
import Select, { Props as SelectProps } from './../Select';
export default function FieldSelect({
label,
labelTip,
value,
options,
onChange,
placeholder,
maxMenuHeight,
isClearable,
isMulti,
menuPosition,
rule = defaultRule,
isSearchable = false,
isSimpleValue = false,
autoFocus = false,
isDisabled = false,
...styles
}: Props) {
const { valid, message } = useRule(rule(value));
const hasError = Boolean(!valid);
const labelText = hasError ? message : label;
return (
<Box mb="4" {...styles}>
{label && (
<LabelInput htmlFor={'select'} hasError={hasError}>
{labelText}
{labelTip && <LabelTip text={labelTip} />}
</LabelInput>
)}
<Select
inputId="select"
menuPosition={menuPosition}
hasError={hasError}
isSimpleValue={isSimpleValue}
isSearchable={isSearchable}
isClearable={isClearable}
value={value}
onChange={onChange}
options={options}
maxMenuHeight={maxMenuHeight}
placeholder={placeholder}
isMulti={isMulti}
autoFocus={autoFocus}
isDisabled={isDisabled}
/>
</Box>
);
}
const defaultRule = () => () => ({ valid: true });
const LabelTip = ({ text }) => (
<span
css={{ fontWeight: 'normal', textTransform: 'none' }}
>{` - ${text}`}</span>
);
type Props = SelectProps & {
autoFocus?: boolean;
label?: string;
rule?: (options: unknown) => () => unknown;
// styles
[key: string]: any;
};