generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(TimeRangeSelector): added new input Time Range Selector #216
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
045bc96
feat(TimeRangeSelector): added new input TimeRangeSelector
NasgulNexus b029723
fix: input
NasgulNexus 7b02fd4
feat: add TimeRangeSelectorView
NasgulNexus 4a8a450
fix: remarks
NasgulNexus f0408c0
fix: typecheck
NasgulNexus 76da9d7
fix: new logic
NasgulNexus 5c27a80
fix: regexp and fix filter
NasgulNexus e28fbe0
fix: add select disabled
NasgulNexus 924a49a
test: fix test
NasgulNexus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/lib/kit/components/Inputs/TimeRangeSelector/TimeRangeSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React from 'react'; | ||
|
||
import cloneDeep from 'lodash/cloneDeep'; | ||
import isString from 'lodash/isString'; | ||
import set from 'lodash/set'; | ||
|
||
import {FieldValue, ObjectIndependentInput, ValidateError, isStringSpec} from '../../../../core'; | ||
|
||
import {TimeRangeSelect} from './components'; | ||
import {filterTimeArray} from './utils'; | ||
|
||
const START_TIME = 'start'; | ||
const END_TIME = 'end'; | ||
|
||
export const TimeRangeSelector: ObjectIndependentInput = (props) => { | ||
const {spec, input, name, Layout} = props; | ||
|
||
const {startTimeSpec, endTimeSpec} = React.useMemo(() => { | ||
let startTimeSpec, endTimeSpec; | ||
|
||
if (spec.properties?.[START_TIME] && isStringSpec(spec.properties[START_TIME])) { | ||
const _spec = cloneDeep(spec.properties[START_TIME]); | ||
|
||
startTimeSpec = _spec; | ||
} | ||
|
||
if (spec.properties?.[END_TIME] && isStringSpec(spec.properties[END_TIME])) { | ||
const _spec = cloneDeep(spec.properties[END_TIME]); | ||
|
||
endTimeSpec = _spec; | ||
} | ||
|
||
return {startTimeSpec, endTimeSpec}; | ||
}, [spec.properties]); | ||
|
||
const defaultTimeOptions = React.useMemo(() => { | ||
const times: string[] = []; | ||
const totalMinutesInDay = 24 * 60; | ||
const step = spec.viewSpec.timeRangeSelectorParams?.timeStep || 60; | ||
|
||
for (let minutes = 0; minutes < totalMinutesInDay; minutes += step) { | ||
const hours = Math.floor(minutes / 60); | ||
const mins = minutes % 60; | ||
times.push(`${String(hours).padStart(2, '0')}:${String(mins).padStart(2, '0')}`); | ||
} | ||
|
||
return times.map((time) => ({id: time, content: time, value: time})); | ||
}, [spec.viewSpec.timeRangeSelectorParams?.timeStep]); | ||
|
||
const {startTimeOptions, endTimeOptions} = React.useMemo(() => { | ||
let startTimeOptions = defaultTimeOptions.slice(0, -1); | ||
let endTimeOptions = defaultTimeOptions.slice(1); | ||
|
||
if (input.value?.[START_TIME] && isString(input.value[START_TIME])) { | ||
endTimeOptions = filterTimeArray( | ||
defaultTimeOptions, | ||
input.value[START_TIME], | ||
'greater', | ||
); | ||
} | ||
|
||
if (input.value?.[END_TIME] && isString(input.value[END_TIME])) { | ||
startTimeOptions = filterTimeArray(defaultTimeOptions, input.value[END_TIME], 'less'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment about duplication |
||
|
||
return {startTimeOptions, endTimeOptions}; | ||
}, [defaultTimeOptions, input.value]); | ||
|
||
const parentOnChange = React.useCallback( | ||
(childName: string, childValue: FieldValue, childErrors?: Record<string, ValidateError>) => | ||
input.onChange( | ||
(currentValue) => | ||
set({...currentValue}, childName.split(`${name}.`).join(''), childValue), | ||
childErrors, | ||
), | ||
[input, name], | ||
); | ||
|
||
if (!startTimeSpec || !endTimeSpec) { | ||
return null; | ||
} | ||
|
||
const content = ( | ||
<React.Fragment> | ||
<TimeRangeSelect | ||
spec={startTimeSpec} | ||
name={`${name}.${START_TIME}`} | ||
options={startTimeOptions} | ||
value={input.value?.[START_TIME]} | ||
placeholder={startTimeOptions[0]?.content} | ||
handleChange={(value) => parentOnChange(START_TIME, value[0])} | ||
props={props} | ||
/> | ||
<TimeRangeSelect | ||
spec={endTimeSpec} | ||
name={`${name}.${END_TIME}`} | ||
options={endTimeOptions} | ||
value={input.value?.[END_TIME]} | ||
placeholder={endTimeOptions[0]?.content} | ||
handleChange={(value) => parentOnChange(END_TIME, value[0])} | ||
props={props} | ||
/> | ||
</React.Fragment> | ||
); | ||
|
||
if (Layout) { | ||
return <Layout {...props}>{content}</Layout>; | ||
} | ||
|
||
return <React.Fragment>{content}</React.Fragment>; | ||
}; |
Binary file added
BIN
+10.5 KB
...lector.visual.test.tsx-snapshots/TimeRangeSelector-View-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.85 KB
...Selector.visual.test.tsx-snapshots/TimeRangeSelector-View-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.4 KB
...ector.visual.test.tsx-snapshots/TimeRangeSelector-View-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.65 KB
...elector.visual.test.tsx-snapshots/TimeRangeSelector-View-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13.7 KB
...tor.visual.test.tsx-snapshots/TimeRangeSelector-default-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.1 KB
...ector.visual.test.tsx-snapshots/TimeRangeSelector-default-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13.2 KB
...or.visual.test.tsx-snapshots/TimeRangeSelector-default-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.6 KB
...ctor.visual.test.tsx-snapshots/TimeRangeSelector-default-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15 KB
...sual.test.tsx-snapshots/TimeRangeSelector-default-value-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.2 KB
...visual.test.tsx-snapshots/TimeRangeSelector-default-value-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.5 KB
...ual.test.tsx-snapshots/TimeRangeSelector-default-value-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.5 KB
...isual.test.tsx-snapshots/TimeRangeSelector-default-value-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.2 KB
...visual.test.tsx-snapshots/TimeRangeSelector-description-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.4 KB
...r.visual.test.tsx-snapshots/TimeRangeSelector-description-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.6 KB
...isual.test.tsx-snapshots/TimeRangeSelector-description-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.7 KB
....visual.test.tsx-snapshots/TimeRangeSelector-description-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.3 KB
...or.visual.test.tsx-snapshots/TimeRangeSelector-required-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.6 KB
...ctor.visual.test.tsx-snapshots/TimeRangeSelector-required-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13.8 KB
...r.visual.test.tsx-snapshots/TimeRangeSelector-required-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.2 KB
...tor.visual.test.tsx-snapshots/TimeRangeSelector-required-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions
39
src/lib/kit/components/Inputs/TimeRangeSelector/__tests__/TimeRangeSelector.visual.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
|
||
import {TIME_RANGE_SELECTOR, VALUE} from './helpers'; | ||
|
||
import {test} from '~playwright/core'; | ||
import {DynamicForm} from '~playwright/core/DynamicForm'; | ||
import {DynamicView} from '~playwright/core/DynamicView'; | ||
|
||
test.describe('TimeRangeSelector', () => { | ||
test('default', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={TIME_RANGE_SELECTOR.default} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
|
||
test('default value', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={TIME_RANGE_SELECTOR.defaultValue} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
|
||
test('required', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={TIME_RANGE_SELECTOR.required} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
|
||
test('description', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={TIME_RANGE_SELECTOR.desription} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
}); | ||
|
||
test('TimeRangeSelector View', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicView spec={TIME_RANGE_SELECTOR.default} value={VALUE} />); | ||
|
||
await expectScreenshot(); | ||
}); |
117 changes: 117 additions & 0 deletions
117
src/lib/kit/components/Inputs/TimeRangeSelector/__tests__/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import {FormValue, ObjectSpec, SpecTypes} from '../../../../../core'; | ||
|
||
export const TIME_RANGE_SELECTOR: Record<string, ObjectSpec> = { | ||
default: { | ||
type: SpecTypes.Object, | ||
properties: { | ||
start: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'Start of launch', | ||
}, | ||
}, | ||
end: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'End of launch', | ||
}, | ||
}, | ||
}, | ||
viewSpec: { | ||
type: 'time_range_selector', | ||
layout: 'transparent', | ||
}, | ||
}, | ||
defaultValue: { | ||
defaultValue: { | ||
start: '03:00', | ||
end: '06:00', | ||
}, | ||
type: SpecTypes.Object, | ||
properties: { | ||
start: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'Start of launch', | ||
}, | ||
}, | ||
end: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'End of launch', | ||
}, | ||
}, | ||
}, | ||
viewSpec: { | ||
type: 'time_range_selector', | ||
layout: 'transparent', | ||
}, | ||
}, | ||
required: { | ||
type: SpecTypes.Object, | ||
properties: { | ||
start: { | ||
required: true, | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'Start of launch', | ||
}, | ||
}, | ||
end: { | ||
required: true, | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'End of launch', | ||
}, | ||
}, | ||
}, | ||
viewSpec: { | ||
type: 'time_range_selector', | ||
layout: 'transparent', | ||
}, | ||
}, | ||
desription: { | ||
type: SpecTypes.Object, | ||
properties: { | ||
start: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'Start of launch', | ||
layoutDescription: 'End of launch Description', | ||
}, | ||
}, | ||
end: { | ||
type: SpecTypes.String, | ||
viewSpec: { | ||
type: 'select', | ||
layout: 'row', | ||
layoutTitle: 'End of launch', | ||
layoutDescription: 'End of launch Description', | ||
}, | ||
}, | ||
}, | ||
viewSpec: { | ||
type: 'time_range_selector', | ||
layout: 'transparent', | ||
}, | ||
}, | ||
}; | ||
|
||
export const VALUE: FormValue = { | ||
start: '10:00', | ||
end: '20:00', | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about duplication