From 74bc97f3d44f11b4591ccd910b7873c6ad4f5f55 Mon Sep 17 00:00:00 2001 From: Conor_Mack <36074859+Conor-Mack@users.noreply.github.com> Date: Wed, 12 Feb 2020 12:32:46 +0000 Subject: [PATCH] Completed checkbox and form field. Changes to test app (#91) * Completed checkbox and form field component with changes around how test app renders components * Tidyup --- .../materialdesign-components/package.json | 2 + .../src/Checkbox/Checkbox.svelte | 49 ++++++++++ .../src/Checkbox/_style.scss | 2 + .../src/Checkbox/index.js | 2 + .../src/ClassBuilder.js | 92 +++++++++++++------ .../src/Common/Formfield.svelte | 35 +++++++ .../src/Common/FormfieldStore.js | 19 ++++ .../src/Test/TestApp.svelte | 25 ++--- .../src/Test/createApp.js | 25 +++-- .../src/Test/props.js | 17 ++-- .../src/Test/rootComponent.js | 15 +++ .../src/Test/testComponents.js | 5 +- .../materialdesign-components/src/index.js | 15 ++- 13 files changed, 243 insertions(+), 60 deletions(-) create mode 100644 packages/materialdesign-components/src/Checkbox/Checkbox.svelte create mode 100644 packages/materialdesign-components/src/Checkbox/_style.scss create mode 100644 packages/materialdesign-components/src/Checkbox/index.js create mode 100644 packages/materialdesign-components/src/Common/Formfield.svelte create mode 100644 packages/materialdesign-components/src/Common/FormfieldStore.js create mode 100644 packages/materialdesign-components/src/Test/rootComponent.js diff --git a/packages/materialdesign-components/package.json b/packages/materialdesign-components/package.json index 15d80d9e50..f279bbc908 100644 --- a/packages/materialdesign-components/package.json +++ b/packages/materialdesign-components/package.json @@ -40,6 +40,8 @@ "license": "MIT", "gitHead": "115189f72a850bfb52b65ec61d932531bf327072", "dependencies": { + "@material/checkbox": "^4.0.0", + "@material/form-field": "^4.0.0", "@material/textfield": "^4.0.0" } } diff --git a/packages/materialdesign-components/src/Checkbox/Checkbox.svelte b/packages/materialdesign-components/src/Checkbox/Checkbox.svelte new file mode 100644 index 0000000000..3a48c444df --- /dev/null +++ b/packages/materialdesign-components/src/Checkbox/Checkbox.svelte @@ -0,0 +1,49 @@ + + + + + +
+ +
+ + + +
+
+
+
+ diff --git a/packages/materialdesign-components/src/Checkbox/_style.scss b/packages/materialdesign-components/src/Checkbox/_style.scss new file mode 100644 index 0000000000..666f84c426 --- /dev/null +++ b/packages/materialdesign-components/src/Checkbox/_style.scss @@ -0,0 +1,2 @@ +@import "@material/form-field/mdc-form-field"; +@import "@material/checkbox/mdc-checkbox.scss"; diff --git a/packages/materialdesign-components/src/Checkbox/index.js b/packages/materialdesign-components/src/Checkbox/index.js new file mode 100644 index 0000000000..3954d4062a --- /dev/null +++ b/packages/materialdesign-components/src/Checkbox/index.js @@ -0,0 +1,2 @@ +import "./_style.scss"; +export { default as checkbox } from "./Checkbox.svelte"; diff --git a/packages/materialdesign-components/src/ClassBuilder.js b/packages/materialdesign-components/src/ClassBuilder.js index 1e152ca1c6..cebed61f4f 100644 --- a/packages/materialdesign-components/src/ClassBuilder.js +++ b/packages/materialdesign-components/src/ClassBuilder.js @@ -1,38 +1,74 @@ export default class ClassBuilder { - constructor(block, customDefaults) { - this.block = `mdc-${block}` - this.customDefaults = customDefaults //will be ignored when building custom classes + constructor(block, defaultIgnoreList) { + this.block = `mdc-${block}`; + this.defaultIgnoreList = defaultIgnoreList; //will be ignored when building custom classes } - // classParams: {modifiers:[] (mdc), custom:[] (bbmd), extra:[] (any)} - blocks(classParams) { - let base = this.block - if (classParams == undefined) return base - return this.buildClass(base, classParams) + /* + handles both blocks and elementss (BEM MD Notation) + params = {elementName: string, props: {modifiers{}, customs:{}, extras: []}} + All are optional + */ + build(params) { + if (!params) return this.block; //return block if nothing passed + const { props, elementName } = params; + let base = !!elementName ? `${this.block}__${elementName}` : this.block; + if (!props) return base; + return this._handleProps(base, props); } - //elementName: string, classParams: {} - elements(elementName, classParams) { - let base = `${this.block}__${elementName}` - if (classParams == undefined) return base - return this.buildClass(base, classParams) + //Easily grab a simple element class + elem(elementName) { + return this.build({ elementName }); } - buildClass(base, classParams) { - let cls = base - const { modifiers, customs, extras } = classParams - if (modifiers) cls += modifiers.map(m => ` ${base}--${m}`).join(" ") - if (customs) - cls += Object.entries(customs) - .map(([property, value]) => { - //disregard falsy and values set by customDefaults constructor param - if (!!value && !this.customDefaults.includes(value)) { - //custom scss name convention = bbmd-[block | element]--[property]-[value] - return ` bbmd-${base}--${property}-${value}` + //use if a different base is needed than whats defined by this.block + debase(base, elementProps) { + if (!elementProps) return base; + return this._handleProps(base, elementProps); + } + + //proxies bindProps and checks for which elementProps exist before binding + _handleProps(base, elementProps) { + let cls = base; + const { modifiers, customs, extras } = elementProps; + if (!!modifiers) cls += this._bindProps(modifiers, base); + if (!!customs) cls += this._bindProps(customs, base, true); + if (!!extras) cls += ` ${extras.join(" ")}`; + return cls.trim(); + } + + /* + Handles both modifiers and customs. Use property, value or both depending + on whether it is passsed props for custom or modifiers + if custom uses the following convention for scss mixins: + bbmd-{this.block}--{property}-{value} + bbmd-mdc-button--size-large + */ + _bindProps(elementProps, base, isCustom = false) { + return Object.entries(elementProps) + .map(([property, value]) => { + //disregard falsy and values set by defaultIgnoreList constructor param + if ( + !!value && + (!this.defaultIgnoreList || !this.defaultIgnoreList.includes(value)) + ) { + let classBase = isCustom ? `bbmd-${base}` : `${base}`; + let valueType = typeof value; + + if (valueType == "string" || valueType == "number") { + return isCustom + ? ` ${classBase}--${this._convertCamel(property)}-${value}` + : ` ${classBase}--${value}`; + } else if (valueType == "boolean") { + return ` ${classBase}--${this._convertCamel(property)}`; } - }) - .join("") - if (extras) cls += ` ${extras.join(" ")}` - return cls.trim() + } + }) + .join(""); + } + + _convertCamel(str) { + return str.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`); } } diff --git a/packages/materialdesign-components/src/Common/Formfield.svelte b/packages/materialdesign-components/src/Common/Formfield.svelte new file mode 100644 index 0000000000..ec71ff2380 --- /dev/null +++ b/packages/materialdesign-components/src/Common/Formfield.svelte @@ -0,0 +1,35 @@ + + +
+ + +
diff --git a/packages/materialdesign-components/src/Common/FormfieldStore.js b/packages/materialdesign-components/src/Common/FormfieldStore.js new file mode 100644 index 0000000000..082b120ff5 --- /dev/null +++ b/packages/materialdesign-components/src/Common/FormfieldStore.js @@ -0,0 +1,19 @@ +import { writable } from "svelte/store"; + +function store() { + const { set, update, subscribe } = writable({}); + + function setInput(inp) { + update(n => { + n.input = inp; + }); + } + + return { + subscribe, + set, + setInput + }; +} + +export const fieldStore = store(); diff --git a/packages/materialdesign-components/src/Test/TestApp.svelte b/packages/materialdesign-components/src/Test/TestApp.svelte index 306ccb1169..bac1c09bb7 100644 --- a/packages/materialdesign-components/src/Test/TestApp.svelte +++ b/packages/materialdesign-components/src/Test/TestApp.svelte @@ -1,19 +1,22 @@ @@ -21,9 +24,7 @@ {#await _appPromise} loading {:then _bb} -
- {/await}