Skip to content

Commit

Permalink
feat: adds select component (EightfoldAI#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktorlakonda-eightfold authored Jun 13, 2022
1 parent cfff49b commit bb258e9
Show file tree
Hide file tree
Showing 12 changed files with 739 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/__snapshots__/storybook.test.js.snap
Git LFS file not shown
20 changes: 15 additions & 5 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const Dropdown: FC<DropdownProps> = ({
offset = 0,
positionStrategy = 'absolute',
onVisibleChange,
showDropdown,
disabled,
closeOnDropdownClick = true,
}) => {
Expand All @@ -56,16 +57,18 @@ export const Dropdown: FC<DropdownProps> = ({
});

const toggle: Function =
(show: boolean): Function =>
(show: boolean, showDropdown = (show: boolean) => show): Function =>
(e: SyntheticEvent): void => {
// to control the toggle behaviour
const updatedShow = showDropdown(show);
if (PREVENT_DEFAULT_TRIGGERS.includes(trigger)) {
e.preventDefault();
}
setClosing(!show);
setClosing(!updatedShow);
timeout && clearTimeout(timeout);
timeout = setTimeout(
() => {
setVisible(show);
setVisible(updatedShow);
},
!show ? ANIMATION_DURATION : 0
);
Expand Down Expand Up @@ -136,7 +139,9 @@ export const Dropdown: FC<DropdownProps> = ({
style={dropdownStyles}
className={dropdownClasses}
tabIndex={0}
onClick={closeOnDropdownClick ? toggle(false) : null}
onClick={
closeOnDropdownClick ? toggle(false, showDropdown) : null
}
id={dropdownId}
>
{overlay}
Expand All @@ -149,7 +154,12 @@ export const Dropdown: FC<DropdownProps> = ({
style={style}
ref={reference}
{...(TRIGGER_TO_HANDLER_MAP_ON_LEAVE[trigger]
? { [TRIGGER_TO_HANDLER_MAP_ON_LEAVE[trigger]]: toggle(false) }
? {
[TRIGGER_TO_HANDLER_MAP_ON_LEAVE[trigger]]: toggle(
false,
showDropdown
),
}
: {})}
>
{getReference()}
Expand Down
9 changes: 8 additions & 1 deletion src/components/Dropdown/Dropdown.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Placement, Strategy } from '@floating-ui/react-dom';
import React from 'react';
import { Placement, Strategy } from '@floating-ui/react-dom';

export interface DropdownProps {
/**
Expand All @@ -11,6 +11,13 @@ export interface DropdownProps {
* @param visible {boolean}
*/
onVisibleChange?: (visible: boolean) => void;
/**
* Callback to control the show/hide behavior of the dropdown.
* triggered before the visible change
* @param show {boolean}
* @returns true or false.
*/
showDropdown?: (show: boolean) => boolean;
/**
* The dropdown content
*/
Expand Down
18 changes: 18 additions & 0 deletions src/components/Inputs/Input.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ export interface TextInputProps extends InputProps<HTMLInputElement> {
* @default false
*/
required?: boolean;

/**
* option to show the clear input button.
* default is true for backward compatibility
* @default true
*/
clearable?: boolean;

/**
* onclear event handler.
*/
onClear?: React.MouseEventHandler<Element>;
}

export interface InputProps<T>
Expand Down Expand Up @@ -283,4 +295,10 @@ export interface InputProps<T>
* @default 10
*/
waitInterval?: number;

/**
* input readonly.
* @default false
*/
readonly?: boolean;
}
22 changes: 20 additions & 2 deletions src/components/Inputs/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState } from 'react';
import React, { FC, useEffect, useState } from 'react';
import { ButtonSize, DefaultButton } from '../../Button';
import { Icon, IconName, IconSize } from '../../Icon';
import { Label } from '../../Label';
Expand Down Expand Up @@ -32,18 +32,21 @@ export const TextInput: FC<TextInputProps> = ({
numbersOnly = false,
onBlur,
onChange,
onClear,
onFocus,
onKeyDown,
placeholder,
required = false,
readonly = false,
shape = TextInputShape.Rectangle,
style,
theme = TextInputTheme.light,
value,
waitInterval = 10,
clearable = true,
...rest
}) => {
const [clearButtonShown, setClearButtonShown] = useState<boolean>(false);
const [clearButtonShown, _setClearButtonShown] = useState<boolean>(false);
const [inputId] = useState<string>(uniqueId(id || 'input-'));
const inputField: HTMLElement = document.getElementById(inputId);

Expand Down Expand Up @@ -111,12 +114,26 @@ export const TextInput: FC<TextInputProps> = ({
},
]);

useEffect(() => {
if (value?.toString()?.length > 0) {
return setClearButtonShown(true);
}
setClearButtonShown(false);
}, [value]);

const setClearButtonShown = (showClear: boolean) => {
return !clearable
? _setClearButtonShown(false)
: _setClearButtonShown(showClear);
};

const handleOnClear = (_event: React.MouseEvent) => {
_event.preventDefault();
_event.stopPropagation();
if (!!inputField) {
(inputField as HTMLInputElement).value = '';
}
onClear?.(_event);
setClearButtonShown(false);
};

Expand Down Expand Up @@ -168,6 +185,7 @@ export const TextInput: FC<TextInputProps> = ({
tabIndex={0}
type={numbersOnly ? 'number' : htmlType}
value={value}
readOnly={readonly}
/>
{iconProps && (
<div className={iconClassNames}>
Expand Down
1 change: 1 addition & 0 deletions src/components/Pills/Pill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const Pill: FC<PillProps> = React.forwardRef(
]);
const tagClassName: string = mergeClasses([
styles.tagPills,
rest.classNames,
{ [styles.red]: theme === 'red' },
{ [styles.orange]: theme === 'orange' },
{ [styles.yellow]: theme === 'yellow' },
Expand Down
Loading

0 comments on commit bb258e9

Please sign in to comment.