Skip to content

Commit

Permalink
fix: add select disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
NasgulNexus committed Aug 12, 2024
1 parent 5c27a80 commit e28fbe0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const TimeRangeSelector: ObjectIndependentInput = (props) => {
return spec.enum.map((id) => ({
id,
value: id,
text: spec.description?.[id] || id,
content: spec.viewSpec.selectParams?.meta?.[id] ? (
<div key={id}>
<Text>{spec.description?.[id] || id}</Text>
Expand Down Expand Up @@ -114,7 +113,6 @@ export const TimeRangeSelector: ObjectIndependentInput = (props) => {
name={`${name}.${START_TIME}`}
options={startTimeOptions}
value={input.value?.[START_TIME]}
placeholder={startTimeOptions[0]?.text}
handleChange={(value) => parentOnChange(START_TIME, value[0])}
props={props}
/>
Expand All @@ -123,7 +121,6 @@ export const TimeRangeSelector: ObjectIndependentInput = (props) => {
name={`${name}.${END_TIME}`}
options={endTimeOptions}
value={input.value?.[END_TIME]}
placeholder={endTimeOptions[0]?.text}
handleChange={(value) => parentOnChange(END_TIME, value[0])}
props={props}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ interface TimeRangeSelectProps {
name: string;
options: SelectOption<string>[];
value?: FieldValue;
placeholder?: string;
handleChange: (value: string[]) => void;
props: IndependentInputProps<
FieldObjectValue,
Expand All @@ -40,7 +39,6 @@ export const TimeRangeSelect: React.FC<TimeRangeSelectProps> = ({
spec,
options,
value,
placeholder,
props,
handleChange,
}) => {
Expand Down Expand Up @@ -75,7 +73,7 @@ export const TimeRangeSelect: React.FC<TimeRangeSelectProps> = ({
options={options}
onUpdate={handleChange}
disabled={spec.viewSpec.disabled}
placeholder={spec.viewSpec.placeholder || placeholder}
placeholder={spec.viewSpec.placeholder}
filterPlaceholder={spec.viewSpec.selectParams?.filterPlaceholder}
renderOption={renderOption}
getOptionHeight={getOptionHeight}
Expand Down
23 changes: 0 additions & 23 deletions src/lib/kit/components/Inputs/TimeRangeSelector/utils.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/lib/kit/components/Inputs/TimeRangeSelector/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';

import {Text} from '@gravity-ui/uikit';

export const filterTimeArray = (
times: {id: string; value: string; content: string | JSX.Element; key: string}[],
cutoff: string,
direction: 'greater' | 'less',
) => {
const isTimeFormat = (value: string) => /^\d{1,2}:\d{2}$/.test(value);

const compareValues = (a: string, b: string) => {
if (isTimeFormat(a) && isTimeFormat(b)) {
return direction === 'less' ? a >= b : a <= b;
} else {
const aNum = parseInt(a, 10);
const bNum = parseInt(b, 10);

return direction === 'less' ? aNum >= bNum : aNum <= bNum;
}
};

return times.map((time) => {
const disabled = compareValues(time.value, cutoff);

return {
...time,
content: disabled ? <Text color="secondary">{time.content}</Text> : time.content,
disabled,
};
});
};

export const validateArray = (arr: {value: string}[]) =>
arr.every((obj) => /^(\d+|\d{1,2}:\d{1,2})$/.test(obj.value));
8 changes: 8 additions & 0 deletions src/stories/ObjectTimeRangeSelector.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const baseSpec: ObjectSpec = {
type: 'select',
layout: 'row',
layoutTitle: 'Start of launch',
placeholder: '00:00',
selectParams: {
filterPlaceholder: 'Start time',
},
},
},
end: {
Expand Down Expand Up @@ -78,6 +82,10 @@ const baseSpec: ObjectSpec = {
type: 'select',
layout: 'row',
layoutTitle: 'End of launch',
placeholder: '01:00',
selectParams: {
filterPlaceholder: 'End time',
},
},
},
},
Expand Down

0 comments on commit e28fbe0

Please sign in to comment.