-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
ui: Release notes signup - Email subscription form #44744
Merged
craig
merged 6 commits into
cockroachdb:master
from
koorosh:ui-release-notes-signup__subscription-form
Feb 25, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f86b2c3
ui: Email subscription form
koorosh db2c672
ui: Fix import statement and definition for Button component
koorosh 7e5af42
ui: Extend Button and TextInput components
koorosh f99ac7e
ui: Correct validation message for invalid email address
koorosh 4afffab
ui: Tests on EmailSubscriptionForm
koorosh c29e322
ui: Validate input email on change
koorosh 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
import React from "react"; | ||
import cn from "classnames"; | ||
|
||
import { Text, TextTypes } from "src/components"; | ||
import "./textInput.styl"; | ||
|
||
interface TextInputProps { | ||
onChange: (value: string) => void; | ||
value: string; | ||
initialValue?: string; | ||
placeholder?: string; | ||
className?: string; | ||
name?: string; | ||
// validate function returns validation message | ||
// in case validation failed or undefined if successful. | ||
validate?: (value: string) => string | undefined; | ||
} | ||
|
||
interface TextInputState { | ||
validationMessage: string; | ||
isValid: boolean; | ||
isDirty: boolean; | ||
isTouched: boolean; | ||
needValidation: boolean; | ||
} | ||
|
||
export class TextInput extends React.Component<TextInputProps, TextInputState> { | ||
static defaultProps = { | ||
initialValue: "", | ||
validate: () => true, | ||
}; | ||
|
||
constructor(props: TextInputProps) { | ||
super(props); | ||
|
||
this.state = { | ||
isValid: true, | ||
validationMessage: undefined, | ||
isDirty: false, | ||
isTouched: false, | ||
needValidation: false, | ||
}; | ||
} | ||
|
||
validateInput = (value: string) => { | ||
const { validate } = this.props; | ||
const validationMessage = validate(value); | ||
this.setState({ | ||
isValid: !Boolean(validationMessage), | ||
validationMessage, | ||
}); | ||
} | ||
|
||
handleOnTextChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = event.target.value; | ||
const { needValidation, isValid } = this.state; | ||
if (needValidation && !isValid) { | ||
this.validateInput(value); | ||
} | ||
this.setState({ | ||
isDirty: true, | ||
}); | ||
this.props.onChange(value); | ||
} | ||
|
||
handleOnBlur = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = event.target.value; | ||
this.validateInput(value); | ||
this.setState({ | ||
isTouched: true, | ||
needValidation: true, | ||
}); | ||
} | ||
|
||
render() { | ||
const { initialValue, placeholder, className, name, value } = this.props; | ||
const { isDirty, isValid, validationMessage } = this.state; | ||
const textValue = isDirty ? value : initialValue; | ||
|
||
const classes = cn( | ||
className, | ||
"crl-text-input", | ||
{ | ||
"crl-text-input--invalid": !isValid, | ||
}, | ||
); | ||
return ( | ||
<div className="crl-text-input__wrapper"> | ||
<input | ||
name={name} | ||
type="text" | ||
value={textValue} | ||
placeholder={placeholder} | ||
className={classes} | ||
onChange={this.handleOnTextChange} | ||
onBlur={this.handleOnBlur} | ||
autoComplete="off" | ||
/> | ||
{ | ||
!isValid && ( | ||
<div className="crl-text-input__validation-container"> | ||
<Text | ||
textType={TextTypes.Caption} | ||
className="crl-text-input__error-message" | ||
> | ||
{validationMessage} | ||
</Text> | ||
</div> | ||
) | ||
} | ||
</div> | ||
); | ||
} | ||
} |
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,41 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
@require '~src/components/core/index.styl' | ||
|
||
.crl-text-input | ||
height $line-height--larger | ||
border-radius 3px | ||
border solid 1px $colors--neutral-4 | ||
background-color $colors--white | ||
padding 0 $spacing-smaller | ||
|
||
&:focus | ||
border solid 1px $colors--primary-blue-3 | ||
|
||
&:hover | ||
border solid 1px $colors--neutral-5 | ||
|
||
&--invalid | ||
border: solid 1px $colors--functional-red-3 | ||
background-color $colors--functional-red-1 | ||
&:focus | ||
border solid 1px $colors--functional-red-3 | ||
|
||
&__wrapper | ||
display flex | ||
flex-direction column | ||
|
||
&__error-message | ||
color $colors--functional-red-3 | ||
position absolute | ||
|
||
&__validation-container | ||
position relative |
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,14 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
export function isValidEmail(value: string): boolean { | ||
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
return re.test(value); | ||
} |
67 changes: 67 additions & 0 deletions
67
pkg/ui/src/views/shared/components/emailSubscriptionForm/emailSubscriptionForm.spec.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,67 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
import React from "react"; | ||
import { assert } from "chai"; | ||
import { mount, ReactWrapper } from "enzyme"; | ||
import sinon, { SinonSpy } from "sinon"; | ||
import "src/enzymeInit"; | ||
import { EmailSubscriptionForm } from "./index"; | ||
|
||
const sandbox = sinon.createSandbox(); | ||
|
||
describe("EmailSubscriptionForm", () => { | ||
let wrapper: ReactWrapper; | ||
let onSubmitHandler: SinonSpy; | ||
|
||
beforeEach(() => { | ||
sandbox.reset(); | ||
onSubmitHandler = sandbox.spy(); | ||
wrapper = mount(<EmailSubscriptionForm onSubmit={onSubmitHandler} />); | ||
}); | ||
|
||
describe("when correct email", () => { | ||
it("provides entered email on submit callback", () => { | ||
const emailAddress = "foo@bar.com"; | ||
const inputComponent = wrapper.find("input.crl-text-input").first(); | ||
inputComponent.simulate("change", { target: { value: emailAddress } }); | ||
const buttonComponent = wrapper.find("button.crl-button").first(); | ||
buttonComponent.simulate("click"); | ||
|
||
onSubmitHandler.calledOnceWith(emailAddress); | ||
}); | ||
}); | ||
|
||
describe("when invalid email", () => { | ||
beforeEach(() => { | ||
const emailAddress = "foo"; | ||
const inputComponent = wrapper.find("input.crl-text-input").first(); | ||
inputComponent.simulate("change", { target: { value: emailAddress } }); | ||
inputComponent.simulate("blur"); | ||
}); | ||
|
||
it("doesn't call onSubmit callback", () => { | ||
const buttonComponent = wrapper.find("button.crl-button").first(); | ||
buttonComponent.simulate("click"); | ||
assert.isTrue(onSubmitHandler.notCalled); | ||
}); | ||
|
||
it("submit button is disabled", () => { | ||
const buttonComponent = wrapper.find("button.crl-button.crl-button--disabled").first(); | ||
assert.isTrue(buttonComponent.exists()); | ||
}); | ||
|
||
it("validation message is shown", () => { | ||
const validationMessageWrapper = wrapper.find(".crl-text-input__error-message").first(); | ||
assert.isTrue(validationMessageWrapper.exists()); | ||
assert.equal(validationMessageWrapper.text(), "Invalid email address."); | ||
}); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
pkg/ui/src/views/shared/components/emailSubscriptionForm/emailSubscriptionForm.styl
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,22 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
@require '~src/components/core/index.styl' | ||
|
||
.email-subscription-form | ||
display flex | ||
flex-direction row | ||
|
||
&__input | ||
margin-right $spacing-smaller | ||
width 258px | ||
|
||
&__submit-button | ||
min-width 80px |
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.
Great test coverage on the functionality!