Skip to content

Commit

Permalink
- enable hidden, disabled props in OptionListItem for <Select/>
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfxiao committed Oct 23, 2023
1 parent 641099d commit 86d37cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 4.9.10

- enable `hidden`, `disabled` props in OptionListItem for `<Select/>`

# 4.9.9

- `customStyleOptionListWrapper` for `<Select/>`
Expand Down
2 changes: 1 addition & 1 deletion docs/v4-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ import 'react-inputs-validation/lib/react-inputs-validation.min.css';
|keyword | Opt | Str |Show a keyword for search box. | '' |
|**validate** |**Opt**|**Bool**| |**false** |
|**validationCallback** |**Opt**|**Func**| |**none** |
|**optionList** |**Req**|**Array**|**[{id: '1', name: 'Twin Peaks', icon: 'optional']** |**[]** |
|**optionList** |**Req**|**Array**|**[{id: '1', name: 'Twin Peaks', icon: 'optional', hidden: false, disabled: false}]** |**[]** |
|classNameSelect | Opt | Str | | "" |
|classNameWrapper | Opt | Str | | "" |
|classNameContainer | Opt | Str | | "" |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-inputs-validation",
"version": "4.9.9",
"version": "4.9.10",
"description": "A react component for form inputs validation.",
"main": "index.js",
"types": "./lib/index.d.ts",
Expand Down
44 changes: 27 additions & 17 deletions src/js/Inputs/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ interface OptionListItem {
id: string;
name: string;
icon?: string;
hidden?: boolean;
disabled?: boolean;
}
interface Props {
attributesWrapper?: React.HTMLAttributes<HTMLButtonElement>;
Expand Down Expand Up @@ -213,7 +215,7 @@ const component: React.FC<Props> = ({
const $elItemsWrapper: { [key: string]: any } | null = $itemsWrapper;
const $itemsRef: { [key: string]: any } = [];
const filteredOptionList = useMemo(() => {
let res = optionList;
let res = optionList.filter(i => !i.hidden);
if (res.length) {
if (stateKeyword[0]) {
res = optionList.filter(i => i.name.toLowerCase().includes(stateKeyword[0].toLowerCase()));
Expand Down Expand Up @@ -389,7 +391,10 @@ const component: React.FC<Props> = ({
}
}, []);
const handleOnItemClick = useCallback(
(item: object, e: React.MouseEvent<HTMLElement>) => {
(item: OptionListItem, e: React.MouseEvent<HTMLElement>) => {
if (item.disabled) {
return;
}
handleOnChange(item, e);
stateKeyword[1]('');
},
Expand Down Expand Up @@ -603,20 +608,25 @@ const component: React.FC<Props> = ({
let optionListHtml;
const item = getItem(optionList, String(value));
if (filteredOptionList.length) {
optionListHtml = filteredOptionList.map((i, k) => (
<Option
key={k}
index={k}
id={`react-inputs-validation__select_option-${i.id}`}
className={String(i.id) === String(value) ? `${selectOptionListItemClass} ${reactInputsValidationCss['active']}` : `${selectOptionListItemClass}`}
item={i}
customStyleOptionListItem={customStyleOptionListItem}
onClick={handleOnItemClick}
onMouseOver={handleOnItemMouseOver}
onMouseMove={handleOnItemMouseMove}
onMouseOut={handleOnItemMouseOut}
/>
));
optionListHtml = filteredOptionList.map((i, k) => {
const activeClassName = String(i.id) === String(value) ? reactInputsValidationCss['active'] : '';
const disabledClassName = i.disabled ? reactInputsValidationCss['disabled'] : '';
const itemclassName = `${selectOptionListItemClass} ${activeClassName} ${disabledClassName}`;
return (
<Option
key={k}
index={k}
id={`react-inputs-validation__select_option-${i.id}`}
className={itemclassName}
item={i}
customStyleOptionListItem={customStyleOptionListItem}
onClick={handleOnItemClick}
onMouseOver={handleOnItemMouseOver}
onMouseMove={handleOnItemMouseMove}
onMouseOut={handleOnItemMouseOut}
/>
);
});
} else {
if (showSearch) {
optionListHtml = (
Expand Down Expand Up @@ -724,7 +734,7 @@ interface OptionProps {
className?: string;
item?: OptionListItem;
customStyleOptionListItem?: React.CSSProperties;
onClick?: (res: object, e: React.MouseEvent<HTMLElement>) => void;
onClick?: (res: OptionListItem, e: React.MouseEvent<HTMLElement>) => void;
onMouseOver?: (res: number) => void;
onMouseMove?: () => void;
onMouseOut?: () => void;
Expand Down
6 changes: 6 additions & 0 deletions src/js/Inputs/react-inputs-validation.css
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ input[type='checkbox'] {
display: block;
}

.select__options-item.disabled{
color: #ccc !important;
background: #f8f8f8 !important;
cursor: not-allowed;
}

.select__options-item span {
padding: 0 10px;
}
Expand Down

0 comments on commit 86d37cc

Please sign in to comment.