This repository was archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
feat(select): enhanced select #823
Merged
Merged
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e77cbfd
fix: native select working. WIP: enhanced select
3e6c5c1
WIP: enhanced select
dd6486e
WIP: enhanced select working - not opening
51f30d3
WIP: enhanced
7ed285f
WIP: change handling
b9e9d61
WIP: select
55a34cf
feat: feature complete. wip: helper tet and icon
b5a055c
fix: helper text. WIP: icon
9cef5a7
WIP: icon
80e9218
fix: added icon
9190dd2
WIP test
51822d6
WIP
f7f49c9
fix: some tests
4bb9422
fix: select tests
90cebd1
fix: finished test. WIP: README
6112d16
fix: lint
e8ba4b9
fix: screenshot
ae2f478
fix: notchedoutline
2101791
fix: golden
adc5c11
WIP
f70a766
fix: add readmes
1a68094
fix: update readme
f078550
fix: remove stuff
714af00
fix: dep
d9bc01d
fix: revert
dd4e76e
Merge branch 'feat/mdcweb-typescript-update' into feat/select/3enhanced
615bfc7
fix: PR changes
fa4e93b
Merge branch 'feat/select/3enhanced' of github.com:material-component…
de4ddf6
fix: golden
c5642ae
Update NativeSelect.tsx
6b66735
Update README.md
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,158 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2019 Google, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import * as React from 'react'; | ||
import NativeSelect, { | ||
NativeSelectProps, // eslint-disable-line no-unused-vars | ||
} from './NativeSelect'; | ||
import EnhancedSelect, { | ||
EnhancedSelectProps, // eslint-disable-line no-unused-vars | ||
} from './EnhancedSelect'; | ||
import {MDCSelectFoundation} from '@material/select/foundation'; | ||
|
||
export type BaseSelectProps<T extends HTMLElement> | ||
= (T extends HTMLSelectElement ? NativeSelectProps : EnhancedSelectProps); | ||
|
||
export interface CommonSelectProps { | ||
enhanced: boolean; | ||
className?: string; | ||
disabled?: boolean; | ||
foundation?: MDCSelectFoundation; | ||
value?: string; | ||
selectClassName?: string; | ||
} | ||
|
||
export class BaseSelect<T extends HTMLElement = HTMLSelectElement> | ||
extends React.Component<BaseSelectProps<T>> { | ||
static defaultProps = { | ||
enhanced: false, | ||
selectClassName: '', | ||
}; | ||
|
||
handleFocus = (evt: React.FocusEvent<T>) => { | ||
const {foundation, onFocus} = this.props; | ||
if (foundation) { | ||
foundation.handleFocus(); | ||
} | ||
onFocus && onFocus(evt); | ||
}; | ||
|
||
handleBlur = (evt: React.FocusEvent<T>) => { | ||
const {foundation, onBlur} = this.props; | ||
if (foundation) { | ||
foundation.handleBlur(); | ||
} | ||
onBlur && onBlur(evt); | ||
}; | ||
|
||
handleTouchStart = (evt: React.TouchEvent<T>) => { | ||
const {foundation, onTouchStart} = this.props; | ||
if (foundation) { | ||
foundation.handleClick(this.getNormalizedXCoordinate(evt)); | ||
} | ||
onTouchStart && onTouchStart(evt); | ||
} | ||
|
||
handleMouseDown = (evt: React.MouseEvent<T>) => { | ||
const {foundation, onMouseDown} = this.props; | ||
if (foundation) { | ||
foundation.handleClick(this.getNormalizedXCoordinate(evt)); | ||
} | ||
onMouseDown && onMouseDown(evt); | ||
} | ||
|
||
handleClick = (evt: React.MouseEvent<T>) => { | ||
const {foundation, onClick} = this.props; | ||
if (foundation) { | ||
foundation.handleClick(this.getNormalizedXCoordinate(evt)); | ||
} | ||
onClick && onClick(evt); | ||
} | ||
|
||
handleKeyDown = (evt: React.KeyboardEvent<T>) => { | ||
const {foundation, onKeyDown} = this.props; | ||
if (foundation) { | ||
foundation.handleKeydown(evt.nativeEvent); | ||
} | ||
onKeyDown && onKeyDown(evt); | ||
} | ||
|
||
private isTouchEvent = (evt: MouseEvent | TouchEvent): evt is TouchEvent => { | ||
return Boolean((evt as TouchEvent).touches); | ||
} | ||
|
||
private getNormalizedXCoordinate | ||
= (evt: React.MouseEvent<T> | React.TouchEvent<T>) => { | ||
const targetClientRect = (evt.currentTarget as Element).getBoundingClientRect(); | ||
const xCoordinate | ||
= this.isTouchEvent(evt.nativeEvent) ? evt.nativeEvent.touches[0].clientX : evt.nativeEvent.clientX; | ||
return xCoordinate - targetClientRect.left; | ||
} | ||
|
||
|
||
render() { | ||
const { | ||
/* eslint-disable no-unused-vars */ | ||
onFocus, | ||
onBlur, | ||
onClick, | ||
onMouseDown, | ||
onTouchStart, | ||
ref, | ||
/* eslint-enable no-unused-vars */ | ||
enhanced, | ||
children, | ||
onKeyDown, | ||
selectClassName, | ||
...otherProps | ||
} = this.props; | ||
|
||
const props = { | ||
onFocus: this.handleFocus, | ||
onBlur: this.handleBlur, | ||
onMouseDown: this.handleMouseDown, | ||
onClick: this.handleClick, | ||
onTouchStart: this.handleTouchStart, | ||
className: selectClassName, | ||
...otherProps, | ||
}; | ||
|
||
if (enhanced) { | ||
return ( | ||
<EnhancedSelect | ||
onKeyDown={this.handleKeyDown} | ||
{...props} | ||
> | ||
{children} | ||
</EnhancedSelect> | ||
); | ||
} | ||
return ( | ||
<NativeSelect | ||
onKeyDown={onKeyDown} | ||
{...props} | ||
> | ||
{children} | ||
</NativeSelect> | ||
); | ||
} | ||
} |
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.
change this in readme