-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the checkbox control to also show radio buttons depending on i…
…f default is used
- Loading branch information
Showing
24 changed files
with
202 additions
and
77 deletions.
There are no files selected for viewing
Binary file added
BIN
+25.3 KB
...adios.spec.pw.snapshot.ts-snapshots/checkbox-snapshot-base-1-firefox-darwin.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
+25.5 KB
...os.spec.pw.snapshot.ts-snapshots/checkbox-snapshot-default-1-firefox-darwin.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
+26.2 KB
...s.spec.pw.snapshot.ts-snapshots/checkbox-snapshot-disabled-1-firefox-darwin.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
+26.3 KB
...ec.pw.snapshot.ts-snapshots/checkbox-snapshot-invalid-blur-1-firefox-darwin.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
+26.3 KB
...pw.snapshot.ts-snapshots/checkbox-snapshot-invalid-default-1-firefox-darwin.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
+29.1 KB
...snapshot.ts-snapshots/checkbox-snapshot-invalid-error-list-1-firefox-darwin.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
+27.6 KB
....pw.snapshot.ts-snapshots/checkbox-snapshot-invalid-inline-1-firefox-darwin.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 removed
BIN
-23.9 KB
...ckbox.spec.pw.snapshot.ts-snapshots/checkbox-snapshot-base-1-firefox-darwin.png
Binary file not shown.
Binary file removed
BIN
-25.4 KB
...ox.spec.pw.snapshot.ts-snapshots/checkbox-snapshot-default-1-firefox-darwin.png
Binary file not shown.
Binary file removed
BIN
-25.1 KB
...x.spec.pw.snapshot.ts-snapshots/checkbox-snapshot-disabled-1-firefox-darwin.png
Binary file not shown.
Binary file removed
BIN
-25.1 KB
...ec.pw.snapshot.ts-snapshots/checkbox-snapshot-invalid-blur-1-firefox-darwin.png
Binary file not shown.
Binary file removed
BIN
-25 KB
...pw.snapshot.ts-snapshots/checkbox-snapshot-invalid-default-1-firefox-darwin.png
Binary file not shown.
Binary file removed
BIN
-27.7 KB
...snapshot.ts-snapshots/checkbox-snapshot-invalid-error-list-1-firefox-darwin.png
Binary file not shown.
Binary file removed
BIN
-26.3 KB
....pw.snapshot.ts-snapshots/checkbox-snapshot-invalid-inline-1-firefox-darwin.png
Binary file not shown.
File renamed without changes.
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
125 changes: 125 additions & 0 deletions
125
packages/design-to-code-react/src/form/controls/type/control.checkbox-or-radios.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,125 @@ | ||
import React from "react"; | ||
import { CheckboxControlProps } from "./control.checkbox-or-radios.props"; | ||
import { classNames } from "@microsoft/fast-web-utilities"; | ||
import { isDefault } from "../utilities/form"; | ||
import cssVariables from "design-to-code/dist/stylesheets/web-components/style/global.css-variables.css"; | ||
import style from "./control.checkbox-or-radios.style.css"; | ||
import { uniqueId } from "lodash-es"; | ||
|
||
// tree-shaking | ||
cssVariables; | ||
style; | ||
|
||
const trueRadioId = `dtc-${uniqueId()}`; | ||
const falseRadioId = `dtc-${uniqueId()}`; | ||
|
||
/** | ||
* Form control definition | ||
*/ | ||
function CheckboxOrRadiosControl(props: CheckboxControlProps) { | ||
const containsDefault: boolean = typeof props.default === "boolean"; | ||
const value: boolean = | ||
typeof props.value === "boolean" | ||
? props.value | ||
: containsDefault | ||
? props.default | ||
: false; | ||
|
||
function handleChange(): (e: React.ChangeEvent<HTMLInputElement>) => void { | ||
return (e: React.ChangeEvent<HTMLInputElement>): void => { | ||
props.onChange({ | ||
value: | ||
e.target.type === "checkbox" | ||
? e.target.checked | ||
: e.target.value === "false" | ||
? false | ||
: true, | ||
}); | ||
}; | ||
} | ||
|
||
function renderCheckbox() { | ||
return ( | ||
<div | ||
className={classNames( | ||
"dtc-checkbox-control", | ||
["dtc-checkbox-control__disabled", props.disabled], | ||
[ | ||
"dtc-checkbox-control__default", | ||
isDefault(props.value, props.default), | ||
] | ||
)} | ||
> | ||
<input | ||
className={"dtc-checkbox-control_input"} | ||
id={props.dataLocation} | ||
type={"checkbox"} | ||
value={value.toString()} | ||
onChange={handleChange()} | ||
checked={value} | ||
disabled={props.disabled} | ||
ref={props.elementRef as React.Ref<HTMLInputElement>} | ||
onFocus={props.reportValidity} | ||
onBlur={props.updateValidity} | ||
/> | ||
<span className={"dtc-checkbox-control_checkmark"} /> | ||
</div> | ||
); | ||
} | ||
|
||
function renderRadios() { | ||
return ( | ||
<div | ||
className={classNames( | ||
"dtc-radio-control", | ||
["dtc-radio-control__disabled", props.disabled], | ||
["dtc-radio-control__default", isDefault(props.value, props.default)] | ||
)} | ||
> | ||
<label htmlFor={`${props.dataLocation}${trueRadioId}`}> | ||
<input | ||
className={"dtc-radio-control_input"} | ||
id={`${props.dataLocation}${trueRadioId}`} | ||
type={"radio"} | ||
name={props.dataLocation} | ||
value={"true"} | ||
onChange={handleChange()} | ||
checked={value.toString() === "true"} | ||
ref={props.elementRef as React.Ref<HTMLInputElement>} | ||
onFocus={props.reportValidity} | ||
onBlur={props.updateValidity} | ||
disabled={props.disabled} | ||
/> | ||
True | ||
</label> | ||
<label htmlFor={`${props.dataLocation}${falseRadioId}`}> | ||
<input | ||
className={"dtc-radio-control_input"} | ||
id={`${props.dataLocation}${falseRadioId}`} | ||
type={"radio"} | ||
name={props.dataLocation} | ||
value={"false"} | ||
onChange={handleChange()} | ||
checked={value.toString() === "false"} | ||
ref={props.elementRef as React.Ref<HTMLInputElement>} | ||
onFocus={props.reportValidity} | ||
onBlur={props.updateValidity} | ||
disabled={props.disabled} | ||
/> | ||
False | ||
</label> | ||
</div> | ||
); | ||
} | ||
|
||
// An assumption is made that if a default is present, | ||
// the only reason to change the value is to set the opposite value | ||
if (containsDefault) { | ||
return renderCheckbox(); | ||
} | ||
|
||
return renderRadios(); | ||
} | ||
|
||
export { CheckboxOrRadiosControl }; | ||
export default CheckboxOrRadiosControl; |
55 changes: 0 additions & 55 deletions
55
packages/design-to-code-react/src/form/controls/type/control.checkbox.tsx
This file was deleted.
Oops, something went wrong.
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