Skip to content
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

Added checkbox CustomInput for additionalSignUpFields #860

Merged
merged 2 commits into from Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -368,6 +368,23 @@ var options = {
}
```


##### Checkbox field

To specify a checkbox field use: `type: "checkbox"`
The `prefill` value can determine the default state of the checkbox and it is required.

```js
var options = {
additionalSignUpFields: [{
type: "checkbox",
name: "newsletter",
prefill: "true",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason for this being a string instead of a boolean value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep the modifications as small as possible.
The prefill value is needed otherwise the checkbox will be "invalid" in the store and the registration won't go through.
Prefill must be string: https://github.com/auth0/lock/blob/master/src/connection/database/index.js#L107
I could change that, but it would need a lot of other changes elsewhere in the code

placeholder: "I hereby agree that I want to receive marketing emails from your company",
}]
}
```

#### Avatar provider

Lock can show avatars fetched from anywhere. A custom avatar provider can be specified with the `avatar` option by passing an object with the keys `url` and `displayName`. Both properties are functions that take an email and a callback function.
Expand Down
14 changes: 13 additions & 1 deletion css/index.styl
Expand Up @@ -386,7 +386,19 @@ tabsHeight = 40px
.auth0-lock-error .auth0-lock-input-wrap
border-color red
transition .3s ease-in-out

.auth0-lock-input-checkbox
text-align left
display block
font-size 12px
color rgba(0,0,0,0.54)
line-height 22px
position relative
label input
float left
margin-top 5px
span
display block
margin-left 20px

// Social

Expand Down
22 changes: 19 additions & 3 deletions src/__tests__/field/__snapshots__/custom_input.test.jsx.snap
@@ -1,4 +1,20 @@
exports[`CustomInput when type !== select calls \`changeField\` when changed 1`] = `
exports[`CustomInput when type == checkbox renders correctly as a CheckBoxInput 1`] = `
<div
className="auth0-lock-input-checkbox">
<label>
<input
checked={false}
name="custom_input"
onChange={[Function]}
type="checkbox" />
<span>
placeholder
</span>
</label>
</div>
`;

exports[`CustomInput when type == input calls \`changeField\` when changed 1`] = `
Array [
1,
"custom_input",
Expand All @@ -7,7 +23,7 @@ Array [
]
`;

exports[`CustomInput when type !== select renders correctly as a TextInput 1`] = `
exports[`CustomInput when type == input renders correctly as a TextInput 1`] = `
<div
data-__type="text_input"
data-iconUrl="iconUrl"
Expand All @@ -19,7 +35,7 @@ exports[`CustomInput when type !== select renders correctly as a TextInput 1`] =
data-value="field-value-custom_input" />
`;

exports[`CustomInput when type !== select sets isValid as true when \`isFieldVisiblyInvalid\` is false 1`] = `
exports[`CustomInput when type == input sets isValid as true when \`isFieldVisiblyInvalid\` is false 1`] = `
<div
data-__type="text_input"
data-iconUrl="iconUrl"
Expand Down
15 changes: 14 additions & 1 deletion src/__tests__/field/custom_input.test.jsx
Expand Up @@ -75,7 +75,7 @@ describe('CustomInput', () => {
expect(mock.calls[0]).toMatchSnapshot();
});
});
describe('when type !== select', () => {
describe('when type == input', () => {
beforeEach(() => defaultProps.type = 'input');
it('renders correctly as a TextInput', () => {
const CustomInput = getComponent();
Expand Down Expand Up @@ -109,4 +109,17 @@ describe('CustomInput', () => {
expect(mock.calls[0]).toMatchSnapshot();
});
});
describe('when type == checkbox', () => {
beforeEach(() => defaultProps.type = 'checkbox');
it('renders correctly as a CheckBoxInput', () => {
const CustomInput = getComponent();

expectComponent(
<CustomInput
{...defaultProps}
/>
).toMatchSnapshot();
});
});

});
2 changes: 1 addition & 1 deletion src/connection/database/index.js
Expand Up @@ -108,7 +108,7 @@ function processDatabaseOptions(opts) {
prefill = undefined;
}

const types = ["select", "text"];
const types = ["select", "text", "checkbox"];
if (type != undefined && (typeof type != "string" || types.indexOf(type) === -1)) {
l.warn(opts, `When provided, the \`type\` property of an element of \`additionalSignUpFields\` must be one of the following strings: "${types.join("\", \"")}".`);
type = undefined;
Expand Down
11 changes: 10 additions & 1 deletion src/field/custom_input.jsx
Expand Up @@ -8,6 +8,7 @@ import {
} from './index';
import TextInput from '../ui/input/text_input';
import SelectInput from '../ui/input/select_input';
import CheckboxInput from '../ui/input/checkbox_input';
import * as l from '../core/index';

const CustomInput = ({iconUrl, model, name, placeholder, type, validator}) => {
Expand All @@ -16,7 +17,7 @@ const CustomInput = ({iconUrl, model, name, placeholder, type, validator}) => {
isValid: !isFieldVisiblyInvalid(model, name),
name,
placeholder
}
};

switch(type) {
case "select":
Expand All @@ -27,6 +28,14 @@ const CustomInput = ({iconUrl, model, name, placeholder, type, validator}) => {
onClick={() => startOptionSelection(l.id(model), name, iconUrl)}
/>
);
case "checkbox":
return (
<CheckboxInput
onChange={e => changeField(l.id(model), name, `${e.target.checked}`, validator)}
checked={getFieldValue(model, name)}
{...props}
/>
);
default:
return (
<TextInput
Expand Down
30 changes: 30 additions & 0 deletions src/ui/input/checkbox_input.jsx
@@ -0,0 +1,30 @@
import React from 'react';
import InputWrap from './input_wrap';

export default class CheckboxInput extends React.Component {
render() {
const {
name,
placeholder,
checked
} = this.props;
return (
<div className="auth0-lock-input-checkbox">
<label>
<input
type="checkbox"
checked={checked === 'true'}
onChange={::this.handleOnChange}
name={name} />
<span>{placeholder}</span>
</label>
</div>
);
}

handleOnChange(e) {
if (this.props.onChange) {
this.props.onChange(e);
}
}
}
27 changes: 27 additions & 0 deletions support/design/index.html
Expand Up @@ -313,6 +313,18 @@ <h2>socialOrSms</h2>
<div class="lock-container" id="socialOrSms-container"></div>
</div> -->

<div class="container">
<div class="info">
<h2>Classic</h2>
<h3>Custom Signup Field</h3>
<p>
Use checkbox custom input
</p>

</div>
<div class="lock-container" id="custom-sign-up-container"></div>
</div>

<script src="/build/lock.design.js"></script>
<script>
var cid = "1";
Expand Down Expand Up @@ -547,4 +559,19 @@ <h2>socialOrSms</h2>
// container: "socialOrSms-container",
// rememberLastLogin: false
// }).show();

new Auth0Lock(cid, domain, {
allowedConnections: ["Username-Password-Authentication", "auth0.com", "rolodato"],
auth: { redirect: true, sso: false },
container: "custom-sign-up-container",
initialScreen: "signUp",
rememberLastLogin: false,
additionalSignUpFields: [{
type: "checkbox",
name: "newsletter",
prefill: "false",
placeholder: "I hereby agree that I want to receive marketing emails from your company",
}],
}).show();

</script>