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

Password widget #22

Merged
merged 2 commits into from
Dec 30, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Here's a list of supported alternative widgets for different JSONSchema data typ
#### `string`:

* `textarea`: a `textarea` element;
* `password`: an `input[type=password]` element;
* by default, a regular `input[type=text]` element is used.

#### `number` and `integer`:
Expand Down
2 changes: 1 addition & 1 deletion playground/samples/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module.exports = {
},
tasks: {
type: "array",
title: "Tasks",
items: {
type: "object",
title: "Task",
required: ["title"],
properties: {
title: {
Expand Down
43 changes: 35 additions & 8 deletions playground/samples/simple.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
module.exports = {
schema: {
title: "A simple todo entry",
title: "A registration form",
type: "object",
required: ["title"],
required: ["firstName", "lastName"],
properties: {
title: {type: "string", title: "Title", default: "A new task"},
done: {type: "boolean", title: "Done?", default: false}
firstName: {
type: "string",
title: "First name",
},
lastName: {
type: "string",
title: "Last name",
},
age: {
type: "integer",
title: "Age"
},
bio: {
type: "string",
title: "Bio",
},
password: {
type: "string",
title: "Password"
}
}
},
uiSchema: {
done: {
widget: "radio"
age: {
widget: "updown"
},
bio: {
widget: "textarea"
},
password: {
widget: "password"
}
},
formData: {
title: "My task",
done: false
firstName: "Chuck",
lastName: "Norris",
age: 75,
bio: "Roundhouse kicking asses since 1940",
password: "noneed",
}
};
9 changes: 7 additions & 2 deletions playground/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
width: 200px;
}

.rjsf input[type=text], .rjsf textarea {
.rjsf input[type=text],
.rjsf input[type=number],
.rjsf input[type=password],
.rjsf input[type=range],
.rjsf textarea,
.rjsf select {
font-weight: normal;
width: 350px;
width: 320px;
}

.rjsf textarea {
Expand Down
2 changes: 1 addition & 1 deletion src/components/fields/ArrayField.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ArrayField extends Component {

render() {
const {schema, uiSchema, name} = this.props;
const title = name || schema.title;
const title = schema.title || name;
const {items} = this.state;
return (
<fieldset
Expand Down
1 change: 0 additions & 1 deletion src/components/fields/BooleanField.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function BooleanField({schema, name, uiSchema, formData, required, onChange}) {
const {widget} = uiSchema;
const commonProps = {
schema,
type: schema.type,
onChange,
label: title || name,
placeholder: description,
Expand Down
3 changes: 1 addition & 2 deletions src/components/fields/StringField.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import SelectWidget from "./../widgets/SelectWidget";


function StringField({schema, name, uiSchema, formData, required, onChange}) {
const {type, title, description} = schema;
const {title, description} = schema;
const {widget} = uiSchema;
const commonProps = {
schema,
type: type,
label: title || name,
placeholder: description,
onChange,
Expand Down
2 changes: 0 additions & 2 deletions src/components/widgets/CheckboxWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { PropTypes } from "react";

function CheckboxWidget({
schema,
type,
onChange,
label,
defaultValue,
Expand All @@ -23,7 +22,6 @@ function CheckboxWidget({
if (process.env.NODE_ENV !== "production") {
CheckboxWidget.propTypes = {
schema: PropTypes.object.isRequired,
type: PropTypes.string.isRequired,
onChange: PropTypes.func,
label: PropTypes.string,
defaultValue: PropTypes.bool,
Expand Down
41 changes: 41 additions & 0 deletions src/components/widgets/PasswordWidget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { PropTypes } from "react";


function PasswordWidget({
schema,
label,
placeholder,
value,
defaultValue,
required,
onChange
}) {
return (
<input type="password"
value={value}
defaultValue={defaultValue}
placeholder={placeholder}
required={required}
onChange={(event) => onChange(event.target.value)} />
);
}

if (process.env.NODE_ENV !== "production") {
PasswordWidget.propTypes = {
schema: PropTypes.object.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
defaultValue: PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
required: PropTypes.bool,
onChange: PropTypes.func,
};
}

export default PasswordWidget;
2 changes: 0 additions & 2 deletions src/components/widgets/RadioWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { PropTypes } from "react";

function RadioWidget({
schema,
type,
options,
label,
placeholder,
Expand Down Expand Up @@ -38,7 +37,6 @@ function RadioWidget({
if (process.env.NODE_ENV !== "production") {
RadioWidget.propTypes = {
schema: PropTypes.object.isRequired,
type: PropTypes.string.isRequired,
options: PropTypes.array.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
Expand Down
2 changes: 0 additions & 2 deletions src/components/widgets/RangeWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function rangeSpec(schema) {

function RangeWidget({
schema,
type,
label,
placeholder,
value,
Expand All @@ -42,7 +41,6 @@ function RangeWidget({
if (process.env.NODE_ENV !== "production") {
RangeWidget.propTypes = {
schema: PropTypes.object.isRequired,
type: PropTypes.string.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.oneOfType([
Expand Down
6 changes: 3 additions & 3 deletions src/components/widgets/SelectWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function processValue(type, value) {

function SelectWidget({
schema,
type,
options,
label,
placeholder,
Expand All @@ -32,7 +31,9 @@ function SelectWidget({
title={placeholder}
value={value}
defaultValue={defaultValue}
onChange={(event) => onChange(processValue(type, event.target.value))}>{
onChange={(event) => {
onChange(processValue(schema.type, event.target.value));
}}>{
options.map((option, i) => {
return <option key={i} value={option}>{String(option)}</option>;
})
Expand All @@ -43,7 +44,6 @@ function SelectWidget({
if (process.env.NODE_ENV !== "production") {
SelectWidget.propTypes = {
schema: PropTypes.object.isRequired,
type: PropTypes.string.isRequired,
options: PropTypes.array.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
Expand Down
2 changes: 0 additions & 2 deletions src/components/widgets/TextWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { PropTypes } from "react";

function TextWidget({
schema,
type,
label,
placeholder,
value,
Expand All @@ -24,7 +23,6 @@ function TextWidget({
if (process.env.NODE_ENV !== "production") {
TextWidget.propTypes = {
schema: PropTypes.object.isRequired,
type: PropTypes.string.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.oneOfType([
Expand Down
2 changes: 0 additions & 2 deletions src/components/widgets/TextareaWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { PropTypes } from "react";

function TextWidget({
schema,
type,
label,
placeholder,
value,
Expand All @@ -24,7 +23,6 @@ function TextWidget({
if (process.env.NODE_ENV !== "production") {
TextWidget.propTypes = {
schema: PropTypes.object.isRequired,
type: PropTypes.string.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.string,
Expand Down
2 changes: 0 additions & 2 deletions src/components/widgets/UpDownWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function rangeSpec(schema) {

function UpDownWidget({
schema,
type,
label,
placeholder,
value,
Expand All @@ -39,7 +38,6 @@ function UpDownWidget({
if (process.env.NODE_ENV !== "production") {
UpDownWidget.propTypes = {
schema: PropTypes.object.isRequired,
type: PropTypes.string.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
value: PropTypes.oneOfType([
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PasswordWidget from "./components/widgets/PasswordWidget";
import RadioWidget from "./components/widgets/RadioWidget";
import UpDownWidget from "./components/widgets/UpDownWidget";
import RangeWidget from "./components/widgets/RangeWidget";
Expand Down Expand Up @@ -33,6 +34,7 @@ export function getAlternativeWidget(type, name) {
break;
case "string":
switch(name) {
case "password": return PasswordWidget;
case "radio": return RadioWidget;
case "select": return SelectWidget;
case "textarea": return TextareaWidget;
Expand Down
36 changes: 36 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,42 @@ describe("Form", () => {
expect(comp.state.formData).eql({foo: "b"});
});
});

describe("password", () => {
const uiSchema = {
foo: {
widget: "password"
}
};

it("should accept a uiSchema object", () => {
const {node} = createComponent({schema, uiSchema});

expect(node.querySelectorAll("[type=password]"))
.to.have.length.of(1);
});

it("should support formData", () => {
const {node} = createComponent({schema, uiSchema, formData: {
foo: "a"
}});

expect(node.querySelector("[type=password]").value)
.eql("a");
});

it("should update state when text is updated is checked", () => {
const {comp, node} = createComponent({schema, uiSchema, formData: {
foo: "a"
}});

Simulate.change(node.querySelector("[type=password]"), {
target: {value: "b"}
});

expect(comp.state.formData).eql({foo: "b"});
});
});
});

describe("string (enum)", () => {
Expand Down